
- Mahotas - Home
- Mahotas - Introduction
- Mahotas - Computer Vision
- Mahotas - History
- Mahotas - Features
- Mahotas - Installation
- Mahotas Handling Images
- Mahotas - Handling Images
- Mahotas - Loading an Image
- Mahotas - Loading Image as Grey
- Mahotas - Displaying an Image
- Mahotas - Displaying Shape of an Image
- Mahotas - Saving an Image
- Mahotas - Centre of Mass of an Image
- Mahotas - Convolution of Image
- Mahotas - Creating RGB Image
- Mahotas - Euler Number of an Image
- Mahotas - Fraction of Zeros in an Image
- Mahotas - Getting Image Moments
- Mahotas - Local Maxima in an Image
- Mahotas - Image Ellipse Axes
- Mahotas - Image Stretch RGB
- Mahotas Color-Space Conversion
- Mahotas - Color-Space Conversion
- Mahotas - RGB to Gray Conversion
- Mahotas - RGB to LAB Conversion
- Mahotas - RGB to Sepia
- Mahotas - RGB to XYZ Conversion
- Mahotas - XYZ to LAB Conversion
- Mahotas - XYZ to RGB Conversion
- Mahotas - Increase Gamma Correction
- Mahotas - Stretching Gamma Correction
- Mahotas Labeled Image Functions
- Mahotas - Labeled Image Functions
- Mahotas - Labeling Images
- Mahotas - Filtering Regions
- Mahotas - Border Pixels
- Mahotas - Morphological Operations
- Mahotas - Morphological Operators
- Mahotas - Finding Image Mean
- Mahotas - Cropping an Image
- Mahotas - Eccentricity of an Image
- Mahotas - Overlaying Image
- Mahotas - Roundness of Image
- Mahotas - Resizing an Image
- Mahotas - Histogram of Image
- Mahotas - Dilating an Image
- Mahotas - Eroding Image
- Mahotas - Watershed
- Mahotas - Opening Process on Image
- Mahotas - Closing Process on Image
- Mahotas - Closing Holes in an Image
- Mahotas - Conditional Dilating Image
- Mahotas - Conditional Eroding Image
- Mahotas - Conditional Watershed of Image
- Mahotas - Local Minima in Image
- Mahotas - Regional Maxima of Image
- Mahotas - Regional Minima of Image
- Mahotas - Advanced Concepts
- Mahotas - Image Thresholding
- Mahotas - Setting Threshold
- Mahotas - Soft Threshold
- Mahotas - Bernsen Local Thresholding
- Mahotas - Wavelet Transforms
- Making Image Wavelet Center
- Mahotas - Distance Transform
- Mahotas - Polygon Utilities
- Mahotas - Local Binary Patterns
- Threshold Adjacency Statistics
- Mahotas - Haralic Features
- Weight of Labeled Region
- Mahotas - Zernike Features
- Mahotas - Zernike Moments
- Mahotas - Rank Filter
- Mahotas - 2D Laplacian Filter
- Mahotas - Majority Filter
- Mahotas - Mean Filter
- Mahotas - Median Filter
- Mahotas - Otsu's Method
- Mahotas - Gaussian Filtering
- Mahotas - Hit & Miss Transform
- Mahotas - Labeled Max Array
- Mahotas - Mean Value of Image
- Mahotas - SURF Dense Points
- Mahotas - SURF Integral
- Mahotas - Haar Transform
- Highlighting Image Maxima
- Computing Linear Binary Patterns
- Getting Border of Labels
- Reversing Haar Transform
- Riddler-Calvard Method
- Sizes of Labelled Region
- Mahotas - Template Matching
- Speeded-Up Robust Features
- Removing Bordered Labelled
- Mahotas - Daubechies Wavelet
- Mahotas - Sobel Edge Detection
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
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 −
