2017-01-16 88 views
1
img = cv2.imread('/home/user/Documents/workspace/ImageProcessing/img.JPG'); 
image = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 

#red, blue, yellow, and gray 
boundaries = [ 
([17, 15, 100], [50, 56, 200]), 
([86, 31, 4], [220, 88, 50]), 
([25, 146, 190], [62, 174, 250]), 
([103, 86, 65], [145, 133, 128])] 


for i, (lower, upper) in enumerate(boundaries): 

    lower = np.array(lower, dtype="uint8") 
    upper = np.array(upper, dtype="uint8") 

    mask = cv2.inRange(image, lower, upper) 
    output = cv2.bitwise_and(image, image, mask=mask) 

    cv2.imwrite(str(i) + 'image.jpg', output) 

我想從圖像中分離出紅色,藍色,黃色和灰色的顏色(單獨)。 它到目前爲止,但「靈敏度」是低的方式。 該算法缺少一些較小的顏色斑點。 有沒有辦法校準這個? 謝謝!Python OpenCV - cv.inRange()「sensitivity」?

編輯: 輸入圖像 input

輸出
output1 output2 output3 output4

+0

如果您還可以添加輸入/輸出圖像,這將有助於我們更好地理解問題。 – ilke444

回答

3

inRange函數不具有內置的靈敏度。它只比較這些值。 inRange(x,10,20)只會給你{10,11,...,20}。

解決這個問題的方法之一是引入您自己的靈敏度度量。

s = 5 # for example sensitivity=5/256 color values in range [0,255] 

for i, (lower, upper) in enumerate(boundaries): 

    lower = np.array([color-s if color-s>-1 else 0 for color in lower], dtype="uint8") 
    upper = np.array([color+s if color+s<256 else 255 for color in upper], dtype="uint8") 

    mask = cv2.inRange(image, lower, upper) 
    output = cv2.bitwise_and(image, image, mask=mask) 

    cv2.imwrite(str(i) + 'image.jpg', output) 

或者您可以事先平滑圖像以擺脫這種噪點像素。這將使像素值彼此更接近,以便超出邊界的像素值可能會更接近該範圍。

+0

謝謝你的回答! – cmplx96

+0

但我試過一切從靈敏度0.01到200,它沒有得到任何更好的 – cmplx96

+0

所有的東西在靈敏度= 20是黑色後 – cmplx96