gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:34:42 35 4
gpt4 key购买 nike

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

enter image description here

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

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

enter image description here

更新

谢谢,现在可以使用了。通过将 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)

关于python - 使用 Hough Circle 变换从图像中检测圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854342/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com