首页 > 解决方案 > 如何使用opencv python(hough circles)在给定图像中找到圆圈?

问题描述

我有一个图像,它包含基于圆圈的印章。我正在尝试使用 hough cicles 算法找到基于印章的圆圈,但我找不到那个圆圈。

我的代码是:

image1=cv2.imread('1.jpg')
gray_image=cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
th2 = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
        cv2.THRESH_BINARY,19,2)
output=image1.copy()
circles = cv2.HoughCircles(th2, cv2.HOUGH_GRADIENT, 1.3, 100)
if circles is not None:
     circles = np.round(circles[0, :]).astype("int")
     for (x, y, r) in circles:
    
         cv2.circle(output, (x, y), r, (0, 255, 0), 2)
         print(x,y,r)
plt.imshow(output)

输入图像 在此处输入图像描述

输出图像:

在此处输入图像描述

我越来越喜欢输出图像圈,但我无法获得印章圈。请告诉我如何解决这个问题或如何在算法中设置参数?谢谢 ..

标签: pythonopencvimage-processingcomputer-vision

解决方案


你可以试试这个:

image = cv2.imread('1.jpg')
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 1000)
# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
    # show the output image
    plt.imshow(output)

魔法就在这里,cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 1000) 您可以尝试accumulator value --> 1.2, minDist --> 1000为您的图像找到最佳参数 ( )


推荐阅读