使用Hough Circle变换从图像中检测圆圈

问题描述 投票:2回答:3

我试图使用OpenCV的Hough Circles功能从下图中检测圆圈

enter image description here

我的代码(OpenCV with Python)

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

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

enter image description here

UPDATE

谢谢它现在正在工作。通过设置param2高,我能够检测到2个圆圈。我错误地展示了它们,现在一切都很好

python opencv hough-transform
3个回答
1
投票

你似乎给坐标错了。

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

1
投票

好吧,有一点是最大半径设置为0 ......

即你的范围是0 <radius <0。

除非我错了(?)这是一个有点限制性的耶?


0
投票

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

cv2.imshow('detected circles',myImage)

对于

cv2.imshow('detected circles',img)

你完成了

© www.soinside.com 2019 - 2024. All rights reserved.