python - 如何使用opencv python(hough circles)在给定图像中找到圆圈?
问题描述
我有一个图像,它包含基于圆圈的印章。我正在尝试使用 hough cicles 算法找到基于印章的圆圈,但我找不到那个圆圈。
我的代码是:
image1=cv2.imread('1.jpg')
gray_image=cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
th2 = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
cv2.THRESH_BINARY,19,2)
output=image1.copy()
circles = cv2.HoughCircles(th2, cv2.HOUGH_GRADIENT, 1.3, 100)
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(output, (x, y), r, (0, 255, 0), 2)
print(x,y,r)
plt.imshow(output)
输出图像:
我越来越喜欢输出图像圈,但我无法获得印章圈。请告诉我如何解决这个问题或如何在算法中设置参数?谢谢 ..
解决方案
你可以试试这个:
image = cv2.imread('1.jpg')
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 1000)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
# show the output image
plt.imshow(output)
魔法就在这里,cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 1000)
您可以尝试accumulator value --> 1.2, minDist --> 1000
为您的图像找到最佳参数 ( )
推荐阅读
- python - 如何检索相应名称的值以在方程式中将其计算为python中df中的新列
- ms-access - 您请求的更改不成功(错误 3022)
- kubernetes - 使用 Azure DevOps Pipeline 部署时收到 VS 2019 Container Application for Kubernetes 错误
- r - 按一列的最大值汇总数据框
- java - 如何替换 ArrayList 的特定部分?
- php - 通过表单的 SQL 注入在 PHP 5.6 中不起作用
- oracle - 如何以 root 用户身份登录 Oracle 的 rman
- php - php将文件保存到相对路径
- amazon-web-services - 在 dynamodb 中存储具有超过 15 个属性的对象时,最好将其作为对象存储在 value 中还是将它们分布在列中?
- php - PHP 检查一个变量是否等于其他三个变量之一