簡體   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)

這是我用來嘗試檢測給定圖像中所有圓圈的代碼:圓圈圖像

我應該使用哪個參數以及如何操作來檢測較小和較大的圓圈? 我已經根據兩個圓圈嘗試了 minRadius 和 maxRadius 的可能值,但它不起作用,所以我讓它重置為默認值。

感覺就像 canny 邊緣檢測器實際上是使用默認參數(100,100)檢測較小的圓圈,作為附加的 canny 檢測

我拿了給定圖像的綠色平面:原始圖像

我建議您先閱讀文檔

如果您只是更改作為 Canny 邊緣檢測器閾值的param1param2 ,則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)

結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM