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
threshold_multiotsu()
methodThe 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 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 thergb2gray()
method before applying the multi thresholding algorithm.
Let’s understand multi-Otsu thresholding with the help of the following examples.
In the following example, we use three classes, which means the input image is thresholded into three classes:
import matplotlibimport matplotlib.pyplot as pltimport numpy as npfrom skimage import datafrom skimage import io,colorfrom 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 valuesnumberOfRegions = 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()
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 3
classes), and the thresholded image.
In the following example, we use four classes, which means the input image is thresholded into four classes:
import matplotlibimport matplotlib.pyplot as pltimport numpy as npfrom skimage import datafrom skimage import io,colorfrom 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 valuesnumberOfRegions = 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()
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.