尝试在 openCV (python) 中使用 HoughCircles 检测所有圆

cod*_*ime 1 python opencv detection computer-vision

我正在关注本教程:https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/

我正在摆弄 HoughCircles 的参数(即使是那些你在代码中没有看到的参数,例如:param2),它看起来非常不准确,在我的项目中,你在图片上看到的磁盘将被放置在随机点上,我需要能够检测它们及其颜色。

目前我只能检测到几个圆圈,有时会在没有圆圈的地方绘制一些随机圆圈,所以我有点困惑。

这是使用 openCV 进行圆检测的最佳方法还是有更准确的方法?另外为什么我的代码没有检测到每个圆圈?

初始板: https: //i.stack.imgur.com/Ba6H9.jpg

绘制的圆圈:https://i.stack.imgur.com/3dY4q.jpg

我的代码:

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)
Run Code Online (Sandbox Code Playgroud)

多谢。

Yun*_*enk 5

首先,您不能指望HoughCircles在不同类型的情况下检测到所有圆圈。它不是人工智能。根据获得的期望结果,它具有不同的参数。您可以在此处查看以了解有关这些参数的更多信息。

HoughCircles是基于轮廓的函数,因此您应该确保正确检测到轮廓。在你的例子中,我确信由于照明问题会出现不良的轮廓结果。金属材料在图像处理中会引起光爆,严重影响轮廓的寻找。

你应该做什么:

  • 解决照明问题
  • 确保HoughCircle参数以获得所需的输出
  • HoughCircle您可以检测每个轮廓及其质心(可以帮助您找到它们的质心),而不是使用。然后,您可以测量轮廓点到该质心的每个长度,如果全部相等,则它是一个圆。