选择hough_gradient_alt的参数

发布于 2025-01-27 19:08:20 字数 1493 浏览 4 评论 0原文

我已经试图解决这些问题数小时,而在Stackoverflow上的类似问题也没有帮助我。

想象一下我有这个图像(实际上,它是一个更复杂的图像,但我现在会呆一个简单的例子):

”

我为hough_gradient

import cv2
import numpy as np
img = cv2.cvtColor(cv2.imread("example.png"), cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img, (9, 9), 1.5)
edges = cv2.Canny(img_blur, threshold1=50, threshold2=250)
circles = cv2.HoughCircles(
    edges, cv2.HOUGH_GRADIENT, 1.5, 20, 
    minRadius=10, maxRadius=100, param1=100, param2=100
)
img_cp = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
if circles is not None:
    circles = np.round(circles[0, :]).astype("int")
    for (x, y, r) in circles:
        cv2.circle(img_cp, (x, y), r, (0, 255, 0), 4)

这无问题起作用,并像我想要的那样检测内部圆圈:

”在此处输入图像说明“

我试图使用hough_gradient_alt,因为OpenCV Repo声称它可以更好地工作。据我了解,大多数函数调用参数应该具有相同的含义,而不是param1param2

所以,我尝试

circles = cv2.HoughCircles(
edges, cv2.HOUGH_GRADIENT_ALT, 1.5, 20, 
minRadius=10, maxRadius=100, param1=300, param2=0.9

:)

我什么也没得到。我尝试了许多不同的参数,但没有结果。我不仅不明白如何修复它,而且我不明白为什么它不起作用。任何帮助将不胜感激。

OPENCV版本4.5.5。

I am trying to solve this problems for hours already, and similar issues on StackOverflow did not help me.

Imagine I have this image (in reality it is a more complicated image but I'll stay with a simple example for now):

enter image description here

I do the following for the HOUGH_GRADIENT:

import cv2
import numpy as np
img = cv2.cvtColor(cv2.imread("example.png"), cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img, (9, 9), 1.5)
edges = cv2.Canny(img_blur, threshold1=50, threshold2=250)
circles = cv2.HoughCircles(
    edges, cv2.HOUGH_GRADIENT, 1.5, 20, 
    minRadius=10, maxRadius=100, param1=100, param2=100
)
img_cp = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
if circles is not None:
    circles = np.round(circles[0, :]).astype("int")
    for (x, y, r) in circles:
        cv2.circle(img_cp, (x, y), r, (0, 255, 0), 4)

This works without any problems and detects the inner circles just as I want:

enter image description here

I was trying to use HOUGH_GRADIENT_ALT instead as the OpenCV repo claims it to work better. From what I understand, most function call arguments should have the same meaning instead of param1 and param2.

So, I try:

circles = cv2.HoughCircles(
edges, cv2.HOUGH_GRADIENT_ALT, 1.5, 20, 
minRadius=10, maxRadius=100, param1=300, param2=0.9

)

And I get nothing. I have tried many different parameters but to no result. Not only I do not understand how to fix it but also I do not understand why it would not work. Any help would be very appreciated.

OpenCV version 4.5.5.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月无声 2025-02-03 19:08:20

我解决了问题。尝试以下尝试:编辑:我们无法使用此cv2.hough_gradient_alt

#!/usr/bin/python39
#OpenCV 4.5.5 Raspberry Pi 3/B/4B-w/4/8GB RAM, Bullseye,v11.
#Date: 10th May, 2022

import numpy as np
import cv2

img = cv2.imread('circle.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bilateral = cv2.bilateralFilter(gray,10,50,50)

minDist = 30
param1 = 30
param2 = 30
minRadius = 5
maxRadius = 100

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

if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 4)

cv2.imwrite('inner_circle.png', img)
# Show result for testing:
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

编辑:我们可以cv2.hough_gradint_alt,并从@valeria中使用了使用。

import numpy as np
import cv2


img = cv2.imread('circle.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bilateral = cv2.bilateralFilter(gray,10,50,50)       

#circles = cv2.HoughCircles(bilateral, cv2.HOUGH_GRADIENT_ALT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)
circles = cv2.HoughCircles(bilateral,
                           cv2.HOUGH_GRADIENT_ALT,
                           1.5, 20,
                           minRadius=10, maxRadius=100,
                           param1=300, param2=0.9)

if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 4)

cv2.imwrite('inner_circle1.png', img)
# Show result for testing:
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

I solved problem. Try this: Edit: we cannot used this cv2.HOUGH_GRADIENT_ALT

#!/usr/bin/python39
#OpenCV 4.5.5 Raspberry Pi 3/B/4B-w/4/8GB RAM, Bullseye,v11.
#Date: 10th May, 2022

import numpy as np
import cv2

img = cv2.imread('circle.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bilateral = cv2.bilateralFilter(gray,10,50,50)

minDist = 30
param1 = 30
param2 = 30
minRadius = 5
maxRadius = 100

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

if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 4)

cv2.imwrite('inner_circle.png', img)
# Show result for testing:
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

enter image description here

Edit: we can cv2.HOUGH_GRADINT_ALT And used snipped from @Valeria.

import numpy as np
import cv2


img = cv2.imread('circle.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bilateral = cv2.bilateralFilter(gray,10,50,50)       

#circles = cv2.HoughCircles(bilateral, cv2.HOUGH_GRADIENT_ALT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)
circles = cv2.HoughCircles(bilateral,
                           cv2.HOUGH_GRADIENT_ALT,
                           1.5, 20,
                           minRadius=10, maxRadius=100,
                           param1=300, param2=0.9)

if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 4)

cv2.imwrite('inner_circle1.png', img)
# Show result for testing:
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

enter image description here

微凉 2025-02-03 19:08:20

hough_gradint_alt似乎被打破了。
我有一个略微变形的白色圆圈的图像,黑色背景约为420x420
我称此

houghcircles(被边框,圆,hough_gradient_alt
,1#dp。累积=图像大小并检测到“小”
,20#圆圈之间的识别主义者
,600#param1 =中心阈值,
,0.7#param2 = [erfectness
,M,362); #min,max,

如果我设置了M = 194,则有效。如果我设置了M = 193或更低,则无效。从文档中,我了解此功能也应该检测到我的圆,即使将其设置为0。

HOUGH_GRADINT_ALT seems to be broken.
I have an image of a slightly deformed white circle with radius 230 on a black background that is about 420x420
I call this

HoughCircles(bordered, circles,HOUGH_GRADIENT_ALT
, 1 # dp. accumsize=imagesize and detect also 'small'
, 20 # mindist between circles
, 600# param1 = threshold for center,
, 0.7 # param2=[erfectness
, M, 362); # min,max

If I set M=194 it works. If I set M=193 or lower it does not work. From the documentation I understand this function should detect my circle even if I set it to 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文