【youcans 的 OpenCV 例程200篇】167.基于移动平均的可变阈值处理

欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列,持续更新中
欢迎关注 『youcans 的 OpenCV学习课』 系列,持续更新中


【youcans 的 OpenCV 例程200篇】167.基于移动平均的可变阈值处理


3.7 基于移动平均的可变阈值处理

移动平均法是一种简单高效的局部阈值方法。

灰度遮蔽着点照明(如闪光灯)图像中十分常见。图中被斑点灰度模式遮蔽的手写文本图像,如果使用 OTSU 全局阈值处理,不能克服灰度变化的影响。在不均匀的光照场中,阈值分割的效果不好。使用移动平均的局部阈值,则能很好地进行处理。

移动平均法是线性的 Z 字形模式扫描整个图片,对每个像素产生一个阈值。

m ( k + 1 ) = { 1 n ∑ i = k + 2 − n k + 1 z i , k ≥ n − 1 m ( k ) + 1 n ( z k + 1 − z k − n ) , k ≥ n + 1 m(k+1) = \begin{cases} \frac{1}{n} \sum_{i=k+2-n}^{k+1} z_i, & k \ge n-1 \\ m(k) + \frac{1}{n} (z_{k+1} - z_{k-n}), & k \ge n+1 \end{cases}\\ m(k+1)={n1i=k+2nk+1zi,m(k)+n1(zk+1zkn),kn1kn+1

然后进行阈值处理:

g ( x , y ) = { 1 , f ( x , y ) ≥ b ∗ m ( x , y ) 0 , k < b ∗ m ( x , y ) g(x,y)= \begin{cases} 1, & f(x,y) \ge b*m(x,y) \\ 0, & k \lt b*m(x,y) \end{cases} g(x,y)={1,0,f(x,y)bm(x,y)k<bm(x,y)

对于手写内容的图像,通常可以令 n 为平均笔画宽度的 5倍。


例程 11.24:基于移动平均的可变阈值处理

    # 11.24 基于移动平均的可变阈值处理
    def movingThreshold(img, n, b):
        img[1:-1:2, :] = np.fliplr(img[1:-1:2, :])  # 向量翻转
        f = img.flatten()  # 展平为一维
        ret = np.cumsum(f)
        ret[n:] = ret[n:] - ret[:-n]
        m = ret / n  # 移动平均值
        g = np.array(f>=b*m).astype(int)  # 阈值判断,g=1 if f>=b*m
        g = g.reshape(img.shape)  # 恢复为二维
        g[1:-1:2, :] = np.fliplr(g[1:-1:2, :])  # 交替翻转
        return g*255

    img1 = cv2.imread("../images/Fig1049a.tif", flags=0)
    img2 = cv2.imread("../images/Fig1050a.tif", flags=0)

    ret1, imgOtsu1 = cv2.threshold(img1, 127, 255, cv2.THRESH_OTSU)  # OTSU 阈值分割
    ret2, imgOtsu2 = cv2.threshold(img2, 127, 255, cv2.THRESH_OTSU)

    imgMoveThres1 = movingThreshold(img1, 20, 0.5)  # 移动平均阈值处理
    imgMoveThres2 = movingThreshold(img2, 20, 0.5)  # n=20, b=0.5

    plt.figure(figsize=(9, 6))
    plt.subplot(231), plt.axis('off'), plt.title("Origin")
    plt.imshow(img1, 'gray')
    plt.subplot(232), plt.axis('off'), plt.title("OTSU(T={})".format(ret1))
    plt.imshow(imgOtsu1, 'gray')
    plt.subplot(233), plt.axis('off'), plt.title("Moving threshold")
    plt.imshow(imgMoveThres1, 'gray')
    plt.subplot(234), plt.axis('off'), plt.title("Origin")
    plt.imshow(img2, 'gray')
    plt.subplot(235), plt.axis('off'), plt.title("OTSU(T={})".format(ret2))
    plt.imshow(imgOtsu2, 'gray')
    plt.subplot(236), plt.axis('off'), plt.title("Moving threshold")
    plt.imshow(imgMoveThres2, 'gray')
    plt.tight_layout()
    plt.show()

【youcans 的 OpenCV 例程200篇】167.基于移动平均的可变阈值处理_第1张图片


(本节完)


版权声明:

youcans@xupt 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/124400813)

Copyright 2022 youcans, XUPT
Crated:2022-4-25


欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列,持续更新中
欢迎关注 『youcans 的 OpenCV学习课』 系列,持续更新中

【youcans 的 OpenCV 例程200篇】158. 阈值处理之固定阈值法
【youcans 的 OpenCV 例程200篇】159. 图像分割之全局阈值处理
【youcans 的 OpenCV 例程200篇】160. 图像处理之OTSU 方法
【youcans 的 OpenCV 例程200篇】161. OTSU 阈值处理算法的实现
【youcans 的 OpenCV 例程200篇】162. 全局阈值处理改进方法
【youcans 的 OpenCV 例程200篇】163. 基于边缘信息改进全局阈值处理
【youcans 的 OpenCV 例程200篇】164.使用 Laplace 边缘信息改进全局阈值处理
【youcans 的 OpenCV 例程200篇】165.多阈值 OTSU 处理方法
【youcans 的 OpenCV 例程200篇】166.自适应阈值处理
【youcans 的 OpenCV 例程200篇】167.基于移动平均的可变阈值处理
更多内容,请见:
【OpenCV 例程200篇 总目录-202206更新】

你可能感兴趣的:(opencv,python,图像处理,计算机视觉,算法)