2015-06-30 200 views
2

我試圖弄清楚Hough Circle,然後將其合併到我的主代碼中,用於跟蹤程序我正在嘗試寫入,但我似乎無法獲取除None之外的任何內容圓圈。我使用孟加拉國旗作爲我的形象,因爲它很簡單,很容易被發現。這裏是我的代碼:Python OpenCV Hough Circles返回None

import numpy as np 
import cv2 


img = cv2.imread('Capture.PNG') 

grayput = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

circles = cv2.HoughCircles(grayput, cv2.cv.CV_HOUGH_GRADIENT, 1, 20, param1 =50, param2 =10, minRadius=10, maxRadius=40) 
print (circles) 

    # need circles 
if circles is not None: 
    # convert the coord. to integers 
    circles = np.round(circles[0, :]).astype("int") 

    # loop over the (x, y) coordinates and radius of the circles 
    for (x, y, r) in circles: 
     # draw the circle in the output image 
     cv2.circle(img, (x, y), r, (0, 0, 0), 4) 


cv2.imwrite("image.PNG",img) 

回答

1

下面的代碼會給你非無圈:

import numpy as np 
import cv2 

img = cv2.imread("../images/opencv_logo.png", 0) 
img = cv2.medianBlur(img,5) 
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR) 
cv2.imshow("grayscale", cimg) 
cv2.waitKey(0) 

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

事實上,輸出是:

[[[ 45.5   133.5   16.50757408] 
    [ 97.5   45.5   16.80773544] 
    [ 147.5   133.5   16.32482719]]] 


注:摘錄使用以下作爲其輸入圖像:

enter image description here