找不到圆圈

时间:2017-03-03 14:37:41

标签: python python-2.7 opencv opencv3.0

我想从给定的图像中检测到一个圆圈。但它只是不按我想要的方式工作。我实现了一个圆检测算法,它可以在一些圆形图像上工作,但不能在我想要的图像上工作。我调整了参数,但无法使其工作。

import cv2
import numpy as np

# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread("damn-circle.png")
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect circles in the image
blur = cv2.GaussianBlur(gray,(5,5),0)
circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT, 2, 120)
cv2.imshow("output", np.hstack([blur]))
cv2.waitKey(0)
print circles

# 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
    cv2.imshow("output", np.hstack([output]))
    cv2.waitKey(0)

Normal circle The circle I want to detect

1 个答案:

答案 0 :(得分:0)

你的代码几乎是完美的。只是方法CV_HOUGH_GRADIENT位于包cv内(至少对于opencv版本:2.4.13)。我改变了一行来提及包装,它运作良好。如果您仍未在此简单图像上获得正确的结果,则必须为OpenCV和NumPy添加特定版本。将您的行更改为: circles = cv2.HoughCircles(blur, cv2.cv.CV_HOUGH_GRADIENT, 2, 120) 你应该得到一个好结果。至少我刚做过。 image with found Hough circle shown

编辑: 啊,我不明白这个问题是关于哪个形象的。我更改了几个项目的参数,尤其是Canny探测器参数和半径最小值/最大值以及累加器分辨率。我认为这些参数会找到你想要的东西: circles = cv2.HoughCircles(blur, method = cv2.cv.CV_HOUGH_GRADIENT, minDist = 90 , dp = 1, param1 = 3, param2 = 12 , minRadius = 30, maxRadius = 50) 我找到的图片现在看起来像这样:another image with found circle