找不到圈子
Circle not found
我想从给定的图像中检测一个圆。但它并没有按照我想要的方式工作。我实现了一个圆圈检测算法,它适用于一些带圆圈的图像,但不适用于我想要的图像。我调整了参数,但无法正常工作。
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)
你的代码几乎是完美的。只是方法 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 检测器参数和半径 min/max 以及累加器分辨率。我认为这些参数会找到你想要的:
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
我想从给定的图像中检测一个圆。但它并没有按照我想要的方式工作。我实现了一个圆圈检测算法,它适用于一些带圆圈的图像,但不适用于我想要的图像。我调整了参数,但无法正常工作。
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)
你的代码几乎是完美的。只是方法 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 检测器参数和半径 min/max 以及累加器分辨率。我认为这些参数会找到你想要的:
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