- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图通过使用 OpenCV(在 Python 中)的图像预处理技术几乎纯粹地检测黑白足球。我的想法如下;
我一直在寻找合适的人选。目前,这是我的方法;
Step 2: The blurred image (medianblur, kernel 7)
第 3 步:Generated binary image A Generated binary image B
然后我使用 findContours 在二值图像上查找轮廓。如果在二值图像 B 上没有找到候选对象(使用最小和最大边界框阈值),则 findContours 将在二值图像 A 上运行(并且将返回候选对象)。如果在二值图像 B 上找到一个或多个候选者,则原始图像将被重新模糊(使用内核 15),二值图像 C 将用于寻找轮廓并返回候选者。请参阅:Generated binary image C
这是生成这些二进制图像的代码:
def generateMask(imgOriginal, rgb, margin):
lowerLimit = np.asarray(rgb)
upperLimit = lowerLimit+margin
# switch limits if margin is negative
if(margin < 0):
lowerLimit, upperLimit = upperLimit, lowerLimit
mask = cv.inRange(imgOriginal, lowerLimit, upperLimit)
return mask
# generates a set of six images with (combinations of) mask(s) applied
def applyMasks(imgOriginal, mask1, mask2):
# applying both masks to original image
singleAppliedMask1 = cv.bitwise_and(imgOriginal, imgOriginal, mask = mask1) #res3
singleAppliedMask2 = cv.bitwise_and(imgOriginal, imgOriginal, mask = mask2) #res1
# applying masks to overlap areas in single masked and original image
doubleAppliedMaskOv1 = cv.bitwise_and(
imgOriginal,
singleAppliedMask1,
mask = mask2
) #res4
doubleAppliedMaskOv2 = cv.bitwise_and(
imgOriginal,
singleAppliedMask2,
mask = mask1
) #res2
# applying masks to joint areas in single masked and original image
doubleAppliedMaskJoin1 = cv.bitwise_or(
imgOriginal,
singleAppliedMask1,
mask = mask2
) #res7
doubleAppliedMaskJoin2 = cv.bitwise_or(
imgOriginal,
singleAppliedMask2,
mask = mask1
) #res6
return (
singleAppliedMask1, singleAppliedMask2,
doubleAppliedMaskOv1, doubleAppliedMaskOv2,
doubleAppliedMaskJoin1, doubleAppliedMaskJoin2
)
def generateBinaries(appliedMasks):
# variable names correspond to output variables in applyMasks()
(sam1, sam2, damov1, damov2, damjo1, damjo2) = appliedMasks
# generate thresholded images
(_, sam1t) = cv.threshold(sam1, 0, 255, cv.THRESH_BINARY_INV)
(_, sam1ti) = cv.threshold(sam1, 0, 255, cv.THRESH_BINARY_INV)
(_, sam2t) = cv.threshold(sam2, 0, 255, cv.THRESH_BINARY)
(_, sam2ti) = cv.threshold(sam2, 0, 255, cv.THRESH_BINARY_INV)
(_, damov1t) = cv.threshold(damov1, 0, 255, cv.THRESH_BINARY)
(_, damov2t) = cv.threshold(damov2, 0, 255, cv.THRESH_BINARY_INV)
(_, damjo1t) = cv.threshold(damjo1, 0, 255, cv.THRESH_BINARY_INV)
(_, damjo2t) = cv.threshold(damjo2, 0, 255, cv.THRESH_BINARY)
# return differences in binary images
return ((damov2t-sam2t), (sam1t-damov1t), (sam2ti-damjo2t))
此示例图像中的结果很好且非常有用,尽管它看起来很不对:see result .
很容易得到这个示例图像的结果更好(例如,只返回一两个候选对象,其中包括一个完美的足球边界框),但是,在对参数进行大量参数调整后,我在这个例子中使用的似乎产生了最好的整体召回。
然而,我非常坚持某些照片,我将展示原始图像、二值 A 和 B 图像(基于使用内核 7 模糊的原始图像中值生成)和二值 C 图像(内核 15) .目前,我的方法平均每张照片返回大约 15 个候选对象,其中 25% 的照片至少包含球的完美边界框,而大约 75% 的照片至少 包含一个部分正确的边界框(例如,在边界框中有一 block 球,或者只是球本身的一部分)。
Original images + binary images A
Binary images B + binary images C
(我最多只能发布 8 个链接)
我希望你们能给我一些关于如何进行的建议。
最佳答案
关于如何做到这一点有很多可能性。可能使用神经网络是一个不错的选择,但您仍然需要为您的任务理解和训练其中之一。
您可以使用阈值化和高斯模糊,作为建议,我可以添加使用归一化互相关进行模板匹配。基本上你拿一个模板(球的图像,在你的情况下,或者更好的是,一组不同尺寸的图像,因为球可能根据位置有不同的尺寸)。
然后您迭代图像并检查模板何时匹配。当然,这不适用于有遮挡的图像,但它可能有助于获得一些候选人。
有关本文 ( https://ieeexplore.ieee.org/document/5375779 ) 或此处幻灯片 ( http://www.cse.psu.edu/~rtc12/CSE486/lecture07.pdf ) 中提到的过程的更多详细信息。
我写了一小段代码来向您展示这个想法。刚刚从图像中裁剪了球(所以我作弊了,但这只是为了展示这个想法)。它也只使用球和图像之间的差异,而更复杂的度量(如 NCC)会更好,但如前所述,这只是一个例子。
import matplotlib.pyplot as plt
import numpy as np
import pdb
import cv2
def rgb2gray(rgb):
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
if __name__ == "__main__":
ball = plt.imread('ball.jpg');
ball = rgb2gray(ball);
findtheballcol = plt.imread('findtheball.jpg');
findtheball = rgb2gray(findtheballcol)
matching_img = np.zeros((findtheball.shape[0], findtheball.shape[1]));
#METHOD 1
width = ball.shape[1]
height = ball.shape[0]
for i in range(ball.shape[0], findtheball.shape[0]-ball.shape[0]):
for j in range(ball.shape[1], findtheball.shape[1]-ball.shape[1]):
# here use NCC or something better
matching_score = np.abs(ball - findtheball[i:i+ball.shape[0], j:j+ball.shape[1]]);
# inverting so that max is what we are looking for
matching_img[i,j] = 1 / np.sum(matching_score);
plt.subplot(221);
plt.imshow(findtheball);
plt.title('Image')
plt.subplot(222);
plt.imshow(matching_img, cmap='jet');
plt.title('Matching Score')
plt.subplot(223);
#pick a threshold
threshold_val = np.mean(matching_img) * 2; #np.max(matching_img - (np.mean(matching_img)))
found_at = np.where(matching_img > threshold_val)
show_match = np.zeros_like(findtheball)
for l in range(len(found_at[0])):
yb = round(found_at[0][l]-height/2).astype(int)
yt = round(found_at[0][l]+height/2).astype(int)
xl = round(found_at[1][l]-width/2).astype(int)
xr = round(found_at[1][l]+width/2).astype(int)
show_match[yb: yt, xl: xr] = 1;
plt.imshow(show_match)
plt.title('Candidates')
plt.subplot(224)
# higher threshold
threshold_val = np.mean(matching_img) * 3; #np.max(matching_img - (np.mean(matching_img)))
found_at = np.where(matching_img > threshold_val)
show_match = np.zeros_like(findtheball)
for l in range(len(found_at[0])):
yb = round(found_at[0][l]-height/2).astype(int)
yt = round(found_at[0][l]+height/2).astype(int)
xl = round(found_at[1][l]-width/2).astype(int)
xr = round(found_at[1][l]+width/2).astype(int)
show_match[yb: yt, xl: xr] = 1;
plt.imshow(show_match)
plt.title('Best Candidate')
plt.show()
玩得开心!
关于python - 在具有大量噪声的二值图像上检测圆形形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54346560/
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
好吧,这就是我的困境: 我正在为 Reddit 子版 block 开发常见问题解答机器人。我在 bool 逻辑方面遇到了麻烦,需要一双更有经验的眼睛(这是我在 Python 中的第一次冒险)。现在,该
它首先遍历所有 y 值,然后遍历所有 x 值。我需要 X 和 y 同时改变。 For x = 3 To lr + 1 For y = 2 To lr anyl.Cells(x, 1)
假设我有一个包含 2 列的 Excel 表格:单元格 A1 到 A10 中的日期和 B1 到 B10 中的值。 我想对五月日期的所有值求和。我有3种可能性: {=SUM((MONTH(A1:A10)=
如何转换 Z-score来自 Z-distribution (standard normal distribution, Gaussian distribution)到 p-value ?我还没有找到
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我遇到过包含日期格式的时间戳日期的情况。然后我想构建一个图表,显示“点击”项目的数量“每天”, //array declaration $array1 = array("Date" => 0); $a
我是scala的新手! 我的问题是,是否有包含成员的案例类 myItem:Option[String] 当我构造类时,我需要将字符串内容包装在: Option("some string") 要么 So
我正在用 PHP 创建一个登录系统。我需要用户使用他或她的用户名或电子邮件或电话号码登录然后使用密码。因为我知道在 Java 中我们会像 email==user^ username == user 这
我在 C++ 项目上使用 sqlite,但是当我在具有文本值的列上使用 WHERE 时出现问题 我创建了一个 sqlite 数据库: CREATE TABLE User( id INTEGER
当构造函数是显式时,它不用于隐式转换。在给定的代码片段中,构造函数被标记为 explicit。那为什么在 foo obj1(10.25); 情况下它可以工作,而在 foo obj2=10.25; 情况
我知道这是一个主观问题,所以如果需要关闭它,我深表歉意,但我觉得它经常出现,让我想知道是否普遍偏爱一种形式而不是另一种形式。 显然,最好的答案是“重构代码,这样你就不需要测试是否存在错误”,但有时没有
这两个 jQuery 选择器有什么区别? 以下是来自 w3schools.com 的定义: [attribute~=value] 选择器选择带有特定属性,其值包含特定字符串。 [attribute*=
为什么我们需要CSS [attribute|=value] Selector根本当 CSS3 [attribute*=value] Selector基本上完成相同的事情,浏览器兼容性几乎相似?是否存在
我正在解决 regx 问题。我已经有一个像这样的 regx [0-9]*([.][0-9]{2})。这是 amont 格式验证。现在,通过此验证,我想包括不应提供 0 金额。比如 10 是有效的,但
我正在研究计算机科学 A 考试的样题,但无法弄清楚为什么以下问题的正确答案是正确的。 考虑以下方法。 public static void mystery(List nums) { for (
好的,我正在编写一个 Perl 程序,它有一个我收集的值的哈希值(完全在一个完全独立的程序中)并提供给这个 Perl 脚本。这个散列是 (string,string) 的散列。 我想通过 3 种方式对
我有一个表数据如下,来自不同的表。仅当第三列具有值“债务”并且第一列(日期)具有最大值时,我才想从第四列中获取最大值。最终值基于 MAX(DATE) 而不是 MAX(PRICE)。所以用简单的语言来说
我有一个奇怪的情况,只有错误状态保存到数据库中。当“状态”应该为 true 时,我的查询仍然执行 false。 我有具有此功能的 Controller public function change_a
我有一个交易表(针对所需列进行了简化): id client_id value 1 1 200 2 2 150 3 1
我是一名优秀的程序员,十分优秀!