【问题标题】:detect circles from the image using Hough Circle transform使用霍夫圆变换从图像中检测圆
【发布时间】:2016-04-26 02:37:09
【问题描述】:

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

我的代码(OpenCV 和 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()

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

更新

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

【问题讨论】:

  • 是那些圈子吗?在我看来像椭圆

标签: python opencv hough-transform


【解决方案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)

【讨论】:

  • 只是这样做了,但输出仍然不正确..查看我的更新
  • 两个坐标都改了吗?
  • 是的,请看新图片
  • 请贴出完整代码。总共检测到 6 个中心,而图像中只有 4 个对象,只有一个圆圈。代码有问题。尝试将 param1 和 param2 更改为更小和更高的值
  • @user1388142 尝试将 param2 增加到一个较大的值,例如 50 或 75,然后看看输出是什么......
【解决方案2】:

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

即你的范围是 0

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

【讨论】:

  • afaik 0 表示没有限制,因此算法应考虑所有尺寸。
【解决方案3】:

您正在显示原始图像cv2.imshow('detected circles',myImage),但圆圈是在灰度重新缩放图像上计算的。改变

cv2.imshow('detected circles',myImage)

cv2.imshow('detected circles',img)

你就完成了。

【讨论】:

    猜你喜欢
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    相关资源
    最近更新 更多