ret, dst = cv2.threshold(src, thresh, maxval, type)
官方文檔的示例代碼:
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()
結果為:
自適應閾值二值化函數(shù)根據(jù)圖片一小塊區(qū)域的值來計算對應區(qū)域的閾值,從而得到也許更為合適的圖片。
dst = cv2.adaptiveThreshold(src, maxval, thresh_type, type, 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是一種基于直方圖的二值化方法,它需要和threshold函數(shù)配合使用。
Otsu過程:
1. 計算圖像直方圖;
2. 設定一閾值,把直方圖強度大于閾值的像素分成一組,把小于閾值的像素分成另外一組;
3. 分別計算兩組內的偏移數(shù),并把偏移數(shù)相加;
4. 把0~255依照順序多為閾值,重復1-3的步驟,直到得到最小偏移數(shù),其所對應的值即為結果閾值。
官方文檔的示例代碼:
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()
結果為:
參考文獻:http://docs.opencv.org/3.2.0/d7/d4d/tutorial_py_thresholding.html
到此這篇關于opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實現(xiàn)的文章就介紹到這了,更多相關opencv threshold、adaptiveThreshold、Otsu內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
標簽:懷化 昆明 西寧 梅州 文山 石家莊 浙江 錫林郭勒盟
巨人網(wǎng)絡通訊聲明:本文標題《opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實現(xiàn)》,本文關鍵詞 opencv,函數(shù),threshold,adaptiveThreshold,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。