![Mahotas](/mahotas/images/mahotas-mini-logo.jpg)
- Mahotas Tutorial
- 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 - 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.
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
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 does not provide a direct way for setting threshold, however it can be achieved by using mahotas with numpy.
Syntax
Following is the basic syntax of the mean() function in numpy −
numpy.mean(image)
Where,
image − It is the input grayscale image.
Example
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()
Output
Following is the output of the above code −
![Setting Threshold](/mahotas/images/setting_threshold.jpg)
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.
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
Output of the above code is as follows −
![Inverse Threshold](/mahotas/images/inverse_threshold.jpg)
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.
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 − 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()
Output
After executing the above code, we get the following output −
![Custom Threshold Value](/mahotas/images/custom_threshold_value.jpg)