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
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
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.
Syntax
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.
Example
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()
Output
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.
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
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.
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.
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()
Output
After executing the above code, we get the following output −