简体   繁体   English

如何使用 python 操作霍夫圆以检测给定图像中的所有圆?

[英]How to manipulate hough circles to detect all circles in the given image using python?

import numpy as np
import cv2
import matplotlib.pyplot as plt

import copy

image = cv2.imread('C:/Users/Deepak Padhi/Desktop/download.png')

#cv2.imshow('Filtered_original',image)

image = cv2.GaussianBlur(image,(5,5),0)

orig_image = np.copy(image)
gray= image[:,:,1]
output=gray.copy()
##cv2.imshow("gray", gray)
##cv2.waitKey(0)

circles=cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 3, 4.0)

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

This is the code I used to try and detect all circles in the given image: Image of circles这是我用来尝试检测给定图像中所有圆圈的代码:圆圈图像

Which parameter and how should I manipulate to detect the smaller as well as the larger circles?我应该使用哪个参数以及如何操作来检测较小和较大的圆圈? I had tried minRadius and maxRadius to possible values as per both circles, but it didn't work, so I let it reset to default.我已经根据两个圆圈尝试了 minRadius 和 maxRadius 的可能值,但它不起作用,所以我让它重置为默认值。

It feels like the canny edge detector is actually detecting the smaller circles with the default arguments(100,100), as attached canny detection感觉就像 canny 边缘检测器实际上是使用默认参数(100,100)检测较小的圆圈,作为附加的 canny 检测

I took the Green plane of the given image: Original Image我拿了给定图像的绿色平面:原始图像

I would recommend you to first read the docs .我建议您先阅读文档

If you just change the param1 and param2 which are thresholds for the Canny edge detector, the cv2.HoughCircles works fine.如果您只是更改作为 Canny 边缘检测器阈值的param1param2 ,则cv2.HoughCircles可以正常工作。

Here the code:这里的代码:

import cv2
import numpy as np

image = cv2.imread('circles.png')

image = cv2.GaussianBlur(image, (5, 5), 0)

orig_image = np.copy(image)
gray = image[:, :, 1]
output = gray.copy()

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

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

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM