OPENCV:霍夫圆圈,检测对象的麻烦

发布于 2025-01-19 05:27:32 字数 509 浏览 3 评论 0原文

当谈到 OpenCV 时,我是一个完全的初学者,当尝试检测特定大小的圆圈时,我不知道从哪里开始,下面是我当前的代码(不多)以及我试图检测的图像,如果有人可以的话帮助我或给我一些建议,我将不胜感激,(我已将图像转换为灰度并添加高斯模糊,以便更容易检测)谢谢!

图片

import cv2
import numpy as np


test = cv2.imread('test.jpg')
gray_img = cv2.cvtColor(test, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray_img,  5)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)


cv2.imshow("HoughCirlces",  test)
cv2.waitKey()
cv2.destroyAllWindows()

I am a complete beginner when it comes to OpenCV, I have no clue where to start when trying to detect circles of a certain size, below is my current code (not much) along with the image I am trying to detect, if anyone could help me or give me some advice it would be much appreciated, (i have converted image to grayscale and added gaussian blur so it is easier to detect) Thanks!

Image

import cv2
import numpy as np


test = cv2.imread('test.jpg')
gray_img = cv2.cvtColor(test, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray_img,  5)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)


cv2.imshow("HoughCirlces",  test)
cv2.waitKey()
cv2.destroyAllWindows()

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

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

发布评论

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

评论(1

仄言 2025-01-26 05:27:32

干得好,你已经快到了,你现在所要​​做的就是实际应用 CHT。您正在寻找的函数是 cv2.HoughCircles()。您需要传递图像,在您的情况下,您可以使用 img 或 cimg 以及一些可能需要调整的参数。这是一些模板代码

import cv2
import numpy as np


test = cv2.imread('test.jpg')
gray_img = cv2.cvtColor(test, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray_img,  5)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow("HoughCirlces",  test)
cv2.waitKey()
cv2.destroyAllWindows()

您也可以查看文档和教程。我将在下面链接它们。
教程:https://docs.opencv.org/4.x/ da/d53/tutorial_py_houghcircles.html
文档: https://docs.opencv.org/4.x/dd/d1a/group__imgproc__feature.html#ga47849c3be0d0406ad3ca45db65a25d2d

great work, you're almost there, all you have to do now is actually apply the CHT. The function you are looking for is cv2.HoughCircles(). You need to pass the image, in your case you can use either img or cimg and some parameters that might require tuning. Here is some template code

import cv2
import numpy as np


test = cv2.imread('test.jpg')
gray_img = cv2.cvtColor(test, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray_img,  5)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow("HoughCirlces",  test)
cv2.waitKey()
cv2.destroyAllWindows()

You can also take a look at the documentation and tutorial. I'll link them below.
Tutorial: https://docs.opencv.org/4.x/da/d53/tutorial_py_houghcircles.html
Docs: https://docs.opencv.org/4.x/dd/d1a/group__imgproc__feature.html#ga47849c3be0d0406ad3ca45db65a25d2d

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