我正在学习本教程: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:07:47
首先,您不能期望HoughCircles在不同类型的情况下检测所有的圆圈。这不是人工智能。根据所期望的结果,它具有不同的参数。您可以检查这里以了解有关这些参数的更多信息。
HoughCircles是一个基于轮廓的函数,因此您应该确保正确地检测到轮廓。在你的例子,我相信坏的轮廓结果会出现,因为照明问题。金属材料在图像处理中造成光爆炸,这严重影响了轮廓的寻找。
你应该做的是:
HoughCircle参数以获得所需的输出HoughCircle,您可以检测每个轮廓和它们的质量中心( 时刻帮助您找到它们的质量中心)。然后,你可以测量到质量中心的每一个等高线点的长度,如果它们都相等,那么它就是一个圆。发布于 2021-01-26 06:04:54
Hough变换对单色/二值图像的处理效果最好,因此您可能需要使用某种阈值函数对其进行预处理。函数的参数值对于正确识别是非常重要的。
这是用openCV进行圆周检测的最佳方法,还是有一种更精确的方法?另外,为什么我的代码没有检测到每个圆圈?
还有findContours函数shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0,我认为它更健壮、更通用;您可能想试一试
https://stackoverflow.com/questions/65895764
复制相似问题