【问题标题】:Trying to detect all the circles with HoughCircles in openCV (python)尝试在openCV(python)中使用HoughCircles检测所有圆圈
【发布时间】:2021-01-26 04:07:26
【问题描述】:

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

我在玩 HoughCircles 的参数(即使是你在代码中看不到的参数,例如:param2),看起来很不准确,在我的项目中,你在图片上看到的磁盘将被放置在随机点上我需要能够检测到它们和它们的颜色。

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

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

初始板:https://imgur.com/BrPB5Ox

画圈:https://imgur.com/dT7k29E

我的代码:

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)

非常感谢。

【问题讨论】:

    标签: python opencv computer-vision detection


    【解决方案1】:

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

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

    你应该做什么:

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

    【讨论】:

      【解决方案2】:

      霍夫变换在单色/二值图像上效果最好,因此您可能需要使用某种阈值函数对其进行预处理。函数的参数值对于正确识别非常重要。

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

      还有findContours函数 https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0 根据我的喜好,它更健壮和通用;你可能想试一试

      【讨论】:

      • 感谢您的信息,如果冰球的颜色很重要怎么办?
      • @codetime 然后你ll need some way of filtering out objects of ifferent colors, e.g. inrange` cv2.core 函数。这实际上可能会极大地改善结果 youre getting, since this allows for more agressive preprocessing (much of the background will be culled, noice supressed etc.); *generality* will be negatively affected, though, because selecting colors will bind you to the selected colors. Theres 这是youtu.be/WQeoO7MI0Bs?t=3374的有趣演示@
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多