【问题标题】:Detecting circle-like shapes on binary images with lots of noise在具有大量噪声的二进制图像上检测圆形形状
【发布时间】:2019-06-18 04:24:24
【问题描述】:

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

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

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

Step 1: The original image

Step 2: The blurred image (medianblur, kernel 7)

第三步:Generated binary image AGenerated 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% 的照片,至少 strong> 包含一个部分正确的边界框(例如,在边界框中有一块球,或者只是球本身的一块)。

Original images + binary images A

Binary images B + binary images C

(我最多只能发布 8 个链接)

希望大家能给我一些建议。

【问题讨论】:

    标签: python image opencv machine-learning detection


    【解决方案1】:

    如何做到这一点有很多可能性。可能使用神经网络是一个不错的选择,但您仍然需要了解并训练其中一个来完成您的任务。

    您可以使用阈值处理和高斯模糊,作为建议,我可以添加使用归一化互相关进行模板匹配。基本上你需要一个模板(在你的情况下是一个球的图像,或者更好的是一组不同大小的图像,因为球的大小可能会根据位置而变化)。

    然后您迭代图像并检查模板何时匹配。当然,这不适用于有遮挡的图像,但它可能有助于获得一些候选对象。

    在此处的论文 (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()
    

    玩得开心!

    【讨论】:

    • 目前正在阅读和研究这个,非常有趣。谢谢!我会尽快回复您。
    【解决方案2】:

    您还可以使用 blackhat 和 tophat 形态学运算来在白色部分中查找嵌套的黑色部分。它将比阈值更稳健。

    【讨论】:

    • 我实现了这两个想法,并且已经可以说您的解决方案效果更好,因为我的解决方案的一个大问题是,当一个球(例如)位于足球场的白线上时, findContours 在阈值照片上找到整个形状(线和球一起)。使用黑帽,它经常在线和球之间创造空间——所以,这是完美的。谢谢楼主!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2014-03-25
    • 2016-06-01
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多