Mahotas 简明教程

Mahotas - Bernsen Local Thresholding

Bernsen 局部阈值化是一种用于将图像分割成前景和背景区域的技术。它利用局部邻域中的亮度变化为图像中每个像素分配阈值。

局部邻域的大小是使用窗口来确定的。在阈值化过程中,较大的窗口大小会考虑更多相邻像素,从而可以在不同区域之间创建平滑的过渡,但也去除了更精细的细节。

另一方面,较小的窗口大小能捕获更多细节,但可能容易受到噪声的影响。

Bernsen 局部阈值化与其他阈值化技术之间的区别在于:Bernsen 局部阈值化使用动态阈值,而其他阈值化技术则使用单个阈值来分离前景和背景区域。

Bernsen Local Thresholding in Mahotas

在 Mahotas 中,我们可以使用 thresholding.bernsen()thresholding.gbernsen() 函数对图像应用 Bernsen 局部阈值化。这些函数会创建一个固定大小的窗口,并计算窗口内每个像素的局部对比度范围,以对图像进行分割。

局部对比度范围是窗口内的最小灰度值与最大灰度值。

然后,阈值会被计算为最小灰度值与最大灰度值的平均值。如果像素亮度高于阈值,则将其分配给前景(白色),否则将其分配给背景(黑色)。

然后,窗口移动到整个图像中,覆盖所有像素以创建二值图像,其中前景和背景区域根据局部亮度变化进行分离。

The mahotas.tresholding.bernsen() function

mahotas.thresholding.bernsen() 函数以灰度图像作为输入,并在其上应用 Bernsen 局部阈值化。它输出一张图像,其中每个像素都分配了一个值 0(黑色)或 255(白色)。

前景像素对应于图像中具有较高亮度的区域,而背景像素对应于图像中具有较低亮度的区域。

以下是 mahotas 中 bernsen() 函数的基本语法:

mahotas.thresholding.bernsen(f, radius, contrast_threshold, gthresh={128})

其中,

  1. f − 它是输入的灰度图像。

  2. radius − 它是每个像素周围窗口的大小。

  3. contrast_threshold − 它是局部阈值。

  4. gthresh (optional) − 它是全局阈值(默认值为 128)。

以下示例显示了 mh.thresholding.bernsen() 函数在图像上应用 Bernsen 局部阈值化的用法。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Creating Bernsen threshold image
threshold_image = mh.thresholding.bernsen(image, 5, 200)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the threshold image
axes[1].imshow(threshold_image, cmap='gray')
axes[1].set_title('Bernsen Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

以下是上面代码的输出: -

bernsen local thresholding

The mahotas.thresholding.gbernsen() function

mahotas.thresholding.gbernsen() 函数还会对输入灰度图像应用 Bernsen 局部阈值化。

这是 Bernsen 局部阈值算法的泛化版本。它输出一个分割后的图像,其中每个像素被分配一个值 0 或 255,具体取决于它是背景还是前景。

gbernsen() 和 bernsen() 函数之间的区别在于 gbernsen() 函数使用结构元素来定义局部邻域,而 bernsen() 函数使用固定大小的窗口来定义像素周围的局部邻域。

此外,gbernsen() 根据对比度阈值和全局阈值计算阈值,而 bernsen() 仅使用对比度阈值来计算每个像素的阈值。

以下是 mahotas 中 gbernsen() 函数的基本语法 −

mahotas.thresholding.gbernsen(f, se, contrast_threshold, gthresh)

其中,

  1. f − 它是输入的灰度图像。

  2. se − 这是结构元素。

  3. contrast_threshold − 它是局部阈值。

  4. gthresh (optional) − 这是全局阈值。

在此示例中,我们使用 mh.thresholding.gbernsen() 函数对图像应用泛化的 Bernsen 局部阈值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Creating a structuring element
structuring_element = np.array([[0, 0, 0],[0, 0, 0],[1, 1, 1]])
# Creating generalized Bernsen threshold image
threshold_image = mh.thresholding.gbernsen(image, structuring_element, 200,
128)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the threshold image
axes[1].imshow(threshold_image, cmap='gray')
axes[1].set_title('Generalized Bernsen Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

bernsen local thresholding1

Bernsen Local Thresholding using Mean

我们可以使用像素强度的平均值作为阈值来应用 Bernsen 局部阈值。它指的是图像的平均强度,通过对所有像素的强度值求和然后将其除以像素总数来计算。

在 mahotas 中,我们可以先使用 numpy.mean() 函数找到所有像素的平均像素强度来实现这一点。然后,我们定义一个窗口大小来取得像素的局部邻域。

最后,我们将平均值设置为阈值,方法是将其传递给 bernsen() 或 gbernsen() 函数的 contrast_threshold 参数。

Example

在此,我们对图像应用 Bernsen 局部阈值,其中阈值是所有像素强度的平均值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Calculating mean pixel value
mean = np.mean(image)
# Creating bernsen threshold image
threshold_image = mh.thresholding.bernsen(image, 15, mean)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the threshold image
axes[1].imshow(threshold_image, cmap='gray')
axes[1].set_title('Bernsen Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

执行上面的代码后,我们得到以下输出: -

bernsen local thresholding mean