python - 使用 FloodFill 后 OpenCV HoughCircles 无法正常工作
问题描述
我正在使用 HoughCircles 并且在灰度图像上效果很好,尝试使用二进制文件并且仍然可以按预期工作。在我使用 FloodFill 仅检测图像的内圈后,HoughCircles 返回一个空数组。在这里我有代码工作:
import cv2
import numpy as np
# Read image.
img = cv2.imread('terminales.webp', cv2.IMREAD_COLOR)
# Convert to grayscale.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Blur using 3 * 3 kernel.
gray_blurred = cv2.blur(gray, (3, 3))
ret,binary_img = cv2.threshold(gray_blurred,220,255,cv2.THRESH_BINARY_INV)
# Copy the thresholded image.
im_floodfill = binary_img.copy()
# Mask used to flood filling.
# Notice the size needs to be 2 pixels than the image.
h, w = binary_img.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
# Floodfill from point (0, 0)
cv2.floodFill(im_floodfill, mask, (0,0), 255);
# Invert floodfilled image
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
# Combine the two images to get the foreground.
im_out = binary_img | im_floodfill_inv
# Apply Hough transform on the blurred image.
detected_circles = cv2.HoughCircles(binary_img,
cv2.HOUGH_GRADIENT, 1, 1, param1 = 50,
param2 = 30, minRadius = 10, maxRadius = 150)
# Draw circles that are detected.
if detected_circles is not None:
# Convert the circle parameters a, b and r to integers.
detected_circles = np.uint16(np.around(detected_circles))
for pt in detected_circles[0, :]:
a, b, r = pt[0], pt[1], pt[2]
# Draw the circumference of the circle.
cv2.circle(img, (a, b), r, (0, 255, 0), 2)
# Draw a small circle (of radius 1) to show the center.
cv2.circle(img, (a, b), 1, (0, 0, 255), 3)
else:
print("Nothing detected")
cv2.imshow("Detected Circle", img)
cv2.waitKey(0)
如果我在 HoughCircle 中使用“im_floodfill”、“im_out”或“im_floodfill_inv”而不是“binary_img”,我不会得到任何结果。我不知道我是否错过了一些无法完成的事情。
解决方案
我能够检测到减少参数 2 并增加 minDist 的东西。
detected_circles_inner = cv2.HoughCircles(im_floodfill,
cv2.HOUGH_GRADIENT, 1, 30, param1 = 50,
param2 = 15, minRadius = 10, maxRadius = 150)
推荐阅读
- assembly - 我可以反编译 WebAssembly 模块吗?
- c# - 在 lucene.net 中使用带有多查询子句的 Query/BooleanQuery 突出显示文本
- android - 如何使用android Paging 3在某个位置插入项目
- java - 从 Netbeans JTable 中的 sparql 获取结果集中的多个列
- javascript - 如何使用 passportjs 为两种不同的模型创建策略
- oracle11g - 在不安装 oracle 的情况下安装 oracle_fdw - 可能吗?
- mysql - AWS Glue 从源或动态 DataFrame 过滤记录
- newrelic - (SPA)的newrelic浏览器工具:如何捕获完整的#路由
- python - 解析网页(抓取)从 span 类标签中返回两个元素
- python - python中数学级数的快速乘法/求和