在OpenCV中使用Houghcircles检测圆圈

发布于 2025-02-06 02:39:09 字数 1801 浏览 2 评论 0 原文

我正在尝试根据图像是否包含圆圈来区分图像,但它一直在检测没有圆的圆圈,并错过了明显的圆圈。我一直在玩参数(尽管我不完全确定param1和2做什么),但似乎没有任何作用。这是用于检测圆并绘制其检测到的圆的代码(从另一个用户改编):

def circle_detection(filename):
    img = cv2.imread(filename)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blurred = cv2.medianBlur(gray, 25)

    minDist = 70
    param1 = 71
    param2 = 20
    minRadius = 1
    maxRadius = 20 

    circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)

    if circles is not None:
        circles = np.round(circles[0, :]).astype("int")
        circles1 = sorted(circles, key = lambda x:x[2])

        for (x, y, r) in circles1:
            r_mm = round(r/109, 2)
            cv2.circle(img, (x,y), r, (0, 0, 255), 1)
            cv2.circle(img, (x,y), 1, (0,0,255), 1)

        return img
    else:
        return None

这是我正在查看的一些图像中的一些结果。我想排除图像8、20和24,但它检测到一个圆圈。其他图像上的圆圈似乎也放错了位置:

这是我的代码排除的一些图像,其中有些显然有圆圈。我已经测试过的其他参数似乎可以检测到某些圆圈,但随后拒绝了以前具有圆圈的其他参数:

“

我不确定哪些最佳参数是获得最佳结果的最佳参数,也不知道是否有一个排除我想要的图像的更好方法。我还可以识别一些图像,但是我的代码似乎检测到圆圈或未任意地检测到。

这是一些图像的链接:

I am trying to differentiate images based on whether they contain circles or not, but it keeps on detecting circles where there are none and misses obvious ones. I have been playing around with the parameters (though I am not being entirely sure what param1 and 2 do) but nothing seems to work. Here is the code for detecting circles and drawing the circle it detects (adapted from another user):

def circle_detection(filename):
    img = cv2.imread(filename)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blurred = cv2.medianBlur(gray, 25)

    minDist = 70
    param1 = 71
    param2 = 20
    minRadius = 1
    maxRadius = 20 

    circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)

    if circles is not None:
        circles = np.round(circles[0, :]).astype("int")
        circles1 = sorted(circles, key = lambda x:x[2])

        for (x, y, r) in circles1:
            r_mm = round(r/109, 2)
            cv2.circle(img, (x,y), r, (0, 0, 255), 1)
            cv2.circle(img, (x,y), 1, (0,0,255), 1)

        return img
    else:
        return None

Here are some of my results from a few of the images I am looking at. I want to exclude images 8, 20 and 24, but it detects a circle. The circles on the other images also seem to be misplaced:
Circle Detection test

Here are some of the images that were excluded by my code this run, where some clearly have circles. Other parameters I've tested seem to detect circles for some, but then rejects others that had circles before:

Circle Rejection test

I am not sure what the best parameters are to get the best results, nor know if there is a better way to exclude the images that I want. I am ok with a few images being incorrectly identified, but my code seems to detect circles or not arbitrarily.

Here is a link to some of the images:
https://mcgill-my.sharepoint.com/:f:/g/personal/lewis_mackay_mail_mcgill_ca/EmyvEVgf-K5HiCDWOVuLti0BmoNAxBmMIeLc0dJYtkobow?e=IjEDuP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文