简体   繁体   English

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

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

I am trying to detect circles from the following image using Hough Circles function of OpenCV我正在尝试使用 OpenCV 的 Hough Circles 函数从下图中检测圆圈

在此处输入图片说明

My code (OpenCV with Python)我的代码(带有 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()

But due to some reason, I am not able to get the correct outputs.但由于某种原因,我无法获得正确的输出。 I am getting the following output我得到以下输出

在此处输入图片说明

UPDATE更新

Thanks it's working now.谢谢它现在正在工作。 By setting the param2 high I am able to detect 2 circles.通过将param2设置为高,我可以检测到 2 个圆圈。 I was displaying them wrongly and now everything is fine我错误地显示它们,现在一切都很好

You seem to have given the coordinates wrong.你好像给坐标错了。

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

Change that to将其更改为

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

Well, one thing is that max radius is set at 0...嗯,有一件事是最大半径设置为 0 ...

ie your range is 0 < radius < 0.即您的范围是 0 < 半径 < 0。

Unless I'm mistaken (?) that's a wee bit restrictive yeah?除非我弄错了(?),这有点限制,是吗?

You are showing the original image cv2.imshow('detected circles',myImage) but the circles are computed over the gray reescales image.您正在显示原始图像cv2.imshow('detected circles',myImage)但这些圆圈是在灰色 reescales 图像上计算的。 Change改变

cv2.imshow('detected circles',myImage)

for为了

cv2.imshow('detected circles',img)

and you are done.你就完成了。

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

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