Mahotas 简明教程

Mahotas - Image Thresholding

图像阈值化是根据像素强度将感兴趣区域与背景分离开来的技术。它包括设置一个阈值,该阈值将图像分为前景和背景。

强度高于阈值的像素被分类为前景,而低于阈值的像素被分类为背景。

这种二进制分离允许进一步的分析,如物体检测、分割或特征提取。图像阈值化可以应用于各种类型的图像,包括灰度图像和彩色图像,以简化后续处理和分析任务。

使用图像阈值化涉及选择一个合适的阈值并将其应用于图像。可以利用各种阈值化技术计算阈值。

阈值化方法的选择取决于图像属性、噪声水平和预期结果等因素。

在这里,我们简要讨论了每种技术。我们将后面章节中讨论深入信息。

让我们看看可以在 mahotas 中执行的每种阈值化技术−

Bernsen Thresholding

伯恩森阈值化是一种阈值化技术,它将灰度图像中的前景与背景分离开来。它根据每个像素周围邻域内的最大和最小像素强度计算局部阈值。

如果像素的强度更接近最大值,则将其视为前景的一部分;否则,则将其视为背景的一部分。

让我们看看下面的伯恩森阈值图像−

bernsen thresholding

Generalized Bernsen Thresholding

广义伯恩森阈值化是伯恩森阈值化方法的改进。它考虑邻域内更广泛的像素强度,而不仅仅是最大和最小强度值。

下图显示了广义伯恩森阈值图像−

generalized bernsen thresholding

Otsu Thresholding

大津阈值化技术自动确定将前景与背景分离开来的最佳阈值。

大津的方法迭代地检查所有可能的阈值,并选择使类间方差最大化的阈值。

让我们看看下列大津阈值化图像−

otsu thresholding

Riddler-Calvard Thresholding

Riddler−Calvard 阈值化也自动确定最佳阈值。它基于前景和背景方差的加权和的最小化。

让我们看看下面的 Riddler−Calvard 阈值图像−

riddler calvard thresholding

Soft Thresholding

软阈值化是用于图像去噪的技术。它将强度值小于某个阈值的像素设置为零。软阈值化在减少噪声的同时保留图像的重要结构信息。

下图显示了软阈值化 −

soft thresholding

Example

在以下示例中,我们尝试执行所有以上解释过的阈值化技巧 −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image = mh.imread('sea.bmp', as_grey=True)
# Bernsen thresholding
bernsen = mh.thresholding.bernsen(image, 5, 5)
mtplt.imshow(bernsen)
mtplt.title('Bernsen Thresholding')
mtplt.axis('off')
mtplt.show()
# Generalized Bernsen thresholding
gbernsen = mh.thresholding.gbernsen(image, mh.disk(3), 10, 109)
mtplt.imshow(gbernsen)
mtplt.title('Generalized Bernsen Thresholding')
mtplt.axis('off')
mtplt.show()
# Otsu threshold
int_image_otsu = image.astype(np.uint8)
otsu = mh.otsu(int_image_otsu)
result_image = image > otsu
mtplt.imshow(result_image)
mtplt.title('Otsu Thresholding')
mtplt.axis('off')
mtplt.show()
# Riddler-Calvard threshold
int_image_rc = image.astype(np.uint8)
rc = mh.thresholding.rc(int_image_rc)
final_image = image > rc
mtplt.imshow(final_image)
mtplt.title('RC Thresholding')
mtplt.axis('off')
mtplt.show()
# Soft threshold
soft = mh.thresholding.soft_threshold(image, np.mean(image))
mtplt.imshow(soft)
mtplt.title('Soft Thresholding')
mtplt.axis('off')
mtplt.show()

获得的输出如下所示 −

Bernsen Thresholding:

bernsen thresholding1

Generalized Bernsen Thresholding:

generalized bernsen thresholding1

Otsu Thresholding:

otsu thresholding1

Riddler−Calvard Thresholding:

riddler calvard thresholding1

Soft Thresholding:

soft thresholding1

在剩余章节中,我们将详细讨论所有阈值化技巧。