简单阈值
cv2.threshold()函数
th,res = cv2.threshold(img,thresh,maxVal,type) img:原图像 thresh:阈值 maxVal:最大值,一般为255 type: cv2.THRESH_BINARY:超过阈值的像素设置为maxVal,不超过的设置为0 cv2.THRESH_BINARY_INV:不超过阈值的设置为maxVal,超过的设置为0 cv2.THRESH_TOZERO:低于阈值的设置为0 cv2.THRESH_TOZERO_INV:低于阈值的设置为maxVal cv2.THRESH_TRUNC:超过阈值的设置为threshold th:和thresh一样 res:处理结果 import cv2 from matplotlib import pyplot as plt img=cv2.imread('lhu.jpg',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 range(6): plt.subplot(2,3,i+1),plt.imshow(images[i],'gray') plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()自适应阈值
• Adaptive Method- 指定计算阈值的方法。 – cv2.ADPTIVE_THRESH_MEAN_C:阈值取自相邻区域的平 均值 – cv2.ADPTIVE_THRESH_GAUSSIAN_C:阈值取值相邻区域 的加权和,权重为一个高斯窗口。 • Block Size - 邻域大小(用来计算阈值的区域大小)。 • C - 这就是是一个常数,阈值就等于的平均值或者加权平均值减去这个常数。 import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('lhu.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()Otsu’s 二值化
对一副双峰图像自动根据其直方图计算出一个阈值。 import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('noisy2.png',0) #第一种方法,我们设 127 为全局阈值 ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY) #第二种方法,我们直接使用 Otsu 二值化。 ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) # ( 5,5 )为高斯核的大小, 0 为标准差 blur = cv2.GaussianBlur(img,(5,5),0) # 阈值一定要设为 0 ! #第三种方法,我 们首先使用一个 5x5 的高斯核除去噪音,然后再使用 Otsu 二值化。 ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 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"] #这里使用了pyplot中画直方图的方法, plt.hist,要注意的是它的参数是一维数组 #所以这里使用了( numpy ) ravel方法,将多维数组转换成一维,也可以使用flatten方法 for i in range(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()