![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 - Image Thresholding
Image thresholding is the technique of separating regions of interest from the background based on pixel intensities. It involves setting a threshold value, which divides the image between the foreground and the background.
Pixels with intensities above the threshold are classified as the foreground, while those below the threshold are classified as the background.
This binary separation allows for further analysis, such as object detection, segmentation, or feature extraction. Image thresholding can be applied to various types of images, including grayscale and color images, to simplify subsequent processing and analysis tasks.
The use of image thresholding involves selecting an appropriate threshold value and applying it to the image. The threshold value can be calculated using various thresholding techniques.
The choice of the thresholding method depends on factors such as image properties, noise levels, and desired results.
Here, we have discussed each technique in brief. In−depth information is discussed in the later chapters.
Let us look at each thresholding technique which can be done in mahotas −
Bernsen Thresholding
Bernsen thresholding is a thresholding technique that separates the foreground from the background in grayscale images. It calculates a local threshold value based on the maximum and minimum pixel intensities within a neighborhood around each pixel.
If the pixel's intensity is closer to the maximum value, it is considered part of the foreground; otherwise, it is considered as part of the background.
Let’s see the Bernsen threshold image below −
![Bernsen Thresholding](/mahotas/images/bernsen_thresholding.jpg)
Generalized Bernsen Thresholding
Generalized Bernsen thresholding is an improvement of the Bernsen thresholding method. It considers a wider range of pixel intensities within the neighborhood, instead of only the maximum and minimum intensity values.
The following images show generalized Bernsen threshold image −
![Generalized Bernsen Thresholding](/mahotas/images/generalized_bernsen_thresholding.jpg)
Otsu Thresholding
Otsu thresholding technique automatically determines the optimal threshold value to separate the foreground from the background.
Otsu's method iteratively examines all possible threshold values and selects the threshold value that maximizes the betweenclass variance.
Let's look at Otsu threshold image below −
![Otsu Thresholding](/mahotas/images/otsu_thresholding.jpg)
Riddler-Calvard Thresholding
Riddler−Calvard thresholding also automatically determines the optimal threshold value. It is based on the minimization of the weighted sum of the foreground and the background variances.
Let us look at the Riddler−Calvard threshold image below −
![Riddler-Calvard Thresholding](/mahotas/images/riddler_calvard_thresholding.jpg)
Soft Thresholding
Soft thresholding is a technique used in image denoising. It sets pixels with intensity value less than a certain threshold to zero. Soft thresholding preserves the important structural information of the image while reducing noise.
The following image shows soft thresholding −
![Soft Thresholding](/mahotas/images/soft_thresholding.jpg)
Example
In the following example, we are trying to perform all the above explained thresholding techniques −
import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt image = mh.imread('sea.bmp', as_grey=True) # Bernsen thresholding bernsen = mh.thresholding.bernsen(image, 5, 5) mtplt.imshow(bernsen) mtplt.title('Bernsen Thresholding') mtplt.axis('off') mtplt.show() # Generalized Bernsen thresholding gbernsen = mh.thresholding.gbernsen(image, mh.disk(3), 10, 109) mtplt.imshow(gbernsen) mtplt.title('Generalized Bernsen Thresholding') mtplt.axis('off') mtplt.show() # Otsu threshold int_image_otsu = image.astype(np.uint8) otsu = mh.otsu(int_image_otsu) result_image = image > otsu mtplt.imshow(result_image) mtplt.title('Otsu Thresholding') mtplt.axis('off') mtplt.show() # Riddler-Calvard threshold int_image_rc = image.astype(np.uint8) rc = mh.thresholding.rc(int_image_rc) final_image = image > rc mtplt.imshow(final_image) mtplt.title('RC Thresholding') mtplt.axis('off') mtplt.show() # Soft threshold soft = mh.thresholding.soft_threshold(image, np.mean(image)) mtplt.imshow(soft) mtplt.title('Soft Thresholding') mtplt.axis('off') mtplt.show()
Output
The output obtained is as shown below −
Bernsen Thresholding:
![Bernsen Thresholding1](/mahotas/images/bernsen_thresholding1.jpg)
Generalized Bernsen Thresholding:
![Generalized Bernsen Thresholding1](/mahotas/images/generalized_bernsen_thresholding1.jpg)
Otsu Thresholding:
![Otsu Thresholding1](/mahotas/images/otsu_thresholding1.jpg)
Riddler−Calvard Thresholding:
![Riddler-Calvard Thresholding1](/mahotas/images/riddler_calvard_thresholding1.jpg)
Soft Thresholding:
![Soft Thresholding1](/mahotas/images/soft_thresholding1.jpg)
We will discuss about all the thresholding techniques in detail in the remaining chapters.