我正在学习本教程:https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/
我在玩HoughCircles的参数(甚至那些你在代码ex: HoughCircles中没有看到的参数),它看起来非常innacurate,在我的项目中,您在图片上看到的磁盘将被放置在随机点上,我需要能够检测到它们及其颜色。
目前我只能检测到几个圆圈,有时会有一些随机的圆圈被画在没有圆圈的地方,所以我有点困惑。
这是用openCV进行圆周检测的最佳方法,还是有一种更精确的方法?另外,为什么我的代码没有检测到每个圆圈?
我的代码:
import cv2
import numpy as np
img = cv2.imread('Photos/board.jpg')
output = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)
# 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
cv2.imshow("output", np.hstack([img, output]))
cv2.waitKey(0)非常感谢。
发布于 2021-01-26 06:04:54
Hough变换对单色/二值图像的处理效果最好,因此您可能需要使用某种阈值函数对其进行预处理。函数的参数值对于正确识别是非常重要的。
这是用openCV进行圆周检测的最佳方法,还是有一种更精确的方法?另外,为什么我的代码没有检测到每个圆圈?
还有findContours函数shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0,我认为它更健壮、更通用;您可能想试一试
https://stackoverflow.com/questions/65895764
复制相似问题