Mahotas 简明教程
Mahotas - Image Thresholding
图像阈值化是根据像素强度将感兴趣区域与背景分离开来的技术。它包括设置一个阈值,该阈值将图像分为前景和背景。
Image thresholding is the technique of separating regions of interest from the background based on pixel intensities. It involves setting a threshold value, which divides the image between the foreground and the background.
强度高于阈值的像素被分类为前景,而低于阈值的像素被分类为背景。
Pixels with intensities above the threshold are classified as the foreground, while those below the threshold are classified as the background.
这种二进制分离允许进一步的分析,如物体检测、分割或特征提取。图像阈值化可以应用于各种类型的图像,包括灰度图像和彩色图像,以简化后续处理和分析任务。
This binary separation allows for further analysis, such as object detection, segmentation, or feature extraction. Image thresholding can be applied to various types of images, including grayscale and color images, to simplify subsequent processing and analysis tasks.
使用图像阈值化涉及选择一个合适的阈值并将其应用于图像。可以利用各种阈值化技术计算阈值。
The use of image thresholding involves selecting an appropriate threshold value and applying it to the image. The threshold value can be calculated using various thresholding techniques.
阈值化方法的选择取决于图像属性、噪声水平和预期结果等因素。
The choice of the thresholding method depends on factors such as image properties, noise levels, and desired results.
在这里,我们简要讨论了每种技术。我们将后面章节中讨论深入信息。
Here, we have discussed each technique in brief. In−depth information is discussed in the later chapters.
让我们看看可以在 mahotas 中执行的每种阈值化技术−
Let us look at each thresholding technique which can be done in mahotas −
Bernsen Thresholding
伯恩森阈值化是一种阈值化技术,它将灰度图像中的前景与背景分离开来。它根据每个像素周围邻域内的最大和最小像素强度计算局部阈值。
Bernsen thresholding is a thresholding technique that separates the foreground from the background in grayscale images. It calculates a local threshold value based on the maximum and minimum pixel intensities within a neighborhood around each pixel.
如果像素的强度更接近最大值,则将其视为前景的一部分;否则,则将其视为背景的一部分。
If the pixel’s intensity is closer to the maximum value, it is considered part of the foreground; otherwise, it is considered as part of the background.
让我们看看下面的伯恩森阈值图像−
Let’s see the Bernsen threshold image below −
Generalized Bernsen Thresholding
广义伯恩森阈值化是伯恩森阈值化方法的改进。它考虑邻域内更广泛的像素强度,而不仅仅是最大和最小强度值。
Generalized Bernsen thresholding is an improvement of the Bernsen thresholding method. It considers a wider range of pixel intensities within the neighborhood, instead of only the maximum and minimum intensity values.
下图显示了广义伯恩森阈值图像−
The following images show generalized Bernsen threshold image −
Otsu Thresholding
大津阈值化技术自动确定将前景与背景分离开来的最佳阈值。
Otsu thresholding technique automatically determines the optimal threshold value to separate the foreground from the background.
大津的方法迭代地检查所有可能的阈值,并选择使类间方差最大化的阈值。
Otsu’s method iteratively examines all possible threshold values and selects the threshold value that maximizes the betweenclass variance.
让我们看看下列大津阈值化图像−
Let’s look at Otsu threshold image below −
Riddler-Calvard Thresholding
Riddler−Calvard 阈值化也自动确定最佳阈值。它基于前景和背景方差的加权和的最小化。
Riddler−Calvard thresholding also automatically determines the optimal threshold value. It is based on the minimization of the weighted sum of the foreground and the background variances.
让我们看看下面的 Riddler−Calvard 阈值图像−
Let us look at the Riddler−Calvard threshold image below −
Soft Thresholding
软阈值化是用于图像去噪的技术。它将强度值小于某个阈值的像素设置为零。软阈值化在减少噪声的同时保留图像的重要结构信息。
Soft thresholding is a technique used in image denoising. It sets pixels with intensity value less than a certain threshold to zero. Soft thresholding preserves the important structural information of the image while reducing noise.
下图显示了软阈值化 −
The following image shows soft thresholding −
Example
在以下示例中,我们尝试执行所有以上解释过的阈值化技巧 −
In the following example, we are trying to perform all the above explained thresholding techniques −
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()
获得的输出如下所示 −
The output obtained is as shown below −
Bernsen Thresholding:
Bernsen Thresholding:
Generalized Bernsen Thresholding:
Generalized Bernsen Thresholding:
Otsu Thresholding:
Otsu Thresholding:
Riddler−Calvard Thresholding:
Riddler−Calvard Thresholding:
Soft Thresholding:
Soft Thresholding:
在剩余章节中,我们将详细讨论所有阈值化技巧。
We will discuss about all the thresholding techniques in detail in the remaining chapters.