简体   繁体   English

如何改进圆检测

[英]How to improve circle detection

I am trying to detect certain circles in this image:我正在尝试检测此图像中的某些圆圈:

在此处输入图片说明

This is the best result I have succeeded:这是我成功的最好结果:

在此处输入图片说明

You can see there are 4 circles that it detected that I wasn't trying to detect and 1 circle that it missed.您可以看到它检测到我没有尝试检测的 4 个圆圈和它错过的 1 个圆圈。

Here is the code i have used:这是我使用的代码:

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

I have tried playing with the min and max radius params but without real success, any suggestions would be helpful.我曾尝试使用最小和最大半径参数,但没有真正成功,任何建议都会有所帮助。

I don't see why you would need all this pre-processing.我不明白你为什么需要所有这些预处理。 You have perfect circles in your image, you know their exact radius...您的图像中有完美的圆,您知道它们的确切半径......

All your pre-processing is doing is to render good input into poor input.您所做的所有预处理都是将好的输入呈现为差的输入。 You remove the nice circle outlines you have and then try to recreate them from what is left after medianing your image to death :)您删除了漂亮的圆形轮廓,然后尝试在将图像中位数至死后剩余的部分重新创建它们:)

在此处输入图片说明

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

在此处输入图片说明

I think you should really research how those algorithms work.我认为你应该真正研究这些算法是如何工作的。 You're code looks like you blindly applied stuff you found online.你的代码看起来就像你盲目地应用了你在网上找到的东西。

maybe this solution will help.也许这个解决方案会有所帮助。

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上面代码的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM