如何使用 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)

这是我用来尝试检测给定图像中所有圆圈的代码: Image of circles

我应该操纵哪个参数以及如何操纵来检测较小和较大的圆圈?我已经根据两个圆尝试了 minRadius 和 maxRadius 的可能值,但它没有用,所以我让它重置为默认值。

感觉就像 canny 边缘检测器实际上正在检测具有默认参数 (100,100) 的较小圆圈,如附件 canny detection

我拍了给定图像的绿色平面:Original Image

我建议你先 read the docs

如果您只是更改 param1param2 这两个 Canny 边缘检测器的阈值,cv2.HoughCircles 工作正常。

这里是代码:

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)