What is multi-Otsu thresholding?

Multi-Otsu thresholding is an extension of Otsu’s thresholding method, which is used to segment an image into multiple classes or regions based on pixel intensity values. While Otsu’s method finds a single threshold to divide the image into two classes (typically foreground and background), multi-Otsu thresholding extends this to divide the image into more than two classes or regions, effectively segmenting the image into multiple levels of intensity. This is particularly useful when we have an image with multiple objects or regions with distinct intensity levels.

In multi-Otsu thresholding, the image is divided into kk classes, where kk is a user-specified parameter. The algorithm iteratively searches for k1k-1 thresholds that maximize the variance between the classes, leading to the best separation of intensity levels. Each threshold separates the image into two classes, and this process is repeated until kk classes are obtained. The threshold values determine the intensity ranges for each class.

Difference between Otsu and multi-Otsu thresholding
Difference between Otsu and multi-Otsu thresholding

The threshold_multiotsu() method

The syntax of the threshold_multiotsu() method is given below:

threshold_multiotsu(image=None, classes=3, nbins=256, hist=None)

In Skimage, the threshold_multiotsu() method takes 4 parameters:

  • image: The first parameter is a grayscale input image.

  • classes: The second parameter is the number of kk classes to be thresholded. The default value of these classes is 3. This is an optional parameter.

  • nbins: The third parameter is the number of bins used to calculate the histogram. This is also an optional parameter.

  • hist: The fourth parameter is the histogram from which to determine the threshold. If no hist is provided, then this function computes it from the image. This is also an optional parameter.

Note: The threshold_multiotsu() method only takes a grayscale image. If we have a color image, then we convert it to grayscale using the rgb2gray() method before applying the multi thresholding algorithm.

Let’s understand multi-Otsu thresholding with the help of the following examples.

Example with 3 classes

In the following example, we use three classes, which means the input image is thresholded into three classes:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from skimage import data
from skimage import io,color
from skimage.filters import threshold_multiotsu
# The input grayscale image.
inputImage = data.camera()
# Applying multi-Otsu threshold for
# three classes.
thresholdValues = threshold_multiotsu(inputImage, 3)
# Generating the number of regions by using the threshold values
numberOfRegions = np.digitize(inputImage, bins = thresholdValues)
figure, axis = plt.subplots(nrows=1, ncols=3, figsize=(10, 3.5))
# Plotting the original image.
axis[0].imshow(inputImage, cmap='gray')
axis[0].set_title('Original Image')
axis[0].axis('off')
# Plotting the histogram from
# multi-Otsu.
axis[1].hist(inputImage.ravel(), bins=255)
axis [1].set_title('Histogram of the Image with three classes')
for thresh in thresholdValues:
axis[1].axvline(thresh, color='r')
# Plotting the result of MultiOtsu.
axis[2].imshow(numberOfRegions, cmap='jet')
axis[2].set_title('Multi-Otsu thresholding')
axis[2].axis('off')
plt.subplots_adjust()
plt.show()

Code explanation

  • Lines 1–6: We import the required libraries.

  • Line 9: We load a camera image that is available in Skimage.

  • Line 13: We generate thresholds using the threshold_multiotsu() method and assign the thresholds to the thresholdValues variable.

  • Line 15: We generate a number of regions based on the thresholdValues and assign them to the number OfRegions variable.

  • Lines 1834: We display the original image, a histogram of the original image (with 3 classes), and the thresholded image.

Example with 4 classes

In the following example, we use four classes, which means the input image is thresholded into four classes:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from skimage import data
from skimage import io,color
from skimage.filters import threshold_multiotsu
# The input grayscale image.
inputImage = data.camera()
# Applying multi-Otsu threshold for
# four classes.
thresholdValues = threshold_multiotsu(inputImage, 4)
# Generating the number of regions by using the threshold values
numberOfRegions = np.digitize(inputImage, bins = thresholdValues)
figure, axis = plt.subplots(nrows=1, ncols=3, figsize=(10, 3.5))
# Plotting the original image.
axis[0].imshow(inputImage, cmap='gray')
axis[0].set_title('Original Image')
axis[0].axis('off')
# Plotting the histogram from
# multi-Otsu.
axis[1].hist(inputImage.ravel(), bins=255)
axis [1].set_title('Histogram of the Image with four classes')
for thresh in thresholdValues:
axis[1].axvline(thresh, color='r')
# Plotting the result of MultiOtsu.
axis[2].imshow(numberOfRegions, cmap='jet')
axis[2].set_title('Multi-Otsu thresholding')
axis[2].axis('off')
plt.subplots_adjust()
plt.show()

Code explanation

  • Lines 1–6: We import the required libraries.

  • Line 9: We load a camera image that is available in Skimage.

  • Line 13: We generate thresholds using the threshold_multiotsu() method and assign the thresholds to the thresholdValues variable.

  • Line 15: We generate a number of regions based on the thresholdValues and assign them to the number OfRegions variable.

  • Lines 18–34: We display the original image, a histogram of the original image (with 4 classes), and the thresholded image.

Copyright ©2024 Educative, Inc. All rights reserved