閾值處理是指將影象內高於一定值或者低於一定值的畫素點進行處理
函式形式為:
retval ,dst = cv2.thresshold( src , thresh , maxval , type )retval代表返回的閾值
dst代表閾值分割結果影象,與原始影象有相同的大小和型別
src代表要進行分割的影象,可以是多通道的
thresh代表要設定的閾值
maxval代表當type為thresh_binary或者thresh_binary_inv型別時,需要設定的最大值
type代表閾值分割的型別
具體型別如下
二值化閾值處理(cv2.thresh_binary)
處理之後影象為只有兩個值的二值影象
對於8位灰度影象,將超過閾值thresh的值處理為最大值255,低於閾值的處理位0
1import
cv22
import
numpy as np
3 img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
4 t,rst=cv2.threshold(img,127,255,cv2.thresh_binary)
5print("
img=\n
",img)
6print("t="
,t)7
print("
rst=\n
",rst)
img=[[ 98 151 50 196 238]反二值化閾值處理(cv2.thresh_binary_inv)[ 45 64 225 227 204]
[ 45 19 46 233 82]
[122 103 64 182 218]]
t= 127.0rst=[[ 0 255 0 255 255]
[ 0 0 255 255 255]
[ 0 0 0 2550]
[ 0 0 0 255 255]]
處理後的影象也是只有兩個值的二值影象,
將灰度值大於閾值thresh的畫素點,將其值處理為0,低於的處理為255
1import
cv22
import
numpy as np
3 img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
4 t,rst=cv2.threshold(img,127,255,cv2.thresh_binary_inv)
5print("
img=\n
",img)
6print("t="
,t)7
print("
rst=\n
",rst)
img=[[161 182 120 192 159]截斷閾值化處理(cv2.thresh_trunc)[ 64 197 108 242 182]
[237 203 8 206 67]
[ 31 7 190 226 22]]
t= 127.0rst=[[ 0 0 2550 0]
[255 0 2550 0]
[ 0 0 255 0 255]
[255 255 0 0 255]]
對於畫素值大於閾值thresh的值將其處理為閾值的值,小於閾值的值保持不變
importcv2import
numpy as np
img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
t,rst=cv2.threshold(img,127,255,cv2.thresh_trunc)
print("
img=\n
",img)
print("t="
,t)print("
rst=\n
",rst)
img=[[ 31 121 210 126 117]超閾值零處理(cv2.thresh_tozero_inv)[ 17 144 78 31 193]
[ 91 143 27 58 103]
[203 216 151 176 30]]
t= 127.0rst=[[ 31 121 127 126 117]
[ 17 127 78 31 127]
[ 91 127 27 58 103]
[127 127 127 127 30]]
超閾值零處理會將影象中大於閾值的畫素點的值處理為0,小於或等於該閾值的畫素點的值保持不變
1import
cv22
import
numpy as np
3 img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
4 t,rst=cv2.threshold(img,127,255,cv2.thresh_tozero_inv)
5print("
img=\n
",img)
6print("t="
,t)7
print("
rst=\n
",rst)
img=[[209 180 150 127 21]低閾值值零處理(cv2.thresh_tozero)[ 11 227 7 223 211]
[218 84 90 32 61]
[101 129 240 36 176]]
t= 127.0rst=[[ 0 0 0 127 21]
[ 11 0 70 0]
[ 0 84 90 32 61]
[101 0 0 36 0]]
低閾值處理會將影象中小於或等於閾值的畫素點的值處理為0,大於閾值的畫素點的值保持不變
1import
cv22
import
numpy as np
3 img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
4 t,rst=cv2.threshold(img,127,255,cv2.thresh_tozero)
5print("
img=\n
",img)
6print("t="
,t)7
print("
rst=\n
",rst)
img=[[249 40 83 55 210][195 144 246 163 230]
[ 41 124 16 209 5]
[ 48 177 190 118 75]]
t= 127.0rst=[[249 0 0 0 210]
[195 144 246 163 230]
[ 0 0 0 2090]
[ 0 177 190 0 0]]
OpenCV 閾值處理 二 自適應閾值
因此在同一副影象上的不同區域採用的是不同的閾值,從而使我們能在亮度不同的情況下得到更好的結果。自適應閾值函式 dst cv.adaptivethreshold src,maxvalue,adaptivemethod,thresholdtype,blocksize,c dst 引數 src 8位單通道...
OpenCV學習筆記之閾值操作
簡單來說,閾值是影象分割的標尺,這個標尺根據閾值型別來確定。閾值二值化 threshold binary thresh binary,規定某個閾值,當畫素值大於這個閾值的時候為255,當畫素值小於這個閾值的時候為0。閾值反二值化 threshold binary inverted thresh bi...
opencv 學習之 閾值化 2 自適應閾值
自適應閾值化函式 void cvadaptivethreshold const cvarr src,cvarr dst,double max value,int adaptive method cv adaptive thresh mean c,int threshold type cv thres...