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

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

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

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

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

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

Generalized Bernsen Thresholding:

Generalized Bernsen Thresholding1

Otsu Thresholding:

Otsu Thresholding1

Riddler−Calvard Thresholding:

Riddler-Calvard Thresholding1

Soft Thresholding:

Soft Thresholding1

We will discuss about all the thresholding techniques in detail in the remaining chapters.

Advertisements