我已经查看了多个关于使用Python中的OpenCV优化圆形检测的页面,但所有这些页面都是针对给定图片的特定环境而编写的。有哪些关于cv2.HoughCircles参数的起点参考值?由于我不确定推荐值是什么,我尝试循环范围,但这并没有产生任何有前途的结果。为什么我不能检测到此图像中的任何一个圆?
import cv2
import numpy as np
image = cv2.imread('IMG_stack.png')
output = image.copy()
height, width = image.shape[:2]
maxWidth = int(width/10)
minWidth = int(width/20)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 20,param1=50,param2=50,minRadius=minWidth,maxRadius=maxWidth)
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circlesRound = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circlesRound:
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.imwrite(filename = 'test.circleDraw.png', img = output)
cv2.imwrite(filename = 'test.circleDrawGray.png', img = gray)
else:
print ('No circles found')
这应该是一个简单明了的圆形检测,但是检测到的所有圆形都远远不够接近。