Mahotas 简明教程

Mahotas - Bernsen Local Thresholding

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

Bernsen local thresholding is a technique used for segmenting images into foreground and background regions. It uses intensity variations in a local neighborhood to assign a threshold value to each pixel in an image.

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

The size of the local neighborhood is determined using a window. A large window size considers more neighboring pixels in the thresholding process, creating smoother transitions between regions but removing finer details.

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

On the other hand, a small window size captures more details but may be susceptible to noise.

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

The difference between Bernsen local thresholding and other thresholding techniques is that Bernsen local thresholding uses a dynamic threshold value, while the other thresholding techniques use a single threshold value to separate foreground and background regions.

Bernsen Local Thresholding in Mahotas

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

In Mahotas, we can use the thresholding.bernsen() and thresholding.gbernsen() functions to apply Bernsen local thresholding on an image. These functions create a window of a fixed size and calculate the local contrast range of each pixel within the window to segment the image.

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

The local contrast range is the minimum and maximum grayscale values within the window.

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

The threshold value is then calculated as the average of minimum and maximum grayscale values. If the pixel intensity is higher than the threshold, it is assigned to the foreground (white), otherwise it is assigned to the background (black).

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

The window is then moved across the image to cover all the pixels to create a binary image, where the foreground and background regions are separated based on local intensity variations.

The mahotas.tresholding.bernsen() function

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

The mahotas.thresholding.bernsen() function takes a grayscale image as input and applies Bernsen local thresholding on it. It outputs an image where each pixel is assigned a value of 0 (black) or 255 (white).

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

The foreground pixels correspond to the regions of the image that have a relatively high intensity, whereas the background pixels correspond to the regions of the image that have a relatively low intensity.

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

Following is the basic syntax of the bernsen() function in mahotas −

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

其中,

where,

  1. f − It is the input grayscale image.

  2. radius − It is the size of the window around each pixel.

  3. contrast_threshold − It is the local threshold value.

  4. gthresh (optional) − It is the global threshold value (default is 128).

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

The following example shows the usage of mh.thresholding.bernsen() function to apply Bernsen local thresholding on an image.

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()

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

Following is the output of the above code −

bernsen local thresholding

The mahotas.thresholding.gbernsen() function

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

The mahotas.thresholding.gbernsen() function also applies Bernsen local thresholding on an input grayscale image.

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

It is the generalized version of Bernsen local thresholding algorithm. It outputs a segmented image where each pixel is assigned a value of 0 or 255 depending on whether it is background or foreground.

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

The difference between gbernsen() and bernsen() function is that the gbernsen() function uses a structuring element to define the local neighborhood, while the bernsen() function uses a window of fixed size to define the local neighborhood around a pixel.

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

Also, gbernsen() calculates the threshold value based on the contrast threshold and the global threshold, while bernsen() only uses the contrast threshold to calculate the threshold value of each pixel.

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

Following is the basic syntax of the gbernsen() function in mahotas −

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

其中,

Where,

  1. f − It is the input grayscale image.

  2. se − It is the structuring element.

  3. contrast_threshold − It is the local threshold value.

  4. gthresh (optional) − It is the global threshold value.

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

In this example, we are using mh.thresholding.gbernsen() function to apply generalized Bernsen local thresholding on an image.

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()

上述代码的输出如下:

Output of the above code is as follows −

bernsen local thresholding1

Bernsen Local Thresholding using Mean

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

We can apply Bernsen local thresholding using the mean value of the pixel intensities as the threshold value. It refers to the average intensity of an image and is calculated by summing the intensity value of all pixels and then dividing it by the total number of pixels.

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

In mahotas, we can do this by first finding the mean pixel intensities of all the pixels using the numpy.mean() function. Then, we define a window size to get the local neighborhood of a pixel.

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

Finally, we set the mean value as the threshold value by passing it to the to the contrast_threshold parameter of the bernsen() or gbernsen() function.

Example

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

Here, we are applying Bernsen local thresholding on an image where threshold value is the mean value of all pixel intensities.

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()

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

After executing the above code, we get the following output −

bernsen local thresholding mean