Image Segmentation using Thresholding and Otsu’s Method

Rafael Madrigal
2 min readSep 14, 2022

--

In our previous articles, we binarized our images through simple thresholding of values but sometimes this does not work in colorful images. In the example I shared in my first article, I used a combination of masked done in the HSV color space in order to isolate certain fruits. Doing this in the RGB space will be tricky since the only thing that are red and green in the image are the strawberries, the green apple, and the leaves. This leaves us with almost an impossible way to isolate yellow (for lemons), orange (for oranges), and violet (for grapes). Furthermore, simply using a binary threshold on the grayscale image, will not yield any good result since everything will be gray by then.

In this notebook, we discuss a different segmentation algorithm called Otsu’s method. (We will not touch on thresholding in the HSV space as this is already covered by my previous notebook).

Otsu’s method automatically finds the threshold value by assuming that the image is composed of a background and a foreground. It then minimizes the intra-class variance or maximizes the inter-class variance in the image. Luckily, we don’t have to go through the rigorous math behind Otsu’s method since we can do it fast with one line of code using scikit-image.

In the example below, we do something called “multi-otsu” thresholding, which is like the simple Otsu method I described but generates multiple thresholds. Basically, it computes several thresholds determined by the number of desired classes (default is 3).

import matplotlib.pyplot as plt
import numpy as np
from skimage.data import camera
from skimage.filters import threshold_multiotsu
image = camera()# MultiOtsu will return multiple thresholds
thresholds = threshold_multiotsu(image)
# Generate regions
regions = np.digitize(image, bins=thresholds)

The thresholds are indicated by the red bars in the histogram plot. These lines managed to segment the image to the sky, the grass, and the foreground.

Wrapping Up

In this article, we introduced an image segmentation technique called Otsu’s Method. It is an automatic way to find thresholds to binarize your image. Otsu’s method basically maximizes the interclass-class variance between the foreground and the background. An extension of this method is called Multi-Otsu which allows the user to segment the image into a number of regions. The same concept applies, but instead of 2 regions, the algorithm segments the images to the number specified by the end user.

--

--

Rafael Madrigal

Data Scientist and Corporate Strategist. Can’t function without Coffee