【问题标题】:How to improve circle detection如何提高圆检测
【发布时间】:2020-01-27 12:44:34
【问题描述】:

我正在尝试检测这张图片中的某些圆圈:

这是我成功的最好结果:

您可以看到,它检测到了 4 个我没有尝试检测的圆圈,还有 1 个未检测到的圆圈。

这是我使用的代码:

def draw_circles(img, circles):
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    for i in circles[0,:]:
    # draw the outer circle
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
        cv2.putText(cimg,str(i[0])+str(',')+str(i[1]), (i[0],i[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.4, 255)
    return cimg

def detect_circles(image_path):
    gray = cv2.imread(image_path, 0)
    gray_blur = cv2.medianBlur(gray, 13)  # Remove noise before laplacian
    gray_lap = cv2.Laplacian(gray_blur, cv2.CV_8UC1, ksize=5)
    dilate_lap = cv2.dilate(gray_lap, (3, 3))  # Fill in gaps from blurring. This helps to detect circles with broken edges.
    # Furture remove noise introduced by laplacian. This removes false pos in space between the two groups of circles.
    lap_blur = cv2.bilateralFilter(dilate_lap, 5, 9, 9)
    # Fix the resolution to 16. This helps it find more circles. Also, set distance between circles to 55 by measuring dist in image.
    # Minimum radius and max radius are also set by examining the image.
    circles = cv2.HoughCircles(lap_blur, cv2.HOUGH_GRADIENT, 16, 80, param2=450, minRadius=20, maxRadius=40)
    cimg = draw_circles(gray, circles)
    print("{} circles detected.".format(circles[0].shape[0]))
    # There are some false positives left in the regions containing the numbers.
    # They can be filtered out based on their y-coordinates if your images are aligned to a canonical axis.
    # I'll leave that to you.
    return cimg

plt.imshow(detect_circles('test.jpeg'))

我尝试过使用最小和最大半径参数,但没有真正成功,任何建议都会有所帮助。

【问题讨论】:

  • 你不能指望 houghcircle 函数完全正确。您可以使用边缘检测和 minEnclosureCircle 来支持它的结果。

标签: python opencv image-processing


【解决方案1】:

我不明白您为什么需要所有这些预处理。你的图像中有完美的圆,你知道它们的确切半径......

您所做的所有预处理都是将好的输入呈现为差的输入。您删除了漂亮的圆形轮廓,然后尝试从将图像中位至死后剩下的部分重新创建它们:)

def detect_circles(image_path):
    gray = cv2.imread(image_path, 0)
    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 2, 60, param2=100, minRadius=20, maxRadius=40)
    cimg = draw_circles(gray, circles)
    print("{} circles detected.".format(circles[0].shape[0]))
    return cimg

我认为您应该真正研究这些算法的工作原理。你的代码看起来就像你盲目地应用了你在网上找到的东西。

【讨论】:

    【解决方案2】:

    也许这个解决方案会有所帮助。

    def draw_circles(img, circles):
        cimg = img.copy()
        for i in circles[0,:]:
        # draw the outer circle
            cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
            # draw the center of the circle
            cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
            cv2.putText(cimg,str(i[0])+str(',')+str(i[1]), (i[0],i[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.4, 255)
        return cimg
    
    def detect_circles(image_path):
        # open color image
        image = cv2.imread(image_path)
        image_blur = cv2.medianBlur(image, 3)
        # find edges
        edges = cv2.Canny(image_blur, 5, 50)
        # let's clean the neighboring pixels
        edges = cv2.medianBlur(edges, 3)
    
        circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 2, 80, param1=100, param2=45, minRadius=20, maxRadius=40)
        cimg = draw_circles(image, circles)
        print("{} circles detected.".format(circles[0].shape[0]))
        return cimg
    

    The result of the code above

    【讨论】:

      猜你喜欢
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      相关资源
      最近更新 更多