opencv学习——自适应阈值二值化

来源:互联网 发布:入门程序员必看书籍 编辑:程序博客网 时间:2024/05/23 10:39
import cv2import numpy as npimport matplotlib.pylab as pltimport scipy.misc as misc'''Adaptive Method - It decides how thresholding value is calculated.    cv2.ADAPTIVE_THRESH_MEAN_C : threshold value is the mean of neighbourhood area.    cv2.ADAPTIVE_THRESH_GAUSSIAN_C : threshold value is the weighted sum of neighbourhood values                                where weights are a gaussian window.Block Size - It decides the size of neighbourhood area.C - It is just a constant which is subtracted from the mean or weighted mean calculated.'''img = cv2.imread('images/32.jpg',0)# 中值滤波img = cv2.medianBlur(img,5)ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)#11 为 Block size, 2 为 C 值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 range(4):    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')    plt.title(titles[i])    plt.xticks([]),plt.yticks([])plt.show()
原创粉丝点击