【youcans 的 OpenCV 例程200篇】159. 图像分割之全局阈值处理

【OpenCV 例程200篇】 系列,持续更新中…
【OpenCV 例程200篇 总目录-202205更新】


【youcans 的 OpenCV 例程200篇】159. 图像分割之全局阈值处理


3.2 全局阈值处理基本方法

当图像中的目标和背景的灰度分布较为明显时,可以对整个图像使用固定阈值进行全局阈值处理。

为了获得适当的全局阈值,可以基于灰度直方图进行迭代计算:

(1)设置初始阈值 T,通常可以设为图像的平均灰度;
(2)用灰度阈值 T 分割图像:灰度值等于 T 的所有像素集合 G1 和 大于等于 T 的所有像素集合 G2;
(3)分别计算 G1、G2 的平均灰度值 m1、m2;
(4)求出新的灰度阈值 T = ( m 1 + m 2 ) / 2 T=(m1+m2)/2 T=(m1+m2)/2
(5)重复步骤(2)~(4),直到阈值变化小于设定值。


例程 11.16:图像分割之全局阈值处理

    # 11.16 图像分割之全局阈值处理
    img = cv2.imread("../images/Fig0940a.tif", flags=0)

    deltaT = 1  # 预定义值
    histCV = cv2.calcHist([img], [0], None, [256], [0, 256])  # 灰度直方图
    grayScale = range(256)  # 灰度级 [0,255]
    totalPixels = img.shape[0] * img.shape[1]  # 像素总数
    totalGary = np.dot(histCV[:,0], grayScale)  # 内积, 总和灰度值
    T = round(totalGary/totalPixels)  # 平均灰度

    while True:
        numC1, sumC1 = 0, 0
        for i in range(T): # 计算 C1: (0,T) 平均灰度
            numC1 += histCV[i,0]  # C1 像素数量
            sumC1 += histCV[i,0] * i  # C1 灰度值总和
        numC2, sumC2 = (totalPixels-numC1), (totalGary-sumC1)  # C2 像素数量, 灰度值总和
        T1 = round(sumC1/numC1)  # C1 平均灰度
        T2 = round(sumC2/numC2)  # C2 平均灰度
        Tnew = round((T1+T2)/2)  # 计算新的阈值
        print("T={}, m1={}, m2={}, Tnew={}".format(T, T1, T2, Tnew))
        if abs(T-Tnew) < deltaT:  # 等价于 T==Tnew
            break
        else:
            T = Tnew

    # 阈值处理
    ret, imgBin = cv2.threshold(img, T, 255, cv2.THRESH_BINARY)  # 阈值分割, thresh=T

    plt.figure(figsize=(11, 4))
    plt.subplot(131), plt.axis('off'), plt.title("original"), plt.imshow(img, 'gray')
    plt.subplot(132, yticks=[]), plt.title("Gray Hist")  # 直方图
    histNP, bins = np.histogram(img.flatten(), bins=255, range=[0, 255], density=True)
    plt.bar(bins[:-1], histNP[:])
    plt.subplot(133), plt.title("threshold={}".format(T)), plt.axis('off')
    plt.imshow(imgBin, 'gray')
    plt.tight_layout()
    plt.show()

在这里插入图片描述


(本节完)


版权声明:

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

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


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

youcans_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值