Mahotas 简明教程
Mahotas - Setting Threshold
设置阈值是指为图像定义一个阈值,以执行图像阈值化。图像阈值化是将灰度图像转换为二进制图像的过程,其中像素分为两类之一——前景或背景。
强度值高于阈值的像素被分配到前景,而低于阈值的像素被分配到背景类别。
阈值范围从 0 到 255,其中值 0 产生仅有前景(白色)的图像,而值 255 产生仅有背景(黑色)的图像。
Setting Threshold in Mahotas
在 Mahotas 中,我们可以使用 numpy.mean() 函数设置图像的阈值。
此函数将灰度图像作为输入,并计算其像素的平均强度值。
然后将平均值设置为阈值。任何强度超过阈值的像素都被归类为前景,而强度低于阈值的像素都被归类为背景。
Note − Mahotas 没有提供设置阈值的直接方法,但可以通过使用 mahotas 和 numpy 来实现。
Example
在以下示例中,我们使用 np.mean() 函数设置灰度图像的阈值。
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()
以下是上面代码的输出: -
Set an Inverse Threshold
我们还可为图像设置反转阈值。在反转阈值中,强度高于阈值 的像素被归类为背景,而强度低于阈值的像素被归类为前景。
在 mahotas 中,可通过两个步骤执行反转阈值化。第一步是使用 numpy.mean() 函数计算 图像的阈值。
第二步是使用小于运算符 (<) 将 图像的像素强度与平均阈值进行比较,在正常阈值化中 使用大于运算符 (>)。
Example
以下示例说明如何从灰度图像创建反转阈值图像。
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()
上述代码的输出如下:
Set a Custom Threshold value
设置阈值 的另一种方法是为图像设置自定义阈值。这是一个完全基于图像的随机数。
自定义阈值是未使用任何数学公式计算出的任意数字。这就是不应该使用自定义值为阈值的原因。
另一个原因是自定义值可能会产生噪声大得多的图像。
在 mahotas 中,可以指定任意数字来设置自定义阈值。然后我们可以使用此值并比较图像像素来生成阈值图像。
Note - 设置为 0 或 255 的阈值将导致最终图像仅包含前景色素或背景像素。
Example
在此,我们已将任意数字设置为阈值。
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()
执行上面的代码后,我们得到以下输出: -