2017-03-03 131 views
0

我想從給定圖像中檢測到一個圓。但它只是不按我想要的方式工作。我實現了一個圓檢測算法,該算法可以在一些圖像上使用圓圈,但不是我想要的圖像。我調整了參數,但無法讓它工作。未找到圓圈

import cv2 
import numpy as np 

# load the image, clone it for output, and then convert it to grayscale 
image = cv2.imread("damn-circle.png") 
output = image.copy() 
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 

# detect circles in the image 
blur = cv2.GaussianBlur(gray,(5,5),0) 
circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT, 2, 120) 
cv2.imshow("output", np.hstack([blur])) 
cv2.waitKey(0) 
print circles 

# ensure at least some circles were found 
if circles is not None: 
    # convert the (x, y) coordinates and radius of the circles 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, then draw a rectangle 
     # corresponding to the center of the circle 
     cv2.circle(output, (x, y), r, (0, 255, 0), 4) 
     cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1) 

    # show the output image 
    cv2.imshow("output", np.hstack([output])) 
    cv2.waitKey(0) 

Normal circle The circle I want to detect

回答

0

你的代碼幾乎是完美的。只是方法CV_HOUGH_GRADIENT位於一個包中,cv(至少對於opencv版本:2.4.13)。我改變了這一行提到的包,它運作良好。如果你仍然沒有在這個簡單的圖像上得到正確的結果,你必須爲OpenCV和NumPy設置特定的版本。改變你的線是這樣的: circles = cv2.HoughCircles(blur, cv2.cv.CV_HOUGH_GRADIENT, 2, 120) 你應該得到一個不錯的結果。至少我剛剛做到了。 image with found Hough circle shown

編輯: 啊,我不明白這個問題是關於哪個圖像。我改變了幾個項目的參數,特別是Canny檢測器參數和最小/最大半徑以及累加器分辨率。我認爲這些參數會找到你想要的: circles = cv2.HoughCircles(blur, method = cv2.cv.CV_HOUGH_GRADIENT, minDist = 90 , dp = 1, param1 = 3, param2 = 12 , minRadius = 30, maxRadius = 50) 我的發現圖像現在看起來像這樣:another image with found circle

+0

你可能誤解了我。我正在使用OpenCV 3.0。包'cv2.cv'移動到'cv2'。我也可以很好地檢測出您可以使用的圖像,但請使用下面的圖像嘗試相同的代碼。它不工作:) – Sylnois

+0

我編輯了我的答案,以覆蓋你想要找到圈子的圖像。 –