如何使用opencv python(hough circles)在给定图像中找到圆圈? [英] How to find the circle in the given images using opencv python (hough circles )?

查看:71
本文介绍了如何使用opencv python(hough circles)在给定图像中找到圆圈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图片,它包含基于圆的图章.我正在尝试使用 hough cicles 算法找到基于图章的圆,但我找不到那个圆.

I have an image,it contan the circle based stamp .I am trying to find that stamp based circle using hough cicles algorithm but I could not find that circle .

我的代码是:

image1=cv2.imread('1.jpg')
gray_image=cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
th2 = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
        cv2.THRESH_BINARY,19,2)
output=image1.copy()
circles = cv2.HoughCircles(th2, cv2.HOUGH_GRADIENT, 1.3, 100)
if circles is not None:
     circles = np.round(circles[0, :]).astype("int")
     for (x, y, r) in circles:
    
         cv2.circle(output, (x, y), r, (0, 255, 0), 2)
         print(x,y,r)
plt.imshow(output)

输入图像

输出图像:

我越来越像输出图像圈,但我无法获得图章圈.请告诉我如何解决这个问题或如何在算法中设置参数?谢谢..

I am getting like output image circle but i could not get the stamp circle .Please tell me how to solve this problem or how to set the parameter inside algorithms ? Thanks ..

推荐答案

您的代码非常好,只是您需要向 HoughCircles 添加其他参数.此外,您无需在运行 Hough 变换之前对图像运行阈值处理,因为 Canny 边缘将在任何情况下运行.您可以在调用 HoughCircles 时提供精明的参数 - param1 和 param2.

Your code is perfectly fine except that you need to add other parameters to HoughCircles. Also, you do not need to run thresholding on the image before running Hough transform since Canny edge will run on it in any case. You instead provide canny parameters - param1 and param2 in call to HoughCircles.

image1=cv2.imread('/path/to/your/image/doc_hough.jpeg')
gray_image=cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

output=image1.copy()
circles = cv2.HoughCircles(gray_image, cv2.HOUGH_GRADIENT, 1.3, 100, param1=80, param2=140, minRadius=30, maxRadius=100)
if circles is not None:
     circles = np.round(circles[0, :]).astype("int")
     for (x, y, r) in circles:
    
         cv2.circle(output, (x, y), r, (0, 255, 0), 2)
         print(x,y,r)
plt.imshow(output)

这篇关于如何使用opencv python(hough circles)在给定图像中找到圆圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆