2017-05-08 578 views
0

我试图用houghcircles()检测此裁剪图像中的小圆圈。我试图改变它的参数,但是当我增加参数2以上50 maxRadius也得到错误当其值小于100。现在,它运行,但性能差它得到错误 这是原始图像: enter image description here使用houghCircles检测小圆圈(OpenCV)

而且这是结果图像: enter image description here

这是我的代码:

from imutils.perspective import four_point_transform 
from imutils import contours 
import numpy as np 
import argparse 
import imutils 
import cv2 

im = cv2.imread('crop.png') 
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 
ret,thresh = cv2.threshold(imgray, 200, 255,cv2.THRESH_BINARY) 
cimg = cv2.cvtColor(thresh,cv2.COLOR_GRAY2BGR) 

c = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, 0.5, 41, param1=70, 
param2=30, minRadius=10,maxRadius=175) 
c = np.uint16(np.around(c)) 

for i in c[0,:]: 
    # draw the outer circle 
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2) 
    # draw the center of the circle 
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) 

cv2.namedWindow('img',cv2.WINDOW_NORMAL) 
cv2.resizeWindow('img', 800,800) 
cv2.imshow('img',cimg) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

请,我应该如何改变参数?

回答

2

如果您尝试改变参数而不理解它们的作用,则需要很长时间才能解决此问题。

这说明从here

minDist: Minimum distance between the center (x, y) coordinates of detected circles. If the minDist is too small, multiple circles in the same neighborhood as the original may be (falsely) detected. If the minDist is too large, then some circles may not be detected at all.

param1: Gradient value used to handle edge detection in the Yuen et al. method.

param2: Accumulator threshold value for the cv2.HOUGH_GRADIENT method. The smaller the threshold is, the more circles will be detected (including false circles). The larger the threshold is, the more circles will potentially be returned.

minRadius: Minimum size of the radius (in pixels).

maxRadius: Maximum size of the radius (in pixels).

你可以清楚地看到,在图像的圆圈有固定的半径和具有最小距离隔开。如果你设置这两个,你可以改善你的结果。所以阅读文档很重要。

而关于你的问题,如果你有特定的必要性,使用Houghcircles继续前进并微调它。你可以做的改进之处是,使用gaussianblur进行预处理,使用adaptivethreshold而不是阈值。

如果没有必要使用Hough圈,我建议您改用轮廓。因为它更强大,并能很好地适应不同的图像。这是我使用轮廓的结果。圆圈显得更小,因为我已经使用侵蚀进行了6次迭代,并且仅对3次迭代进行了扩张。

finding circles using contours

这里是我使用的代码。

import numpy as np 
import cv2 

image_color= cv2.imread("Ye3gs.png") 
image_ori = cv2.cvtColor(image_color,cv2.COLOR_BGR2GRAY) 

lower_bound = np.array([0,0,10]) 
upper_bound = np.array([255,255,195]) 

image = image_color 

mask = cv2.inRange(image_color, lower_bound, upper_bound) 

# mask = cv2.adaptiveThreshold(image_ori,255,cv2.ADAPTIVE_THRESH_MEAN_C,\ 
#    cv2.THRESH_BINARY_INV,33,2) 

kernel = np.ones((3, 3), np.uint8) 

#Use erosion and dilation combination to eliminate false positives. 
#In this case the text Q0X could be identified as circles but it is not. 
mask = cv2.erode(mask, kernel, iterations=6) 
mask = cv2.dilate(mask, kernel, iterations=3) 

closing = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) 

contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 
     cv2.CHAIN_APPROX_SIMPLE)[0] 
contours.sort(key=lambda x:cv2.boundingRect(x)[0]) 

array = [] 
ii = 1 
print len(contours) 
for c in contours: 
    (x,y),r = cv2.minEnclosingCircle(c) 
    center = (int(x),int(y)) 
    r = int(r) 
    if r >= 6 and r<=10: 
     cv2.circle(image,center,r,(0,255,0),2) 
     array.append(center) 

cv2.imshow("preprocessed", image_color) 
cv2.waitKey(0) 

希望这有助于:)

+0

谢谢你的帮助,但它给了我一个错误:'contours.sort(键=拉姆达X:cv2.boundingRect(X)[0]) 类型错误:'关键”是无效的关键字参数此功能 ' –

+1

非常感谢它为我工作,当我加入这个功能'高清get_contour_precedence(轮廓的cols): tolerance_factor = 10 起源= cv2.boundingRect(轮廓) 回报(( [1] // tolerance_factor)* tolerance_factor)* cols + origin [0]' –

+0

我建议使用轮廓+ minEnclosingC然后将圆形边界/区域与检测到的轮廓进行比较,以滤除非圆形轮廓 – Micka