【youcans 的 OpenCV 例程200篇】162. 全局阈值处理改进方法

欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列,持续更新中
OpenCV 例程200篇 总目录-202205更新


【youcans 的 OpenCV 例程200篇】162. 全局阈值处理改进方法


3.4 全局阈值处理改进方法

在实际的图像处理中,噪声严重影响阈值处理的结果,严重的噪声会把简单的阈值处理问题变为不能解决的问题。

例程 11.19:全局阈值处理改进之图像平滑

对于存在噪声影响的图像,一种常用的改进方法是在阈值处理前先对图像进行平滑。

    # 11.19 全局阈值处理改进之图像平滑
    img = cv2.imread("../images/Fig1039a.tif", flags=0)

    histCV = cv2.calcHist([img], [0], None, [256], [0, 256])  # 灰度直方图
    scale = range(256)  # 灰度级 [0,255]
    totalPixels = img.shape[0] * img.shape[1]  # 像素总数
    totalGray = np.dot(histCV[:,0], scale)  # 内积, 总和灰度值
    mG = totalGray / totalPixels  # 平均灰度

    icv = np.zeros(256)
    numFt, sumFt = 0, 0
    for t in range(256):  # 遍历灰度值
        numFt += histCV[t,0]   # F(t) 像素数量
        sumFt += histCV[t,0] * t  # F(t) 灰度值总和
        pF = numFt / totalPixels  # F(t) 像素数占比
        mF = (sumFt/numFt) if numFt>0 else 0  # F(t) 平均灰度
        numBt = totalPixels-numFt  # B(t) 像素数量
        sumBt = totalGray - sumFt  # B(t) 灰度值总和
        pB = numBt / totalPixels  # B(t) 像素数占比
        mB = (sumBt/numBt) if numBt>0 else 0  # B(t) 平均灰度
        icv[t] = pF * (mF-mG)**2 + pB * (mB-mG)**2  # 灰度 t 的类间方差
    maxIcv = max(icv)  # ICV 的最大值
    maxIndex = np.argmax(icv)  # 最大值的索引

    # 阈值处理
    ret, imgBin = cv2.threshold(img, maxIndex, 255, cv2.THRESH_BINARY)  # 以 maxIndex 作为最优阈值
    ret, imgOtsu = cv2.threshold(img, mG, 255, cv2.THRESH_OTSU)  # 阈值分割, OTSU
    print("t(maxICV)={}, retOtsu={}".format(maxIndex, round(ret)))

    plt.figure(figsize=(7,7))
    plt.subplot(221), plt.axis('off'), plt.title("Origin"), plt.imshow(img, 'gray')
    plt.subplot(222, yticks=[]), plt.title("Gray Hist")  # 直方图
    plt.plot(scale, histCV[:,0]/max(histCV))  # 灰度直方图
    plt.plot(scale, icv/maxIcv)  # 类间方差图
    plt.subplot(223), plt.title("global binary(T={})".format(maxIndex)), plt.axis('off')
    plt.imshow(imgBin, 'gray')
    plt.subplot(224), plt.title("OTSU binary(T={})".format(round(ret))), plt.axis('off')
    plt.imshow(imgOtsu, 'gray')
    plt.tight_layout()
    plt.show()

在这里插入图片描述


(本节完)


版权声明:

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

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


欢迎关注 『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更新】

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youcans_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值