2017-09-06 78 views
1

The documentationTHRESH_BINARY說:opencv閾值THRESH_BINARY對彩色圖像做什麼?

dst(x,y) = maxval if src(x,y) > thresh else 0

這對我來說並不意味着這不會對彩色圖像的工作。即使應用於彩色圖像,我也期望獲得雙色輸出,但輸出是多色的。爲什麼?當像素x,y分配的可能值僅爲maxval0時,該怎麼辦?

實施例:

from sys import argv 
import cv2 
import numpy as np 

img = cv2.imread(argv[1]) 

ret, threshold = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY) 

cv2.imshow('threshold', threshold) 
cv2.imshow('ori', img) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

enter image description here

回答

3

閾值應用到每個色彩通道,分別。如果tahn閾值較小,則顏色通道設置爲0,否則爲maxval。通道獨立處理,這就是爲什麼結果是具有多種顏色的彩色圖像。你可以得到的顏色是:(0,0,0),(255,0,0),(0,255,0),(255,255,0),(0,0,255),(255,0,255)和(255,255,255) 。

+0

哦,我明白了。謝謝! –

2

假設您的像素來自3通道RGB圖像,其值爲rgb(66, 134, 244)。現在假設你給thresh135。你認爲會發生什麼?

r = 66 
g = 134 
b = 244 

if(r > thresh) r = 255 else r = 0; // we have r = 0 
if(g > thresh) g = 255 else g = 0; // we have g = 0 
if(b > thresh) b = 255 else b = 0; // we have b = 255 

新的像素值是rgb(0, 0, 255)。由於您的圖像是RGB彩色圖像,因此現在像素顏色爲BLUE而不是WHITE