全局阈值(又称简单阈值)
顾名思义,整幅图像采用一个阈值,当某一点像素值大于阈值时,赋予该点一个新值,否则就赋予另外一种值。采用函数cv2.threshold()实现。
cv2.threshold(src, thresh, maxval, type)
参数:
补:cv2.THRESH_OTSU(该方法自动寻找最优阈值,并返回给retval,见下文。实际使用时配合上述5种转变方式之一,例如:cv2.THRESH_OTSU+cv2.THRESH_BINARY)
返回值:
下面通过一段代码进行直观认识:
import cv2
import matplotlib.pyplot as plt
#设定阈值
thresh=130
#载入原图,并转化为灰度图像
img_original=cv2.imread('E:\ShannonT\\notebook workspace\\images\\4.24.tudou.png',0)
img_original=cv2.resize(img_original,(0,0),fx=0.3,fy=0.3)
#采用5种阈值类型(thresholding type)分割图像
retval1,img_binary=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY)
retval2,img_binary_invertion=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY_INV)
retval3,img_trunc=cv2.threshold(img_original,thresh,255,cv2.THRESH_TRUNC)
retval4,img_tozero=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO)
retval5,img_tozero_inversion=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO_INV)
#采用plt.imshow()显示图像
imgs=[img_original,img_binary,img_binary_invertion,img_trunc,img_tozero,img_tozero_inversion]
titles=['original','binary','binary_inv','trunc','tozero','tozero_inv']
for i in range(6):
plt.subplot(2,3,i+1)
plt.imshow(imgs[i],'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
效果图如下:
下面通过加入滑动条动态改变阈值参数,代码如下:
import cv2
import numpy as np
import matplotlib.pyplot as plt
#载入原图,转化为灰度图像,并通过cv2.resize()等比调整图像大小
img_original=cv2.imread('E:\ShannonT\\notebook workspace\\images\\4.24.tudou.png',0)
img_original=cv2.resize(img_original,(0,0),fx=0.3,fy=0.3)
#初始化阈值,定义全局变量imgs
thresh=130
imgs=0
#创建滑动条回调函数,参数thresh为滑动条对应位置的数值
def threshold_segmentation(thresh):
#采用5种阈值类型(thresholding type)分割图像
retval1,img_binary=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY)
retval2,img_binary_invertion=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY_INV)
retval3,img_trunc=cv2.threshold(img_original,thresh,255,cv2.THRESH_TRUNC)
retval4,img_tozero=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO)
retval5,img_tozero_inversion=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO_INV)
#由于cv2.imshow()显示的是多维数组(ndarray),因此我们通过np.hstack(数组水平拼接)
#和np.vstack(竖直拼接)拼接数组,达到同时显示多幅图的目的
img1=np.hstack([img_original,img_binary,img_binary_invertion])
img2=np.hstack([img_trunc,img_tozero,img_tozero_inversion])
global imgs
imgs=np.vstack([img1,img2])
#新建窗口
cv2.namedWindow('Images')
#新建滑动条,初始位置为130
cv2.createTrackbar('threshold value','Images',130,255,threshold_segmentation)
#第一次调用函数
threshold_segmentation(thresh)
#显示图像
while(1):
cv2.imshow('Images',imgs)
if cv2.waitKey(1)==ord('q'):
break
cv2.destroyAllWindows()
GIF演示如下: