(一)基於固定閾值的分割(全域性)
(1)基本定義:
固定閾值的分割是 最為簡單的一種影象分割方法,即選取乙個全域性閾值,然後就把整幅影象分成了非黑即白的二值影象。
(2)關鍵函式:
ret, th = cv2.threshold(img, 127, 255, cv2.thresh_binary)
ret : return value縮寫,代表當前的閾值,暫時不用理會(用於otsu)
th : 切割後的影象
cv2.threshold()函式有四個引數:
引數1:要處理的原圖,一般是灰度圖
引數2:設定的閾值
引數3:最大閾值,一般為255
引數4:閾值的方式,主要有5種:
import cv2
import matplotlib.pyplot as plt
# 灰度圖讀入
img = cv2.imread(
'jiao.png',0
)# 閾值分割
ret, th = cv2.threshold(img,
131,
255, cv2.thresh_binary)
# cv2.imshow('thresh', th)
# cv2.waitkey(0)
# 應用5種不同的閾值方法
ret, th1 = cv2.threshold(img,
131,
255, cv2.thresh_binary)
ret, th2 = cv2.threshold(img,
131,
255, cv2.thresh_binary_inv)
ret, th3 = cv2.threshold(img,
131,
255, cv2.thresh_trunc)
ret, th4 = cv2.threshold(img,
131,
255, cv2.thresh_tozero)
ret, th5 = cv2.threshold(img,
131,
255, cv2.thresh_tozero_inv)
titles =
['original'
,'binary'
,'binary_inv'
,'trunc'
,'tozero'
,'tozero_inv'
]images =
[img, th1, th2, th3, th4, th5]
# 使用matplotlib顯示
for i in
range(6
):plt.subplot(2,
3, i +1)
# plt.subplot(3,2,4) : 分成3行2列,共6個繪圖區域,在第4個區域繪圖。排序為行優先。也可 plt.subplot(324),將逗號省略。
plt.imshow(images[i]
,'gray'
) plt.title(titles[i]
, fontsize=8)
# plt.title(): 對圖形整體增加文字標籤
plt.xticks(
), plt.yticks(
)# 隱藏座標軸
第乙個圖為灰度圖,以下五幅圖對應著五種不同的模式!!
注意:
gaussianblur函式:
函式作用:對影象進行高斯濾波,去除雜訊,影象預處理一般都用
語法:gaussianblur(src,ksize,sigmax [,dst [,sigmay [,bordertype]]])-> dst
src輸入影象;
dst輸出影象
ksize高斯核心大小。 ksize.width和ksize.height可以不同,但它們都必須為正數和奇數,也可以為零,然後根據sigma計算得出。
sigmax x方向上的高斯核標準偏差。
sigmay y方向上的高斯核標準差;如果sigmay為零,則將其設定為等於sigmax;如果兩個sigmas為零,則分別從ksize.width和ksize.height計算得出;為了完全控制結果,而不管將來可能對所有這些語義進行的修改,建議指定所有ksize,sigmax和sigmay。
基於邊緣改進的全域性閾值 opencv
99.97 時候的二值圖 為了視覺化,灰度值範圍設定為0 255 90 的二值圖 核心演算法 void edge threshold mat src uchar d data dst.data for int j 0 j dst.rows j long sum 0,amount 0 double p...
opencv 閾值操作
閾值分割5種方法 尋找閾值2種方法 double cv threshold inputarray src,outputarray dst,double thresh,double maxval,int type 引數 含義src 輸入,要求是單通道影象 thresh 門限值maxval 超過門限的畫...
OpenCV 閾值處理 二 自適應閾值
因此在同一副影象上的不同區域採用的是不同的閾值,從而使我們能在亮度不同的情況下得到更好的結果。自適應閾值函式 dst cv.adaptivethreshold src,maxvalue,adaptivemethod,thresholdtype,blocksize,c dst 引數 src 8位單通道...