2017-05-08 539 views
0

我試圖用houghcircles()檢測此裁剪圖像中的小圓圈。我試圖改變它的參數,但是當我增加參數2以上50 maxRadius也得到錯誤當其值小於100。現在,它運行,但性能差它得到錯誤 這是原始圖像: enter image description here使用houghCircles檢測小圓圈(OpenCV)

而且這是結果圖像: enter image description here

這是我的代碼:

from imutils.perspective import four_point_transform 
from imutils import contours 
import numpy as np 
import argparse 
import imutils 
import cv2 

im = cv2.imread('crop.png') 
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 
ret,thresh = cv2.threshold(imgray, 200, 255,cv2.THRESH_BINARY) 
cimg = cv2.cvtColor(thresh,cv2.COLOR_GRAY2BGR) 

c = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, 0.5, 41, param1=70, 
param2=30, minRadius=10,maxRadius=175) 
c = np.uint16(np.around(c)) 

for i in c[0,:]: 
    # draw the outer circle 
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2) 
    # draw the center of the circle 
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) 

cv2.namedWindow('img',cv2.WINDOW_NORMAL) 
cv2.resizeWindow('img', 800,800) 
cv2.imshow('img',cimg) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

請,我應該如何改變參數?

回答

2

如果您嘗試改變參數而不理解它們的作用,則需要很長時間才能解決此問題。

這說明從here

minDist: Minimum distance between the center (x, y) coordinates of detected circles. If the minDist is too small, multiple circles in the same neighborhood as the original may be (falsely) detected. If the minDist is too large, then some circles may not be detected at all.

param1: Gradient value used to handle edge detection in the Yuen et al. method.

param2: Accumulator threshold value for the cv2.HOUGH_GRADIENT method. The smaller the threshold is, the more circles will be detected (including false circles). The larger the threshold is, the more circles will potentially be returned.

minRadius: Minimum size of the radius (in pixels).

maxRadius: Maximum size of the radius (in pixels).

你可以清楚地看到,在圖像的圓圈有固定的半徑和具有最小距離隔開。如果你設置這兩個,你可以改善你的結果。所以閱讀文檔很重要。

而關於你的問題,如果你有特定的必要性,使用Houghcircles繼續前進並微調它。你可以做的改進之處是,使用gaussianblur進行預處理,使用adaptivethreshold而不是閾值。

如果沒有必要使用Hough圈,我建議您改用輪廓。因爲它更強大,並能很好地適應不同的圖像。這是我使用輪廓的結果。圓圈顯得更小,因爲我已經使用侵蝕進行了6次迭代,並且僅對3次迭代進行了擴張。

finding circles using contours

這裏是我使用的代碼。

import numpy as np 
import cv2 

image_color= cv2.imread("Ye3gs.png") 
image_ori = cv2.cvtColor(image_color,cv2.COLOR_BGR2GRAY) 

lower_bound = np.array([0,0,10]) 
upper_bound = np.array([255,255,195]) 

image = image_color 

mask = cv2.inRange(image_color, lower_bound, upper_bound) 

# mask = cv2.adaptiveThreshold(image_ori,255,cv2.ADAPTIVE_THRESH_MEAN_C,\ 
#    cv2.THRESH_BINARY_INV,33,2) 

kernel = np.ones((3, 3), np.uint8) 

#Use erosion and dilation combination to eliminate false positives. 
#In this case the text Q0X could be identified as circles but it is not. 
mask = cv2.erode(mask, kernel, iterations=6) 
mask = cv2.dilate(mask, kernel, iterations=3) 

closing = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) 

contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 
     cv2.CHAIN_APPROX_SIMPLE)[0] 
contours.sort(key=lambda x:cv2.boundingRect(x)[0]) 

array = [] 
ii = 1 
print len(contours) 
for c in contours: 
    (x,y),r = cv2.minEnclosingCircle(c) 
    center = (int(x),int(y)) 
    r = int(r) 
    if r >= 6 and r<=10: 
     cv2.circle(image,center,r,(0,255,0),2) 
     array.append(center) 

cv2.imshow("preprocessed", image_color) 
cv2.waitKey(0) 

希望這有助於:)

+0

謝謝你的幫助,但它給了我一個錯誤:'contours.sort(鍵=拉姆達X:cv2.boundingRect(X)[0]) 類型錯誤:'關鍵」是無效的關鍵字參數此功能 ' –

+1

非常感謝它爲我工作,當我加入這個功能'高清get_contour_precedence(輪廓的cols): tolerance_factor = 10 起源= cv2.boundingRect(輪廓) 回報(( [1] // tolerance_factor)* tolerance_factor)* cols + origin [0]' –

+0

我建議使用輪廓+ minEnclosingC然後將圓形邊界/區域與檢測到的輪廓進行比較,以濾除非圓形輪廓 – Micka