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

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

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
Advertisements