定义:图像的二值化,就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的只有黑和白的视觉效果。
一幅图像包括目标物体、背景还有噪声,要想从多值的数字图像中直接提取出目标物体,常用的方法就是设定一个阈值T,用T将图像的数据分成两部分:大于T的像素群和小于T的像素群。这是研究灰度变换的最特殊的方法,称为图像的二值化(Binarization)。
全局阈值:
Python-OpenCV中提供了阈值(threshold)函数:cv2.threshold(src, threshold, maxValue, method)
src原图:破折线为将被阈值化的值;虚线为阈值
cv2.THRESH_BINARY:大于阈值的像素点的灰度值设定为maxValue(如8位灰度值最大为255),灰度值小于阈值的像素点的灰度值设定为0。
cv2.THRESH_BINARY_INV :大于阈值的像素点的灰度值设定为0,而小于该阈值的设定为maxValue。
cv2.THRESH_TRUNC:像素点的灰度值小于阈值不改变,大于阈值的灰度值的像素点就设定为该阈值。
cv2.THRESH_TOZERO:像素点的灰度值小于该阈值的不进行任何改变,而大于该阈值的部分,其灰度值全部变为0。
cv2.THRESH_TOZERO_INV:像素点的灰度值大于该阈值的不进行任何改变,像素点的灰度值小于该阈值的,其灰度值全部变为0。
Python+opencv代码:
def getPicMinRect(pic):
GrayImage = np.array(pic).reshape(40,40).astype(np.uint8)
ret,thresh1=cv2.threshold(GrayImage,10,255,cv2.THRESH_BINARY)
ret,thresh2=cv2.threshold(GrayImage,10,255,cv2.THRESH_BINARY_INV)
ret,thresh3=cv2.threshold(GrayImage,10,255,cv2.THRESH_TRUNC)
ret,thresh4=cv2.threshold(GrayImage,10,255,cv2.THRESH_TOZERO)
ret,thresh5=cv2.threshold(GrayImage,10,255,cv2.THRESH_TOZERO_INV)
titles = ['Gray Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [GrayImage, 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()
自适应阈值:
当同一幅图像上的不同部分的具有不同亮度时。这种情况下我们需要采用自适应阈值。此时的阈值是根据图像上的每一个小区域计算与其对应的阈值。因此在同一幅图像上的不同区域采用的是不同的阈值,从而使我们能在亮度不同的情况下得到更好的结果。
Python-OpenCV中提供了阈值(threshold)函数:cv2.adaptiveThreshold(src, maxValue, adaptive_method, threshold_type, block_size, param1)
好吧,这玩意的threshold_type其实就两种:CV_THRESH_BINARY, CV_THRESH_BINARY_INV
adaptive_method也有两种: CV_ADAPTIVE_THRESH_MEAN_C, CV_ADAPTIVE_THRESH_GAUSSIAN_C
函数 cvAdaptiveThreshold 将灰度图像变换到二值图像,采用下面公式:
switch(threshold_type):
case CV_THRESH_BINARY:
if src(x,y)>T(x,y):
dst(x,y) = maxValue
else:
dsy(x,y) = 0
case CV_THRESH_BINARY_INV:
if src(x,y)>T(x,y):
dst(x,y) = 0
else:
dsy(x,y) = maxValue
其中 T(x,y)为当前像素点单独计算的阈值
对方法 CV_ADAPTIVE_THRESH_MEAN_C,先求出block中的均值,再减掉param1。
对方法 CV_ADAPTIVE_THRESH_GAUSSIAN_C ,先求出block中的加权和(gaussian), 再减掉param1。
Python+opencv代码:
def getPic(pic):
GrayImage = np.array(pic).reshape(40,40).astype(np.uint8)
th1 = cv2.adaptiveThreshold(GrayImage,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,3,5)
th2 = cv2.adaptiveThreshold(GrayImage,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,3,50)
th3 = cv2.adaptiveThreshold(GrayImage,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,3,5)
th4 = cv2.adaptiveThreshold(GrayImage,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,3,50)
titles = ['Gray Image', 'Adaptive Mean prama1=5',
'Adaptive Mean prama1=50', 'Adaptive Gaussian prama1=5','Adaptive Gaussian prama1=50']
images = [GrayImage, th1, th2, th3, th4]
for i in xrange(5):
plt.subplot(2,3,i+1),plt.imshow(images[i])
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()