在图像处理中,Thresholding中文翻译过来叫二值化或者阈值化。二值化就是把图片传换成只有white和black这两种颜色。通过Thresholding,可以让图片中感兴趣的颜色变成主角–white,其余的颜色全部隐藏–black。另外,二值化后的图片也便于计算机进行分析,因为边缘轮廓十分清晰,所以计算机可以轻松找到边界线。然而,在找边界这方面,Thresholding并不是特别好的算法,有些时候遇到某些特殊图片效果也不好。
二值化的应用
from imutils import *
image = imread('image/coins.jpg')
show(image)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
plt.imshow(gray,'gray') #使用灰度图效果显示图片
plt.axis('off')
plt.show()
• cv2.THRESH_BINARY
• cv2.THRESH_BINARY_INV # 阈值化操作,值大于阈值,变成较大的值,小于变为0
• cv2.THRESH_TRUNC
• cv2.THRESH_TOZERO
• cv2.THRESH_TOZERO_INV
![FAO](image/threshold.png)
ret1,thresh1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) #阈值都定义为127
ret2,thresh2 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
ret3,thresh3 = cv2.threshold(gray,127,255,cv2.THRESH_TRUNC)
ret4,thresh4 = cv2.threshold(gray,127,255,cv2.THRESH_TOZERO)
ret5,thresh5 = cv2.threshold(gray,127,255,cv2.THRESH_TOZERO_INV)
titles = ['original','BINARY'