Mahotas 简明教程
Mahotas - Soft Threshold
软阈值是指降低图像的噪声(去噪)以提高其质量。
Soft threshold refers to decreasing the noise (denoising) of an image to improve its quality.
它根据像素靠近阈值值的程度,向像素分配连续的值域。这导致前景区域和背景区域之间的平滑过渡。
It assigns a continuous range of values to the pixels based on their closeness to the threshold value. This results in a gradual transition between the foreground and background regions.
在软阈值中,阈值确定去噪和图像保留之间的平衡。较高的阈值导致更强的去噪,但会导致信息丢失。
In soft threshold, the threshold value determines the balance between denoising and image preservation. A higher threshold value results in stronger denoising but leads to loss of information.
相反,较低的阈值保留更多信息,但会导致不必要的噪声。
Conversely, a lower threshold value retains more information but results in unwanted noise.
Soft Threshold in Mahotas
在 Mahotas 中,我们可以使用 thresholding.soft_threshold() 函数对图像应用软阈值。它根据相邻像素动态调整阈值,以增强具有非均匀噪声级别的图像。
In Mahotas, we can use the thresholding.soft_threshold() function to apply a soft threshold on an image. It dynamically adjusts the threshold value based on the neighboring pixels to enhance images with non−uniform noise levels.
通过使用动态调整,该函数按比例降低灰度值超过阈值像素的强度,并将它们分配给前景。
By using dynamic adjustment, the function proportionally reduces intensity of those pixels whose intensity exceeds the threshold value and assigns them to the foreground.
另一方面,如果像素强度低于阈值,则将其分配给背景。
On the other hand, if a pixel’s intensity is below the threshold it is assigned to the background.
The mahotas.thresholding.soft_threshold() function
mahotas.thresholding.soft_threshold() 函数以灰度图像作为输入,并返回应用了软阈值的图像。它通过将像素强度与提供的阈值进行比较来工作。
The mahotas.thresholding.soft_threshold() function takes a grayscale image as input and returns an image on which soft threshold has been applied. It works by comparing the pixel intensity with the provided threshold value.
以下是 mahotas 中的 soft_threshold() 函数的基本语法 −
Following is the basic syntax of the soft_threshold() function in mahotas −
mahotas.thresholding.soft_threshold(f, tval)
其中,
Where,
-
f − It is the input grayscale image.
-
tval − It is the threshold value.
在以下示例中,我们正在使用 mh.thresholding.soft_threshold() 函数对灰度图像应用软阈值。
In the following example, we are applying a soft threshold on a grayscale image using the mh.thresholding.soft_threshold() function.
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)
# Setting threshold value
tval = 150
# Applying soft threshold on the image
threshold_image = mh.thresholding.soft_threshold(image, tval)
# 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('Soft 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 −
Soft Threshold using the Mean value
我们可以使用图像上像素强度的平均值来应用软阈值。平均值是指图像的平均强度。
We can apply a soft threshold using the mean value of the pixel intensities on an image. The mean value refers to the average intensity of an image.
它通过对所有像素的强度值求和,然后除以像素总数来计算。
It is calculated by summing the intensity value of all pixels and then dividing it by the total number of pixels.
在 mahotas 中,我们可以使用 numpy.mean() 函数查找图像中所有像素的平均像素强度。然后可以将平均值传递给 mahotas.thresholding.soft_threshold() 函数的 tval 参数,以生成软阈值图像。
In mahotas, we can find the mean pixel intensities of all the pixels of an image using the numpy.mean() function. The mean value can then be passed to the tval parameter of mahotas.thresholding.soft_threshold() function to generate a soft threshold image.
这种应用软阈值的方法在去噪和图像质量之间保持了良好的平衡,因为阈值既不高也不低。
This approach of applying soft threshold maintains a good balance between denoising and image quality as the threshold value is neither too high nor too low.
Example
以下示例显示了在阈值是像素强度平均值时对灰度图像应用软阈值。
The following example shows a soft threshold being applied on a grayscale image when the threshold is the mean value of pixel intensities.
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)
# Setting mean threshold value
tval = np.mean(image)
# Applying soft threshold on the image
threshold_image = mh.thresholding.soft_threshold(image, tval)
# 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('Soft 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 −
Soft Threshold using the Percentile value
除了平均值,我们还可以使用图像像素强度上的百分位值来应用软阈值。百分位数是指给定百分比的数据低于该值的;在图像处理中,它指的是图像中像素强度的分布。
In addition to mean value, we can also apply a soft threshold using the percentile value on the pixel’s intensities of an image. Percentile refers to the value below which a given percentage of data falls; in image processing it refers to distribution of pixel intensities in an image.
例如,我们将阈值百分位数设置为 85。这意味着只有强度大于图像中其他像素强度 85% 的像素才会被归类为前景,而其余像素将被归类为背景。
For example, let’s set the threshold percentile to 85. This means that only pixels with intensities greater than 85% of the other pixels in the image will be classified as foreground, while the remaining pixels will be classified as background.
在 mahotas 中,我们可以使用 numpy.percentile() 函数根据像素强度的百分位数设置阈值。然后将此值用于 soft_thresholding() 函数以对图像应用软阈值。
In mahotas, we can use numpy.percentile() function to set a threshold value based on a percentile of pixel intensity. This value is then used in the soft_thresholding() function to apply a soft threshold on an image.
Example
在这个示例中,我们展示了在使用百分位数找到阈值时如何应用软阈值。
In this example, we show how soft threshold is applied when threshold is found using percentile value.
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)
# Setting percentile threshold value
tval = np.percentile(image, 85)
# Applying soft threshold on the image
threshold_image = mh.thresholding.soft_threshold(image, tval)
# 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('Soft 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 −