Mahotas 简明教程
Mahotas - Setting Threshold
设置阈值是指为图像定义一个阈值,以执行图像阈值化。图像阈值化是将灰度图像转换为二进制图像的过程,其中像素分为两类之一——前景或背景。
Setting threshold refers to defining a threshold value for an image to perform image thresholding. Image thresholding is the conversion of a grayscale image into a binary image, where the pixels are classified into one of two categories − foreground or background.
强度值高于阈值的像素被分配到前景,而低于阈值的像素被分配到背景类别。
The pixels with intensity values above the threshold value are assigned to the foreground, while those below are assigned to the background class.
阈值范围从 0 到 255,其中值 0 产生仅有前景(白色)的图像,而值 255 产生仅有背景(黑色)的图像。
Threshold value ranges from 0 to 255, where value 0 produces image with only foreground (white) and value 255 produces image with only background (black).
Setting Threshold in Mahotas
在 Mahotas 中,我们可以使用 numpy.mean() 函数设置图像的阈值。
In Mahotas, we can set threshold value of an image using the numpy.mean() function.
此函数将灰度图像作为输入,并计算其像素的平均强度值。
This function takes a grayscale image as input and calculates the average intensity value of its pixels.
然后将平均值设置为阈值。任何强度超过阈值的像素都被归类为前景,而强度低于阈值的像素都被归类为背景。
The mean value is then set as the threshold value. Any pixels whose intensity exceeds the threshold value are classified as foreground, while the pixels whose intensity is below the threshold value are classified as background.
Note − Mahotas 没有提供设置阈值的直接方法,但可以通过使用 mahotas 和 numpy 来实现。
Note − Mahotas does not provide a direct way for setting threshold, however it can be achieved by using mahotas with numpy.
Syntax
以下是 numpy 中 mean() 函数的基本语法−
Following is the basic syntax of the mean() function in numpy −
numpy.mean(image)
其中,
Where,
-
image − It is the input grayscale image.
Example
在以下示例中,我们使用 np.mean() 函数设置灰度图像的阈值。
In the following example, we are setting the threshold value of a grayscale image using np.mean() function.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Calculating mean intensity value
mean_value = np.mean(image)
# Creating threshold image
threshold_image = image > mean_value
# 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('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 −
Set an Inverse Threshold
我们还可为图像设置反转阈值。在反转阈值中,强度高于阈值 的像素被归类为背景,而强度低于阈值的像素被归类为前景。
We can also set an inverse threshold for an image. In inverse threshold, the pixels whose intensity is greater than the threshold value are classified as background, while the pixels whose intensity is less than the threshold value are classified as foreground.
在 mahotas 中,可通过两个步骤执行反转阈值化。第一步是使用 numpy.mean() 函数计算 图像的阈值。
In mahotas, inverse thresholding can be done in two steps. The first step is to calculate the threshold value of the image using numpy.mean() function.
第二步是使用小于运算符 (<) 将 图像的像素强度与平均阈值进行比较,在正常阈值化中 使用大于运算符 (>)。
The second step is to compare the image’s pixel intensity with the mean threshold value using the less−than operator (<) instead of the greater−than operator (>) used in normal thresholding.
Example
以下示例说明如何从灰度图像创建反转阈值图像。
The following example shows a creation of an inverse threshold image from a grayscale image.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Calculating mean intensity value
mean_value = np.mean(image)
# Creating inverse threshold image
threshold_image = image < mean_value
# 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('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 −
Set a Custom Threshold value
设置阈值 的另一种方法是为图像设置自定义阈值。这是一个完全基于图像的随机数。
Another way to set a threshold value is to set a custom threshold value for an image. It is a random number decided purely based on the image.
自定义阈值是未使用任何数学公式计算出的任意数字。这就是不应该使用自定义值为阈值的原因。
Custom threshold is an arbitrary number not calculated using any mathematical formula. This is the reason why a custom value should not be used as threshold value.
另一个原因是自定义值可能会产生噪声大得多的图像。
Another reason being that a custom value may produce an image with significantly more noise.
在 mahotas 中,可以指定任意数字来设置自定义阈值。然后我们可以使用此值并比较图像像素来生成阈值图像。
In mahotas, we can assign an arbitrary number to set a custom threshold value. We can then use this value and compare the image’s pixel to generate a threshold image.
Note - 设置为 0 或 255 的阈值将导致最终图像仅包含前景色素或背景像素。
Note − Setting a threshold value of either 0 or 255 will result in the final image consisting of only either the foreground pixels or the background pixels.
Example
在此,我们已将任意数字设置为阈值。
Here, we have set an arbitrary number as the threshold value.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Setting threshold value
threshold_value = 200
# Creating threshold image
threshold_image = image > threshold_value
# 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('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 −