gpt4 book ai didi

python - 在具有大量噪声的二值图像上检测圆形形状

转载 作者:太空宇宙 更新时间:2023-11-03 23:08:38 25 4
gpt4 key购买 nike

我试图通过使用 OpenCV(在 Python 中)的图像预处理技术几乎纯粹地检测黑白足球。我的想法如下;

  1. 处理图像(例如处理成模糊的二值照片)
  2. 为足球找到多个“候选者”(例如通过轮廓检测)
  3. 调整这些候选对象的大小(例如调整为 48x48 像素)并在一个非常简单的神经网络中输入其像素对应的 bool 值(0 = 黑色像素,1 = 白色像素),然后输出每个候选对象的置信度值
  4. 确定照片中是否有足球以及球最有可能的位置

我一直在寻找合适的人选。目前,这是我的方法;

Step 1: The original image

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)会更好,但如前所述,这只是一个例子。

The ball cropped <-- 裁剪的球

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()

Result on your image

玩得开心!

关于python - 在具有大量噪声的二值图像上检测圆形形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54346560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com