图像二值化

1. 图像二值化

图像二值化就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果的过程。图像的二值化使图像中数据量大为减少,从而能凸显出目标的轮廓。

要得到二值化图像,首先要把图像灰度化,然后将256个亮度等级的灰度图像通过适当的阈值选取而获得仍然可以反映图像整体和局部特征的二值化图像。所有灰度大于或等于阈值的像素被判定为属于特定物体,其灰度值为255,否则这些像素点被排除在物体区域以外,灰度值为0,表示背景或者例外的物体区域。

2. 图像二值化方法及Python实现

比较常用的二值化方法有:简单二值法,平均值法,双峰法和OTSU法等。

2.1 简单二值法

将图像灰度化后,我们选择127(灰度值范围的一半)作为阈值,即将像素值大于127的像素值全部设为255,小于127的全部设为0。

def Easy_Binarization(srcImg_path):
    img = cv.imread(srcImg_path)
    img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    img_gray[img_gray>127] = 255
    img_gray[img_gray<=127] = 0
    plt.imshow(img_gray, cmap='gray')
    plt.title('Easy_Binarization')
    plt.show()

在这里插入图片描述

2.2 平均值法

为了应对每张图片的灰度值大不相同,阈值取为图像本身的平均值。

def Mean_Binarization(srcImg_path):
    img = cv.imread(srcImg_path)
    img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    threshold = np.mean(img_gray)
    print(threshold)
    img_gray[img_gray>threshold] = 255
    img_gray[img_gray<=threshold] = 0
    plt.imshow(img_gray, cmap='gray')
    plt.title('Mean_Binarization')
    plt.show()

在这里插入图片描述

实验中该方法计算的阈值为123。

2.3 双峰法

直方图是图像的重要特质,它可以帮助我们分析图像中的灰度变化。因此,如果物体与背景的灰度值对比明显,直方图就会包含双峰,它们分别为图像的前景和背景,而它们之间的谷底即为边缘附近相对较少数目的像素点,一般来讲,这个最小值就为最优二值化的分界点,通过这个点可以把前景和背景很好地分开。

def Hist_Binarization(srcImg_path):
    img = cv.imread(srcImg_path)
    img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    hist = img_gray.flatten()
    plt.subplot(121)
    plt.hist(hist,256)

    cnt_hist = Counter(hist)
    print(cnt_hist)
    begin,end = cnt_hist.most_common(2)[0][0],cnt_hist.most_common(2)[1][0]
    if begin > end:
        begin, end = end, begin
    print(f'{begin}: {end}')

    cnt = np.iinfo(np.int16).max
    threshold = 0
    for i in range(begin,end+1):
        if cnt_hist[i]<cnt:
            cnt = cnt_hist[i]
            threshold = i
    print(f'{threshold}: {cnt}')
    img_gray[img_gray>threshold] = 255
    img_gray[img_gray<=threshold] = 0
    plt.subplot(122)
    plt.imshow(img_gray, cmap='gray')
    plt.show()

在这里插入图片描述

实验中该方法得到的双峰为(145,154),阈值为150。

2.4 OTSU法

双峰法具有明显的缺陷,因为直方图是不连续的,有非常多尖峰和抖动,要找到准确的极值点十分困难。日本工程师大津展之为这个波谷找到了一个合适的数学表达,并于1979年发表。这个二值化方法称为大津算法(Otsu’s method)。

OTSU法也称作最大类间方差法,因为按照大津法求得的阈值进行图像二值化分割后,前景与背景图像的类间方差最大。它被认为是图像分割中阈值选取的最佳算法,计算简单,不受图像亮度和对比度的影响,因此在数字图像处理上得到了广泛的应用。它是按图像的灰度特性,将图像分成背景和前景两部分。因方差是灰度分布均匀性的一种度量,背景和前景之间的类间方差越大,说明构成图像的两部分的差别越大,当部分前景错分为背景或部分背景错分为前景都会导致类间差别变小。因此,使类间方差最大的分割意味着错分概率最小。

具体计算阈值方法如下:
设阈值为t, 将原图转化成灰度图后,将其高与宽存于h,w,并将小于阈值的灰度值存储在前景front中,大于等于阈值的存在背景back中。如下所示:

# 阈值:t
h, w = img.shape[:2]
front = img[img < t]
back = img[img >= t]

显然,前景与背景的长度和应与h, w的乘积相等,即:

len(front) + len(back) == h * w.

设前景像素数量占总像素数量的比重为frontP,背景像素数量占总像素数量的比重为backP,前景和背景的灰度平均值分别为frontMean和backMean,总平均灰度值为m,则方差公式可写成:
v = f r o n t P ∗ ( f r o n t M e a n − m ) 2 + b a c k P ∗ ( b a c k M e a n − m ) 2 v=frontP*(frontMean-m)^2+backP*(backMean-m)^2 v=frontP(frontMeanm)2+backP(backMeanm)2
又因为:
m = f r o n t P ∗ f r o n t M e a n + b a c k P ∗ b a c k M e a n m=frontP*frontMean+backP*backMean m=frontPfrontMean+backPbackMean
上式可化简为:
v = f r o n t P ∗ b a c k P ∗ ( f r o n t M e a n − b a c k M e a n ) 2 v=frontP*backP*(frontMean-backMean)^2 v=frontPbackP(frontMeanbackMean)2
实验代码如下:

def Otsu(srcImg_path):
    img = cv.imread(srcImg_path)
    img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    h, w = img.shape[:2]
    threshold_t = 0
    max_g = 0
    
    for t in range(255):
        front = img[img < t]
        back = img[img >= t]
        front_p = len(front) / (h * w)
        back_p = len(back) / (h * w)
        front_mean = np.mean(front) if len(front) > 0 else 0.
        back_mean = np.mean(back) if len(back) > 0 else 0.
        
        g = front_p * back_p * ((front_mean - back_mean)**2)
        if g > max_g:
            max_g = g
            threshold_t = t
    print(f"threshold = {threshold_t}")

    img[img < threshold_t] = 0
    img[img >= threshold_t] = 255
    plt.imshow(img, cmap='gray')
    plt.title('Otsu')
    plt.show()

二值化图像如下:
在这里插入图片描述

对比以上四种方法可以发现,Otsu方法得到的二值化图像细节更多,图像更细腻。

3. opencv-python中二值化方法的应用

在OpenCV中,分为简单的阈值分割与自适应阈值分割。

3.1 简单阈值分割(Simple Thresholding)

函数原型如下:

retval, dst = cv.threshold(src, thresh, maxval, type[, dst])

参数说明:
第一个参数src为原图,需要注意的是输入的图像需为灰度图。
第二个参数thresh即为阈值,用于对像素值的分类(一般定义为127)。
第三个参数maxval是最大值,即超过阈值后所定义的值(255)。
第四个参数type,在Simple Thresholding中一共有五种不同的方式: cv.THRESH_BINARY、cv.THRESH_BINARY_INV 、cv.THRESH_TRUNC 、cv.THRESH_TOZERO、cv.THRESH_TOZERO_INV

测试代码如下:

img = cv.imread('lenna.jpg')
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img_gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
ret,thresh1 = cv.threshold(img_gray,127,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img_gray,127,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img_gray,127,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img_gray,127,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img_gray,127,255,cv.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.axis('off')
plt.show()

下图是5种方式的实际效果:
在这里插入图片描述

3.2 自适应阈值分割(Adaptive Thresholding)

函数原型如下:

dst = cv.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst])

其中src, maxValue和thresholdType与Simple Thresholding相同。

在自适应阈值分割中,adaptive method(阈值的计算方式)有两种:

  • cv.ADAPTIVE_THRESH_MEAN_C: 邻域面积(blockSize * blockSize)的平均值并减去C.
  • cv.ADAPTIVE_THRESH_GAUSSIAN_C: 邻域面积的高斯加权总和然后减去C.

下面是它们的实际效果(对于thresholdType在这里选择cv.THRESH_BINARY),测试代码如下:

img = cv.imread('lenna.jpg')
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img_gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
ret,th1 = cv.threshold(img_gray,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img_gray,255,cv.ADAPTIVE_THRESH_MEAN_C,\
            cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img_gray,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv.THRESH_BINARY,11,2)

titles = ['Original Image', 'Simple Thresholding',
            '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.axis('off')
plt.show()

效果如下:
在这里插入图片描述

发现没有像之前otsu那样输出一张类似的图片,但是,它将图像中的边框描绘了出来,实际应用中这样的方式更适合处理文字形式的图片。

4. 源码仓库地址

🌼图像处理、机器学习的常用算法汇总

  • 21
    点赞
  • 274
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值