繁体   English   中英

使用霍夫圆变换从图像中检测圆

[英]detect circles from the image using Hough Circle transform

我正在尝试使用 OpenCV 的 Hough Circles 函数从下图中检测圆圈

在此处输入图片说明

我的代码(带有 Python 的 OpenCV)

myImage = cv2.imread("C:\\sample.jpg") 
img = cv2.resize(myImage,(640,480))        
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(gray,cv2.cv.CV_HOUGH_GRADIENT,1,10, param1=50,param2=35,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(myImage,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(myImage,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',myImage)
cv2.waitKey(0)
cv2.destroyAllWindows()

但由于某种原因,我无法获得正确的输出。 我得到以下输出

在此处输入图片说明

更新

谢谢它现在正在工作。 通过将param2设置为高,我可以检测到 2 个圆圈。 我错误地显示它们,现在一切都很好

你好像给坐标错了。

    # draw the outer circle
    cv2.circle(myImage,(i[1],i[0]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(myImage,(i[1],i[0]),2,(0,0,255),3)

将其更改为

    # draw the outer circle
    cv2.circle(myImage,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(myImage,(i[0],i[1]),2,(0,0,255),3)

嗯,有一件事是最大半径设置为 0 ...

即您的范围是 0 < 半径 < 0。

除非我弄错了(?),这有点限制,是吗?

您正在显示原始图像cv2.imshow('detected circles',myImage)但这些圆圈是在灰色 reescales 图像上计算的。 改变

cv2.imshow('detected circles',myImage)

为了

cv2.imshow('detected circles',img)

你就完成了。

暂无
暂无

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

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