【图像分割】使用Otsu 算法及迭代计算最佳全局阈值估计并实现图像分割(代码实现与分析)

        本实验要求理解全局阈值分割的概念,并实现文本图像分割。需要大家深入理解Ostu 算法的实现过程及其迭代原理,同时通过学习使用Otsu 算法及其迭代,实践图像分割技术在文本图像处理中的应用。

        以下将从实验原理、实验实现、实验结果分析三部分对整个实验进行阐述

实验原理

全局阈值分割原理

        全局阈值分割是一种基于灰度图像的简单分割方法。其基本思想是根据一个固定的阈值T,将图像中的每个像素点的灰度值与阈值T进行比较。如果像素点的灰度值大于或等于T,则将其归为前景(通常表示感兴趣的物体或区域);否则,将其归为背景。

Otsu算法原理

        Otsu算法是一种自动选择全局阈值的方法,它通过最大化类间方差(inter-class variance)来确定最优的阈值。类间方差反映了前景和背景两类像素之间的差异程度,差异越大,说明分割效果越好。

以下是Otsu算法的具体步骤:

        a. 计算图像的灰度直方图:直方图表示了图像中各个灰度级像素出现的频率。

        b. 计算各类别的概率:对于每一个可能的阈值T,可以将图像分为两个类别,一类是灰度值小于T的像素,另一类是灰度值大于或等于T的像素。计算这两个类别的像素数(或者像素的概率)。

        c. 计算类间方差:类间方差定义为两类像素的平均灰度值之差的平方乘以两类像素的概率之和。类间方差越大,说明两类像素的差异越大,分割效果越好。

        d. 寻找最优阈值:遍历所有可能的阈值,对于每个阈值,计算其对应的类间方差。选择使类间方差最大的那个阈值作为最佳全局阈值。

图像分割实现

        利用计算出的最佳全局阈值,对原始图像进行二值化处理,即根据阈值将每个像素点的灰度值转换为0(背景)或1(前景),从而实现图像的分割。

实验实现

输入图像

在本次实验中,小组选取了三幅灰度图片作为实验的输入图像,如下图所示。

实验代码 

        利用Python实现Otsu算法及其迭代方法。对于输入的图像,首先生成它的一个渐变灰度图像,接着计算图像的直方图,并基于直方图使用Otsu方法和迭代方法分别寻找最佳的阈值。

import cv2
import numpy as np
import os

def get_file_paths(folder_path):
    # 获取文件夹内所有文件的路径
    file_paths = [os.path.join(folder_path, file) for file in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, file))]
    return file_paths

def generate_gradient_image(width, height):
    # 生成渐变灰度图像数据
    gradient_image = np.zeros((height, width), dtype=np.uint8)

    # 计算每一列的亮度值
    for col in range(width):
        brightness = int(255 * col / width)
        gradient_image[:, col] = brightness

    return gradient_image

def save_image(image, file_path):
    # 保存图像
    cv2.imwrite(file_path, image)

def iterative_thresholding(image, epsilon=1e-6, max_iter=100):
    # 初始阈值
    threshold = 128.0

    for _ in range(max_iter):
        # 根据当前阈值将图像二值化
        binary_image = image > threshold

        # 计算前景和背景的平均灰度
        mean_foreground = np.mean(image[binary_image])
        mean_background = np.mean(image[~binary_image])

        # 计算新的阈值
        new_threshold = 0.5 * (mean_foreground + mean_background)

        # 如果新旧阈值之间的差异小于 epsilon,停止迭代
        if abs(new_threshold - threshold) < epsilon:
            break

        threshold = new_threshold

    return threshold

def otsu_thresholding(image): # 计算otsu全局最优阈值
    # 计算直方图
    hist, bins = np.histogram(image.flatten(), 256, [0, 256])
    # 归一化直方图
    hist = hist.astype(float) / sum(hist)
    # 初始化类内方差和类间方差
    var_within = np.zeros(256)
    var_between = np.zeros(256)
    for t in range(1, 256):
        # 类内方差
        w0 = sum(hist[:t])
        w1 = sum(hist[t:])
        mu0 = sum(i * hist[i] for i in range(t)) / w0 if w0 > 0 else 0
        mu1 = sum(i * hist[i] for i in range(t, 256)) / w1 if w1 > 0 else 0
        var_within[t] = w0 * w1 * (mu0 - mu1) ** 2

        # 类间方差
        var_between[t] = w0 * w1 * (mu0 - mu1) ** 2
    # 找到最佳阈值
    optimal_threshold = np.argmax(var_between)
    return optimal_threshold

def threshold(image_path): # 生成阈值化图像
    # 读取图像
    img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    image_path=image_path[5:]
    # 应用Otsu方法获取最佳阈值
    otsu_threshold = otsu_thresholding(img)
    # print(f"threshold:{threshold}")
    # 使用阈值进行二值化
    print(f'otsu_threshold:{otsu_threshold}')
    _, binary_image = cv2.threshold(img, otsu_threshold, 255, cv2.THRESH_BINARY)
    # 保存阈值化图像
    save_image(binary_image, f'Otsu_{image_path}')

    iterative_threshold = iterative_thresholding(img)
    # print(f"threshold:{threshold}")
    # 使用阈值进行二值化
    print(f'iterative_threshold:{iterative_threshold}')
    _, binary_image = cv2.threshold(img, iterative_threshold, 255, cv2.THRESH_BINARY)
    # 保存阈值化图像
    save_image(binary_image, f'iterative_{image_path}')
    print(f'{image_path[:-4]}测试已完成')


if __name__ == '__main__':
    # # 设置图像的宽度和高度
    # width = 640
    # height = 480
    # # 生成渐变图像
    # image = generate_gradient_image(width, height)
    # # 保存图像
    # save_image(image, 'exam0.jpg')

    folder_path = 'exam'
    # 获取文件夹内所有文件的路径
    exam_paths = get_file_paths(folder_path)
    # 依次测试图像
    for image_path in exam_paths:
        print(f'image_name:{image_path[5:]}')
        threshold(image_path)

实验结果分析 

对于三幅图像使用Otsu方法和迭代方法进行全局阈值分割的实验结果和分析如下:

(1)图像一处理结果

        由上图第一组实验结果可以看出,对于灰度分布规律简单的图像,全局分割的阈值寻找较为简单,利用Otsu算法和迭代算法的效果几乎没有区别。 

(2)图像二处理结果

        如上图所示,第二组实验中,Otsu算法和迭代方法在作用于灰度值分界较为明显的图像上时均取得了很好的分割效果。 

(3)图像三处理结果

        由上图所示第三组实验可以看出,即使是边界并不是非常清晰的图片,Otsu方法和迭代方法也都能取得很好的分割效果。


以上为本次实验的全部内容~

最近更新

  1. TCP协议是安全的吗?

    2024-03-17 16:04:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-17 16:04:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-17 16:04:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-17 16:04:03       20 阅读

热门阅读

  1. 【Docker】Kong 容器化部署及配置参数说明

    2024-03-17 16:04:03       17 阅读
  2. centos7网络命令:ping、dig、nsloopup、tcpdump

    2024-03-17 16:04:03       20 阅读
  3. 【Python】pymysql库的介绍及用法

    2024-03-17 16:04:03       23 阅读
  4. Python面向对象编程:入门篇(类和对象)

    2024-03-17 16:04:03       22 阅读
  5. 华为机试真题练习汇总(41~50)

    2024-03-17 16:04:03       20 阅读
  6. 客户端渲染与服务端渲染(2)

    2024-03-17 16:04:03       17 阅读
  7. linux ---vim的基本使用

    2024-03-17 16:04:03       18 阅读
  8. 前端面试练习24.3.16

    2024-03-17 16:04:03       22 阅读
  9. 速盾cdn:cdn关闭后多长生效?

    2024-03-17 16:04:03       20 阅读
  10. 【TypeScript系列】Iterators 和 Generators

    2024-03-17 16:04:03       20 阅读
  11. 代码随想录算法训练营 Day25|回溯算法2

    2024-03-17 16:04:03       24 阅读
  12. c++函数

    c++函数

    2024-03-17 16:04:03      20 阅读