【youcans 的 OpenCV 例程200篇】165.多阈值 OTSU 处理方法

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


【youcans 的 OpenCV 例程200篇】165.多阈值 OTSU 处理方法


3.5 多阈值处理方法

OTSU 方法使用最大化类间方差(intra-class variance)作为评价准则,基于对图像直方图的计算,可以给出类间最优分离的最优阈值。

OTSU 方法可以扩展到任意数量的阈值。假设有 K 个分类 c 1 , c 2 , . . . , c k c_1, c_2,...,c_k c1,c2,...,ck 时,可以定义类间方差为:
σ B 2 = ∑ k = 1 K P k ( m k − m G ) 2 \sigma^2_B = \sum^K_{k=1} P_k(m_k - m_G)^2 σB2=k=1KPk(mkmG)2
常用地,考虑由 3个灰度区间组成的 3个类,可以由 2个阈值分割,定义类间方差为:
σ B 2 = P 1 ( m 1 − m G ) 2 + P 2 ( m 2 − m G ) 2 + P 3 ( m 3 − m G ) 2 P 1 + P 2 + P 3 = 1 P 1 ∗ m 1 + P 2 ∗ m 2 + P 3 ∗ m 3 = m G P 1 = ∑ i = 0 k 1 p i ,   P 2 = ∑ i = k 1 + 1 k 2 p i ,   P 3 = ∑ i = k 2 + 1 L − 1 p i m 1 = ∑ i = 0 k 1 i ∗ p i / P 1 ,   m 2 = ∑ i = k 1 + 1 k 2 i ∗ p i / P 2 ,   m 3 = ∑ i = k 2 + 1 L − 1 i ∗ p i / P 3 , \sigma^2_B = P_1(m_1 - m_G)^2 + P_2(m_2 - m_G)^2 + P_3(m_3 - m_G)^2 \\ P_1 + P_2 + P_3 = 1\\ P_1*m_1 + P_2*m_2 + P_3*m_3 = m_G \\ P_1 = \sum^{k_1}_{i=0} p_i ,\ P_2 = \sum^{k_2}_{i=k_1+1} p_i ,\ P_3 = \sum^{L-1}_{i=k_2+1} p_i \\ m_1 = \sum^{k_1}_{i=0} i*p_i /P_1 ,\ m_2 = \sum^{k_2}_{i=k_1+1} i*p_i /P_2 ,\ m_3 = \sum^{L-1}_{i=k_2+1} i*p_i /P_3 ,\\ σB2=P1(m1mG)2+P2(m2mG)2+P3(m3mG)2P1+P2+P3=1P1m1+P2m2+P3m3=mGP1=i=0k1pi, P2=i=k1+1k2pi, P3=i=k2+1L1pim1=i=0k1ipi/P1, m2=i=k1+1k2ipi/P2, m3=i=k2+1L1ipi/P3,
使类间方差 ICV 最大化的灰度值 k 1 , k 2 k_1, k_2 k1,k2 就是最优阈值。

阈值处理后的图像由下式给出:
g ( x , y ) = { a , f ( x , y ) ≤ k 1 ∗ b , k 1 ∗ ≤ f ( x , y ) ≤ k 2 ∗ c , f ( x , y ) ≥ k 2 ∗ g(x,y) = \begin{cases} a, & f(x,y) \le k_1^* \\ b, & k_1^* \le f(x,y) \le k_2^* \\ c, & f(x,y) \ge k_2^* \\ \end{cases} g(x,y)=a,b,c,f(x,y)k1k1f(x,y)k2f(x,y)k2

需要指出的是,双阈值处理涉及两重循环,算法耗时较长,但仍是基于直方图进行运算的,因此用时与图像无关。对于双重循环可以进行优化,就不在本节介绍了。

如果需要使用更多个阈值进行分割,通常不再用循环遍历的方法,而是采用聚类或启发式方法来获得分割阈值。


例程 11.22:阈值处理之多阈值 OTSU

    # 11.22 阈值处理之多阈值 OTSU
    def doubleThreshold(img):
        histCV = cv2.calcHist([img], [0], None, [256], [0, 256])  # 灰度直方图
        grayScale = np.arange(0, 256, 1)  # 灰度级 [0,255]
        totalPixels = img.shape[0] * img.shape[1]  # 像素总数
        totalGray = np.dot(histCV[:,0], grayScale)  # 内积, 总和灰度值
        mG = totalGray / totalPixels  # 平均灰度,meanGray
        varG = sum(((i-mG)**2 * histCV[i,0]/totalPixels) for i in range(256))

        T1, T2, varMax = 1, 2, 0.0
        # minGary, maxGray = np.min(img), np.max(img)  # 最小灰度,最大灰度
        for k1 in range(1, 254):  # k1: [1,253], 1<=k1<k2<=254
            n1 = sum(histCV[:k1, 0])  # C1 像素数量
            s1 = sum((i * histCV[i, 0]) for i in range(k1))
            P1 = n1 / totalPixels  # C1 像素数占比
            m1 = (s1 / n1) if n1 > 0 else 0  # C1 平均灰度

            for k2 in range(k1+1, 256):  # k2: [2,254], k2>k1
                # n2 = sum(histCV[k1+1:k2,0])  # C2 像素数量
                # s2 = sum( (i * histCV[i,0]) for i in range(k1+1,k2) )
                # P2 = n2 / totalPixels  # C2 像素数占比
                # m2 = (s2/n2) if n2>0 else 0  # C2 平均灰度
                n3 = sum(histCV[k2+1:,0])  # C3 像素数量
                s3 = sum((i*histCV[i,0]) for i in range(k2+1,256))
                P3 = n3 / totalPixels  # C3 像素数占比
                m3 = (s3/n3) if n3>0 else 0  # C3 平均灰度

                P2 = 1.0 - P1 - P3  # C2 像素数占比
                m2 = (mG - P1*m1 - P3*m3)/P2 if P2>1e-6 else 0  # C2 平均灰度

                var = P1*(m1-mG)**2 + P2*(m2-mG)**2 + P3*(m3-mG)**2
                if var>varMax:
                    T1, T2, varMax = k1, k2, var

        epsT = varMax / varG  # 可分离测度
        print(totalPixels, mG, varG, varMax, epsT, T1, T2)
        return T1, T2, epsT

    img = cv2.imread("../images/Fig1043a.tif", flags=0)
    # img = cv2.imread("../images/Fig1045a.tif", flags=0)
    histCV = cv2.calcHist([img], [0], None, [256], [0, 256])  # 灰度直方图

    T1, T2, epsT = doubleThreshold(img)
    print("T1={}, T2={}, esp={:.4f}".format(T1, T2, epsT))

    binary = img.copy()
    binary[binary<T1] = 0
    binary[binary>T2] = 255

    ret, imgOtsu = cv2.threshold(img, 127, 255, cv2.THRESH_OTSU)  # OTSU 阈值分割
    ret1, binary1 = cv2.threshold(img, T1, 255, cv2.THRESH_TOZERO)  # 小于阈值置 0,大于阈值不变
    ret2, binary2 = cv2.threshold(img, T2, 255, cv2.THRESH_TOZERO)

    plt.figure(figsize=(9, 6))
    plt.subplot(231), plt.axis('off'), plt.title("Origin"), plt.imshow(img, 'gray')
    plt.subplot(232,yticks=[]), plt.axis([0,255,0,np.max(histCV)])
    plt.bar(range(256), histCV[:,0]), plt.title("Gray Hist")
    plt.subplot(233), plt.title("OTSU binary(T={})".format(round(ret))), plt.axis('off')
    plt.imshow(imgOtsu, 'gray')
    plt.subplot(234), plt.axis('off'), plt.title("Threshold(T={})".format(T1))
    plt.imshow(binary1, 'gray')
    plt.subplot(235), plt.axis('off'), plt.title("Threshold(T={})".format(T2))
    plt.imshow(binary2, 'gray')
    plt.subplot(236), plt.axis('off'), plt.title("DoubleT({},{})".format(T1,T2))
    plt.imshow(binary, 'gray')
    plt.show()

运行结果:

Fig1043a.tif:T1=35, T2=101, esp=0.8733

在这里插入图片描述

Fig1045a.tif:T1=81, T2=177, esp=0.9540

在这里插入图片描述

扫描二维码关注公众号,回复: 14083501 查看本文章

(本节完)


版权声明:

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

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


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

【youcans 的 OpenCV 例程200篇】01. 图像的读取(cv2.imread)
【youcans 的 OpenCV 例程200篇】02. 图像的保存(cv2.imwrite)
【youcans 的 OpenCV 例程200篇】03. 图像的显示(cv2.imshow)
【youcans 的 OpenCV 例程200篇】04. 用 matplotlib 显示图像(plt.imshow)
【youcans 的 OpenCV 例程200篇】05. 图像的属性(np.shape)
【youcans 的 OpenCV 例程200篇】06. 像素的编辑(img.itemset)
【youcans 的 OpenCV 例程200篇】07. 图像的创建(np.zeros)
【youcans 的 OpenCV 例程200篇】08. 图像的复制(np.copy)
【youcans 的 OpenCV 例程200篇】09. 图像的裁剪(cv2.selectROI)
【youcans 的 OpenCV 例程200篇】10. 图像的拼接(np.hstack)
【youcans 的 OpenCV 例程200篇】11. 图像通道的拆分(cv2.split)
【youcans 的 OpenCV 例程200篇】12. 图像通道的合并(cv2.merge)
【youcans 的 OpenCV 例程200篇】13. 图像的加法运算(cv2.add)
【youcans 的 OpenCV 例程200篇】14. 图像与标量相加(cv2.add)
【youcans 的 OpenCV 例程200篇】15. 图像的加权加法(cv2.addWeight)
【youcans 的 OpenCV 例程200篇】16. 不同尺寸的图像加法
【youcans 的 OpenCV 例程200篇】17. 两张图像的渐变切换
【youcans 的 OpenCV 例程200篇】18. 图像的掩模加法
【youcans 的 OpenCV 例程200篇】19. 图像的圆形遮罩
【youcans 的 OpenCV 例程200篇】20. 图像的按位运算
【youcans 的 OpenCV 例程200篇】21. 图像的叠加
【youcans 的 OpenCV 例程200篇】22. 图像添加非中文文字
【youcans 的 OpenCV 例程200篇】23. 图像添加中文文字
【youcans 的 OpenCV 例程200篇】24. 图像的仿射变换
【youcans 的 OpenCV 例程200篇】25. 图像的平移
【youcans 的 OpenCV 例程200篇】26. 图像的旋转(以原点为中心)
【youcans 的 OpenCV 例程200篇】27. 图像的旋转(以任意点为中心)
【youcans 的 OpenCV 例程200篇】28. 图像的旋转(直角旋转)
【youcans 的 OpenCV 例程200篇】29. 图像的翻转(cv2.flip)
【youcans 的 OpenCV 例程200篇】30. 图像的缩放(cv2.resize)
【youcans 的 OpenCV 例程200篇】31. 图像金字塔(cv2.pyrDown)
【youcans 的 OpenCV 例程200篇】32. 图像的扭变(错切)
【youcans 的 OpenCV 例程200篇】33. 图像的复合变换
【youcans 的 OpenCV 例程200篇】34. 图像的投影变换
【youcans 的 OpenCV 例程200篇】35. 图像的投影变换(边界填充)
【youcans 的 OpenCV 例程200篇】36. 直角坐标与极坐标的转换
【youcans 的 OpenCV 例程200篇】37. 图像的灰度化处理和二值化处理
【youcans 的 OpenCV 例程200篇】38. 图像的反色变换(图像反转)
【youcans 的 OpenCV 例程200篇】39. 图像灰度的线性变换
【youcans 的 OpenCV 例程200篇】40. 图像分段线性灰度变换
【youcans 的 OpenCV 例程200篇】41. 图像的灰度变换(灰度级分层)
【youcans 的 OpenCV 例程200篇】42. 图像的灰度变换(比特平面分层)
【youcans 的 OpenCV 例程200篇】43. 图像的灰度变换(对数变换)
【youcans 的 OpenCV 例程200篇】44. 图像的灰度变换(伽马变换)
【youcans 的 OpenCV 例程200篇】45. 图像的灰度直方图
【youcans 的 OpenCV 例程200篇】46. 直方图均衡化
【youcans 的 OpenCV 例程200篇】47. 图像增强—直方图匹配
【youcans 的 OpenCV 例程200篇】48. 图像增强—彩色直方图匹配
【youcans 的 OpenCV 例程200篇】49. 图像增强—局部直方图处理
【youcans 的 OpenCV 例程200篇】50. 图像增强—直方图统计量图像增强
【youcans 的 OpenCV 例程200篇】51. 图像增强—直方图反向追踪
【youcans 的 OpenCV 例程200篇】52. 图像的相关与卷积运算
【youcans 的 OpenCV 例程200篇】53. Scipy 实现图像二维卷积
【youcans 的 OpenCV 例程200篇】54. OpenCV 实现图像二维卷积
【youcans 的 OpenCV 例程200篇】55. 可分离卷积核
【youcans 的 OpenCV 例程200篇】56. 低通盒式滤波器
【youcans 的 OpenCV 例程200篇】57. 低通高斯滤波器
【youcans 的 OpenCV 例程200篇】58. 非线性滤波—中值滤波
【youcans 的 OpenCV 例程200篇】59. 非线性滤波—双边滤波
【youcans 的 OpenCV 例程200篇】60. 非线性滤波—联合双边滤波
【youcans 的 OpenCV 例程200篇】61. 导向滤波(Guided filter)
【youcans 的 OpenCV 例程200篇】62. 图像锐化——钝化掩蔽
【youcans 的 OpenCV 例程200篇】63. 图像锐化——Laplacian 算子
【youcans 的 OpenCV 例程200篇】64. 图像锐化——Sobel 算子
【youcans 的 OpenCV 例程200篇】65. 图像锐化——Scharr 算子
【youcans 的 OpenCV 例程200篇】66. 图像滤波之低通/高通/带阻/带通
【youcans 的 OpenCV 例程200篇】67. 空间域图像增强的综合应用
【youcans 的 OpenCV 例程200篇】68. 空间域图像增强的综合应用
【youcans 的 OpenCV 例程200篇】69. 连续非周期信号的傅立叶系数
【youcans 的 OpenCV 例程200篇】70. 一维连续函数的傅里叶变换
【youcans 的 OpenCV 例程200篇】71. 连续函数的取样
【youcans 的 OpenCV 例程200篇】72. 一维离散傅里叶变换
【youcans 的 OpenCV 例程200篇】73. 二维连续傅里叶变换
【youcans 的 OpenCV 例程200篇】74. 图像的抗混叠
【youcans 的 OpenCV 例程200篇】75. Numpy 实现图像傅里叶变换
【youcans 的 OpenCV 例程200篇】76. OpenCV 实现图像傅里叶变换
【youcans 的 OpenCV 例程200篇】77. OpenCV 实现快速傅里叶变换
【youcans 的 OpenCV 例程200篇】78. 频率域图像滤波基础
【youcans 的 OpenCV 例程200篇】79. 频率域图像滤波的基本步骤
【youcans 的 OpenCV 例程200篇】80. 频率域图像滤波详细步骤
【youcans 的 OpenCV 例程200篇】81. 频率域高斯低通滤波器
【youcans 的 OpenCV 例程200篇】82. 频率域巴特沃斯低通滤波器
【youcans 的 OpenCV 例程200篇】83. 频率域低通滤波:印刷文本字符修复
【youcans 的 OpenCV 例程200篇】84. 由低通滤波器得到高通滤波器
【youcans 的 OpenCV 例程200篇】85. 频率域高通滤波器的应用
【youcans 的 OpenCV 例程200篇】86. 频率域滤波应用:指纹图像处理
【youcans 的 OpenCV 例程200篇】87. 频率域钝化掩蔽
【youcans 的 OpenCV 例程200篇】88. 频率域拉普拉斯高通滤波
【youcans 的 OpenCV 例程200篇】89. 带阻滤波器的传递函数
【youcans 的 OpenCV 例程200篇】90. 频率域陷波滤波器
【youcans 的 OpenCV 例程200篇】91. 高斯噪声、瑞利噪声、爱尔兰噪声
【youcans 的 OpenCV 例程200篇】92. 指数噪声、均匀噪声、椒盐噪声
【youcans 的 OpenCV 例程200篇】93. 噪声模型的直方图
【youcans 的 OpenCV 例程200篇】94. 算术平均滤波器
【youcans 的 OpenCV 例程200篇】95. 几何均值滤波器
【youcans 的 OpenCV 例程200篇】96. 谐波平均滤波器
【youcans 的 OpenCV 例程200篇】97. 反谐波平均滤波器
【youcans 的 OpenCV 例程200篇】98. 统计排序滤波器
【youcans 的 OpenCV 例程200篇】99. 修正阿尔法均值滤波器
【youcans 的 OpenCV 例程200篇】100. 自适应局部降噪滤波器
【youcans 的 OpenCV 例程200篇】101. 自适应中值滤波器
【youcans 的 OpenCV 例程200篇】102. 陷波带阻滤波器的传递函数
【youcans 的 OpenCV 例程200篇】103. 陷波带阻滤波器消除周期噪声干扰
【youcans 的 OpenCV 例程200篇】104. 运动模糊退化模型
【youcans 的 OpenCV 例程200篇】105. 湍流模糊退化模型
【youcans 的 OpenCV 例程200篇】106. 退化图像的逆滤波
【youcans 的 OpenCV 例程200篇】107. 退化图像的维纳滤波
【youcans 的 OpenCV 例程200篇】108. 约束最小二乘方滤波
【youcans 的 OpenCV 例程200篇】109. 几何均值滤波
【youcans 的 OpenCV 例程200篇】110. 投影和雷登变换
【youcans 的 OpenCV 例程200篇】111. 雷登变换反投影重建图像
【youcans 的 OpenCV 例程200篇】112. 滤波反投影重建图像
【youcans 的 OpenCV 例程200篇】113. 形态学操作之腐蚀
【youcans 的 OpenCV 例程200篇】114. 形态学操作之膨胀
【youcans 的 OpenCV 例程200篇】115. 形态学操作之开运算
【youcans 的 OpenCV 例程200篇】116. 形态学操作之闭运算
【youcans 的 OpenCV 例程200篇】117. 形态学操作之顶帽运算
【youcans 的 OpenCV 例程200篇】118. 形态学操作之底帽运算
【youcans 的 OpenCV 例程200篇】119. 图像的形态学梯度
【youcans 的 OpenCV 例程200篇】120. 击中-击不中变换
【youcans 的 OpenCV 例程200篇】121. 击中-击不中用于特征识别
【youcans 的 OpenCV 例程200篇】122. 形态算法之边界提取
【youcans 的 OpenCV 例程200篇】123. 形态算法之孔洞填充
【youcans 的 OpenCV 例程200篇】124. 孔洞填充的泛洪算法
【youcans 的 OpenCV 例程200篇】125. 形态算法之提取连通分量
【youcans 的 OpenCV 例程200篇】126. 形态算法之凸壳
【youcans 的 OpenCV 例程200篇】127. 形态算法之细化
【youcans 的 OpenCV 例程200篇】128. 形态算法之骨架 (skimage)
【youcans 的 OpenCV 例程200篇】129. 形态算法之骨架 (重建开运算)
【youcans 的 OpenCV 例程200篇】130. 形态学之提取水平和垂直线
【youcans 的 OpenCV 例程200篇】131. 形态学重建之竖线字符提取
【youcans 的 OpenCV 例程200篇】132. 形态学重建之孔洞填充算法
【youcans 的 OpenCV 例程200篇】133. 形态学重建之边界清除
【youcans 的 OpenCV 例程200篇】134. 形态学重建之细胞计数
【youcans 的 OpenCV 例程200篇】135. 形态学重建之粒度测定
【youcans 的 OpenCV 例程200篇】136. 灰度腐蚀和灰度膨胀
【youcans 的 OpenCV 例程200篇】137. 灰度开运算和灰度闭运算原理
【youcans 的 OpenCV 例程200篇】138. 灰度开运算和灰度闭运算
【youcans 的 OpenCV 例程200篇】139. 灰度顶帽变换校正阴影
【youcans 的 OpenCV 例程200篇】140. 灰度底帽变换校正光照
【youcans 的 OpenCV 例程200篇】141. 灰度底帽变换的三维地形图
【youcans 的 OpenCV 例程200篇】142. 基于灰度形态学的图像平滑
【youcans 的 OpenCV 例程200篇】143. 基于灰度形态学的粒度测定
【youcans 的 OpenCV 例程200篇】144. 基于灰度形态学的纹理分割
【youcans 的 OpenCV 例程200篇】145. 形态学之边缘和角点检测
【youcans 的 OpenCV 例程200篇】146. 基于灰度形态学的复杂背景图像重建
【youcans 的 OpenCV 例程200篇】147. 图像分割之孤立点检测
【youcans 的 OpenCV 例程200篇】148. 图像分割之线检测
【youcans 的 OpenCV 例程200篇】149. 图像分割之边缘模型
【youcans 的 OpenCV 例程200篇】150. 边缘检测梯度算子
【youcans 的 OpenCV 例程200篇】151. 边缘检测中的平滑处理
【youcans 的 OpenCV 例程200篇】152. 边缘检测之 LoG 算子
【youcans 的 OpenCV 例程200篇】153. 边缘检测之 DoG 算子
【youcans 的 OpenCV 例程200篇】154. 边缘检测之 Canny 算子
【youcans 的 OpenCV 例程200篇】155. 边缘连接的局部处理方法
【youcans 的 OpenCV 例程200篇】156. 边缘连接局部处理的简化算法
【youcans 的 OpenCV 例程200篇】157. 霍夫变换直线检测
【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 处理方法

猜你喜欢

转载自blog.csdn.net/youcans/article/details/124400439