【发布时间】:2015-11-24 01:24:31
【问题描述】:
我在 Stack Overflow 上阅读了很多关于 Circular Hough 变换的信息,但我似乎遗漏了一些东西。我编写了一个程序来检测“靶心”目标的圆圈。然而,即使在使用参数之后,该算法也相当糟糕——它忽略了大部分圆圈,有一次它找到了一个圆圈,但似乎“走开了”。我什至尝试过使用“Unsharp Mask”但无济于事。我已经添加了我的代码、我开始使用的图像和输出。我希望有人能指出我正确的方向。
import cv2
import cv2.cv as cv
import numpy as np
import math
# Load Image
img = cv2.imread('circles1.png',0)
# Apply Unsharp Mask
tmp = cv2.medianBlur(img,5)
img = cv2.addWeighted(img,1.5,tmp,-0.5,0)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
# Hough Transform
circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,5,
param1=100,param2=100,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
# Go over circles, eliminating the ones that are not cocentric enough
height, width = img.shape
center = (width/2,height/2)
for i in circles[0,:]:
# draw the outer circle
if math.sqrt((center[0]-i[0])**2 + (center[1]-i[1])**2) < 15:
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
快速解释:我加载图像,应用 Unsharp Mask,使用 Hough Transfrom 检测圆,然后绘制靠近中心的圆(我发现其他圆都是假圆)。
我尝试使用参数,这是我得到的最好的。我觉得这是一个足够简单的问题,让我感到困惑。我感谢任何帮助。
我的输入图片:
我的输出图片:
【问题讨论】:
-
您必须多次迭代
cv2.HoughCircles,更改minRadius和maxRadius参数,直到检测到所有圆圈。
标签: python opencv image-processing hough-transform