OpenCV固定閾值二值化
threshold:固定閾值二值化,
ret, dst = cv2.threshold(src, thresh, maxval, type)
1
src: 輸入圖,只能輸入單通道影象,通常來說為灰度圖
dst: 輸出圖
thresh: 閾值
maxval: 當畫素值超過了閾值(或者小於閾值,根據type來決定),所賦予的值
type:二值化操作的型別,包含以下5種類型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV
官方文件的示例程式碼:
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('gradient.png',0) ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY) ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV) ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC) ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO) ret,thresh5 = cv2.threshold(img,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 xrange(6): plt.subplot(2,3,i+1),plt.imshow(images[i],'gray') plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()
結果為:
adaptiveThreshold:自適應閾值二值化
自適應閾值二值化函式根據圖片一小塊區域的值來計算對應區域的閾值,從而得到也許更為合適的圖片。
dst = cv2.adaptiveThreshold(src, maxval, thresh_type, type, Block Size, C)
1
src: 輸入圖,只能輸入單通道影象,通常來說為灰度圖
dst: 輸出圖
maxval: 當畫素值超過了閾值(或者小於閾值,根據type來決定),所賦予的值
thresh_type: 閾值的計算方法,包含以下2種類型:cv2.ADAPTIVE_THRESH_MEAN_C; cv2.ADAPTIVE_THRESH_GAUSSIAN_C.
type:二值化操作的型別,與固定閾值函式相同,包含以下5種類型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV.
Block Size: 圖片中分塊的大小
C :閾值計算方法中的常數項
官方文件的示例程式碼:
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('sudoku.png',0) img = cv2.medianBlur(img,5) ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY) th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\ cv2.THRESH_BINARY,11,2) th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,11,2) titles = ['Original Image', 'Global Thresholding (v = 127)', 'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding'] images = [img, th1, th2, th3] for i in xrange(4): plt.subplot(2,2,i+1),plt.imshow(images[i],'gray') plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()
結果為:
Otsu’s Binarization: 基於直方圖的二值化
Otsu’s Binarization是一種基於直方圖的二值化方法,它需要和threshold函式配合使用。
Otsu過程:
1. 計算影象直方圖;
2. 設定一閾值,把直方圖強度大於閾值的畫素分成一組,把小於閾值的畫素分成另外一組;
3. 分別計算兩組內的偏移數,並把偏移數相加;
4. 把0~255依照順序多為閾值,重複1-3的步驟,直到得到最小偏移數,其所對應的值即為結果閾值。
官方文件的示例程式碼:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('noisy2.png',0)
# global thresholding
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# plot all the images and their histograms
images = [img, 0, th1,
img, 0, th2,
blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
'Original Noisy Image','Histogram',"Otsu's Thresholding",
'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in xrange(3):
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()