閾值處理
定義
閾值處理即圖像二值化沮焕。是圖像分割的一種最簡單的方法蔼两。二值化可以把灰度圖像轉(zhuǎn)換成二值圖像色徘。把大于某個(gè)臨界灰度值的像素灰度設(shè)為灰度極大值各薇,把小于這個(gè)值的像素灰度設(shè)為灰度極小值业岁,從而實(shí)現(xiàn)二值化鳞仙。
API
ret, dst = cv2.threshold(src, thresh, maxval, type)
src: 輸入圖,只能輸入單通道圖像笔时,通常來說為灰度圖
dst: 輸出圖
thresh: 閾值
maxval: 當(dāng)像素值超過了閾值(或者小于閾值棍好,根據(jù)type來決定),所賦予的值
-
type:二值化操作的類型允耿,包含以下5種類型:
- cv2.THRESH_BINARY 超過閾值部分取maxval(最大值)借笙,否則取0
- cv2.THRESH_BINARY_INV THRESH_BINARY的反轉(zhuǎn)
- cv2.THRESH_TRUNC 大于閾值部分設(shè)為閾值,否則不變
- cv2.THRESH_TOZERO 大于閾值部分不改變较锡,否則設(shè)為0
- cv2.THRESH_TOZERO_INV THRESH_TOZERO的反轉(zhuǎn)
例子
import cv2 #opencv讀取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
%matplotlib inline
img=cv2.imread('cat.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_gray.shape
(414, 500)
ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO_INV)
titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
自適應(yīng)閾值
定義
上述使用的是全局閾值业稼,整幅圖像采用同一個(gè)數(shù)作為閾值。這種方法并不適應(yīng)與所有情況蚂蕴,尤其是當(dāng)同一幅圖像上的不同部分的具有不同亮度時(shí)低散。這種情況下我們需要采用自適應(yīng)閾值。此時(shí)的閾值是根據(jù)圖像上的每一個(gè)小區(qū)域計(jì)算與其對(duì)應(yīng)的閾值骡楼。因此熔号,在同一幅圖像上的不同區(qū)域采用的不同的閾值,從而使我們能在亮度不同的情況下得到更好的結(jié)果鸟整。
API
cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C)
src:原圖引镊,即輸入圖像,是一個(gè)8位單通道的圖像篮条;
maxValue:分配給滿足條件的像素的非零值弟头;
-
adaptiveMethod:自適應(yīng)閾值的方法,通常有以下幾種方法涉茧;
(1)ADAPTIVE_THRESH_MEAN_C赴恨,閾值T(x,y)是(x,y)減去C的Blocksize×Blocksize鄰域的平均值。
(2)ADAPTIVE_THRESH_GAUSSIAN_C 伴栓,閾值T(x嘱支,y)是(x蚓胸,y)減去C的Blocksize×Blocksize鄰域的加權(quán)和(與高斯相關(guān))挣饥,默認(rèn)sigma(標(biāo)準(zhǔn)差)用于指定的Blocksize除师;具體的情況可以參見getGaussianKernel函數(shù);
-
thresholdType:閾值的類型必須是以下兩種類型扔枫,
(1)THRESH_BINARY汛聚,正向二值化
(2)THRESH_BINARY_INV ,反向二值化
blockSize:像素鄰域的大小短荐,用來計(jì)算像素的閾值倚舀,blockSize必須為奇數(shù),例如忍宋,3痕貌,5,7等等糠排;
C:從平均數(shù)或加權(quán)平均數(shù)減去常量舵稠。通常,它是正的入宦,但也可能是零或負(fù)數(shù)哺徊。
例子
thresh1 = cv2.adaptiveThreshold(img_gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,5,2)
thresh2 = cv2.adaptiveThreshold(img_gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,5,2)
thresh3 = cv2.adaptiveThreshold(img_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,2)
thresh4 = cv2.adaptiveThreshold(img_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,5,2)
titles = ['Original Image', 'THRESH_MEAN', 'THRESH_MEAN_INV', 'THRESH_GAUSSIAN', 'THRESH_GAUSSIAN_INV']
images = [img, thresh1, thresh2, thresh3, thresh4]
for i in range(5):
plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()