Mahotas 简明教程

Mahotas - Otsu’s Method

大津法是用于图像分割以将前景与背景分开的技术。它的工作方式是找到最大化类间方差的阈值。

Otsu’s method is a technique used for image segmentation to separate the foreground from the background. It works by finding a threshold value that maximizes the inter−class variance.

类间方差是对前景区域和背景区域之间分离程度的度量。

The inter−class variance is a measure of the separation between the foreground and the background regions.

最大化类间方差的阈值被认为是图像分割的最佳阈值。

The threshold value that maximizes the inter−class variance is considered the optimal threshold value for image segmentation.

Otsu’s Method in Mahotas

在 Mahotas 中,我们可以利用 thresholding.otsu() 函数使用大津法计算阈值。该函数以下列方式运行−

In Mahotas, we can utilize the thresholding.otsu() function to calculate the threshold value using Otsu’s method. The function operates in the following manner −

  1. First it finds the histogram of the image. The histogram is a plot of the number of pixels in the image at each grayscale level.

  2. Next, the threshold value is set to the average grayscale value of the image.

  3. Then, the inter−class variance is calculated for the current threshold value.

  4. The threshold value is then increased, and the inter−class variance is recalculated.

重复步骤 2 到 4,直到达到最佳阈值。

Steps 2 to 4 are repeated until an optimal threshold value is reached.

The mahotas.thresholding.otsu() function

mahotas.thresholding.otsu() 函数接受灰度图像作为输入,并返回使用大津方法计算出的阈值。然后将灰度图像的像素与阈值进行比较,以创建分段图像。

The mahotas.thresholding.otsu() function takes a grayscale image as input and returns its threshold value calculated using Otsu’s method. The pixels of the grayscale image are then compared to the threshold value to create a segmented image.

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

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

mahotas.thresholding.otsu(img, ignore_zeros=False)

其中,

Where,

  1. img − It is the input grayscale image.

  2. ignore_zeros (optional) − It a flag which specifies whether to ignore zero valued pixels (default is false).

在以下示例中,我们使用 mh.thresholding.otsu() 函数来查找阈值。

In the following example, we are using mh.thresholding.otsu() function to find the threshold value.

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sea.bmp')
# Converting it to grayscale
image = mh.colors.rgb2gray(image).astype(np.uint8)
# Calculating threshold value using Otsu method
otsu_threshold = mh.thresholding.otsu(image)
# Creating image from the threshold value
final_image = image > otsu_threshold
# 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(final_image, cmap='gray')
axes[1].set_title('Otsu 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 −

otsus method

Ignoring the Zero Valued Pixels

我们还可以通过忽略值为零的像素来查找大津阈值。值为零的像素是强度值为 0 的像素。

We can also find Otsu’s threshold value by ignoring the zero valued pixels. Zero valued pixels are pixels that have an intensity value of 0.

它们通常表示图像的背景像素,但在某些图像中,它们也可能表示噪声。

They usually represent the background pixels of an image, but in some images, they may also represent noise.

在灰度图像中,零值像素是表示为颜色“黑色”的像素。

In grayscale images, zero valued pixels are pixels represented by the color 'black'.

若要排除 mahotas 中的值为零的像素,可以将 ignore_zeros 参数设置为布尔值“True”。

To exclude zero valued pixels in mahotas, we can set the ignore_zeros parameter to the boolean value 'True'.

Example

在以下提到的示例中,在使用大津方法计算阈值时,我们忽略值为零的像素。

In the example mentioned below, we are ignoring pixels with the value zero when calculating the threshold value using Otsu’s method.

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image).astype(np.uint8)
# Calculating threshold value using Otsu method
otsu_threshold = mh.thresholding.otsu(image, ignore_zeros=True)
# Creating image from the threshold value
final_image = image > otsu_threshold
# 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(final_image, cmap='gray')
axes[1].set_title('Otsu 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 −

zero valued pixels