2017-03-07 114 views
2

如何改善以下圓檢測碼的性能圓檢測用的OpenCV

from matplotlib.pyplot import imshow, scatter, show 
import cv2 

image = cv2.imread('points.png', 0) 
_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY) 
image = cv2.Canny(image, 1, 1) 
imshow(image, cmap='gray') 

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 2, 32) 
x = circles[0, :, 0] 
y = circles[0, :, 1] 

scatter(x, y) 
show() 

與下面的源圖像:

enter image description here

我試圖調整的參數HoughCircles函數,但它們會導致太多的誤報或太多的漏報。特別是,我有虛假圈麻煩兩個斑點之間的差距正在檢測:

enter image description here

+0

考慮接受答案,如果你認爲它有幫助。 – m3h0w

回答

7

@Carlos,我真的不霍夫圓的大風扇,一個像你」的情況已經描述過。說實話,我覺得這個算法非常不直觀。我會建議你的情況是使用findContour()功能,然後計算質量中心。這樣說,我調整了霍夫的參數有點合理的結果。在Canny之前,我還使用了一種不同的預處理方法,因爲我沒有看到在任何其他情況下該閾值將如何工作。

霍夫方法: enter image description here

發現質量中心: enter image description here

,代碼:

from matplotlib.pyplot import imshow, scatter, show, savefig 
import cv2 

image = cv2.imread('circles.png', 0) 
#_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY) 
image = cv2.GaussianBlur(image.copy(), (27, 27), 0) 
image = cv2.Canny(image, 0, 130) 
cv2.imshow("canny", image) 
cv2.waitKey(0) 
imshow(image, cmap='gray') 

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 22, minDist=1, maxRadius=50) 
x = circles[0, :, 0] 
y = circles[0, :, 1] 

scatter(x, y) 
show() 
savefig('result1.png') 
cv2.waitKey(0) 

_, cnts, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL, 
    cv2.CHAIN_APPROX_SIMPLE) 

# loop over the contours 
for c in cnts: 
    # compute the center of the contour 
    M = cv2.moments(c) 
    cX = int(M["m10"]/M["m00"]) 
    cY = int(M["m01"]/M["m00"]) 

    #draw the contour and center of the shape on the image 
    cv2.drawContours(image, [c], -1, (125, 125, 125), 2) 
    cv2.circle(image, (cX, cY), 3, (255, 255, 255), -1) 

cv2.imshow("Image", image) 
cv2.imwrite("result2.png", image) 
cv2.waitKey(0) 

兩種方法都需要一些微調,但我希望給你更多的東西來與...合作。

來源:this answer當然也是唯一的pyimagesearch

+1

當有許多圓圈緊靠在一起時,由於不同圓圈的部分組合在一起以提供新圓圈的可能性,所以Hough變換變得不可靠。正如@ m3h0w所示,您可以調整Hough參數並使用輪廓和時刻等更高級的功能。關鍵是要麼在Hough變換之前對圖像進行預處理,要麼在變換之後對結果進行後處理,或者兩者兼而有之。 – Totoro

+0

@ m3h0w阿哈另一箇中風。希望有一個選項*最喜歡*的答案。 –