尝试在openCV(python)中检测所有带有HoughCircles的圆

2024-09-27 09:32:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我在玩弄HoughCircles的参数(甚至是你在代码ex: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)

非常感谢


Tags: ofthe代码inhttpsimagecomimg
2条回答

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

Is this the best way to do circle detection with openCV or is there a more accurate way of doing it ? Also why is my code not detecting every circles ?

还有findContours函数 https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0 在我看来,这是一种更为稳健和全面的方法;你可以试试看

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

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

你应该做什么:

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

相关问题 更多 >

    热门问题