opencv學習筆記 閾值分割

2021-10-06 17:32:46 字數 2671 閱讀 9964

先選定乙個特定的閾值量,比如:127

新的閾值產生規則為:

dst

(x,y

)=maxval \quad if \quad src(x,y)>thresh \\ 0,otherwise \end

dst(x,

y)= 0 \quad if \quad src(x,y)>thresh \\ maxval,otherwise \end

dst(x,

y)= threshold \quad if \quad src(x,y)>thresh \\ src(x,y),otherwise \end

dst(x,

y)= 0 \quad if \quad src(x,y)>thresh \\ src(x,y),otherwise \end

dst(x,

y)= src(x,y) \quad if \quad src(x,y)>thresh \\ 0,otherwise \end

dst(x,

y)={

src(

x,y)

ifsr

c(x,

y)>th

resh

0,ot

herw

ise​

retval, dst = cv2.threshold(src, thresh, maxval, type)

# 二進位制閾值化

cv2.threshold(image,

127,

255, cv2.thresh_binary)

# 反二進位制閾值化

cv2.threshold(image,

127,

255, cv2.thresh_binary_inv)

# 截斷閾值化

cv2.threshold(image,

127,

255, cv2.thresh_trunc)

# 反閾值化為0

cv2.threshold(image,

127,

255, cv2.thresh_tozero_inv)

# 閾值化為0

# 二進位制閾值化

cv2.imshow(

'binary'

, binary)

retval, binary_inv = cv2.threshold(gray,

128,

255, cv2.thresh_binary_inv)

# 反二進位制閾值化

cv2.imshow(

'binary_inv'

, binary_inv)

retval, trunc = cv2.threshold(gray,

128,

255, cv2.thresh_trunc)

# 截斷閾值化

cv2.imshow(

'trunc'

, trunc)

retval, tozero_inv = cv2.threshold(gray,

128,

255, cv2.thresh_tozero_inv)

# 反閾值化為0

cv2.imshow(

'zero'

, tozero_inv)

retval, tozero = cv2.threshold(gray,

128,

255, cv2.thresh_tozero)

# 閾值化為0

cv2.imshow(

'tozero'

, tozero)

cv2.waitkey(0)

cv2.destroyallwindows(

)

執行結果如下:

OpenCV學習筆記 4 閾值分割

threshold inputarray src,outputarray dst,double thresh,double maxval,int type src 輸入矩陣,資料型別為cv 8u或者cv 32f dst 輸出矩陣 thresh 閾值 maxval 影象二值化時,一般為255 type...

opencv學習筆記十一 閾值操作

閾值操作型別有 thresh binary 0 二值化,大於閾值的為255,小於閾值的為0 thresh binary inv 1 反二值化,大於閾值的為0,小於閾值的為255 thresh trunc 2 截斷法,大於閾值的取閾值,小於閾值的不變 thresh tozero 3 大於閾值的不變,小...

OpenCV學習筆記之閾值操作

簡單來說,閾值是影象分割的標尺,這個標尺根據閾值型別來確定。閾值二值化 threshold binary thresh binary,規定某個閾值,當畫素值大於這個閾值的時候為255,當畫素值小於這個閾值的時候為0。閾值反二值化 threshold binary inverted thresh bi...