<p>如果你试图改变参数而不了解它们的作用,那么你需要很长时间来解决这个问题。</p>
<p>此说明来自<a href="http://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/" rel="noreferrer">here</a></p>
<blockquote>
<p><strong>minDist</strong>: 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.</p>
<p><strong>param1</strong>: Gradient value used to handle edge detection in the Yuen et al. method.</p>
<p><strong>param2</strong>: 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.</p>
<p><strong>minRadius</strong>: Minimum size of the radius (in pixels).</p>
<p><strong>maxRadius</strong>: Maximum size of the radius (in pixels).</p>
</blockquote>
<p>你可以清楚地看到你的图像中的圆有固定的半径,并且有最小的间隔。如果你设置这两个,你可以提高你的结果。所以读这些文件很重要。</p>
<p>关于你的问题,如果你有特别的必要使用Houghcirles,请继续微调它。可以做的改进是,使用gaussianblur进行预处理,使用adaptivethreshold而不仅仅是threshold。</p>
<p>如果没有必要使用Hough圆,我建议您使用轮廓代替。因为它更健壮,对不同的图像有很好的泛化能力。这是我用等高线得到的结果。圆圈看起来更小,因为我已经使用侵蚀6次迭代和膨胀仅3次迭代。</p>
<p><a href="https://i.stack.imgur.com/iJL36.png" rel="noreferrer"><img src="https://i.stack.imgur.com/iJL36.png" alt="finding circles using contours"/></a></p>
<p>这是我使用的代码。</p>
<pre><code>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)
</code></pre>
<p>希望这有帮助:)</p>