Image Processing with Python: Image Segmentation using Thresholding Methods

How to pinpoint and segment objects based on their color channels?

JManansala
The Startup
5 min readJan 29, 2021

--

(Image by Author)

In this post, we will explore how to conduct image segmentation using trial and error thresholding and Otsu’s method. Moreover, we will explore how the RGB and HSV color spaces can be useful in segmenting images.

Let’s begin.

As usual, we import libraries such as numpy and matplotlib. Additionally, we import specific functions from the skimage library.

Let’s use this image of a Chico tree with some of its fruits.

(Image by Author)

We have previously discussed how binarizing an image can be useful since it simplifies the analysis when the pixel values are constrained only to 0s and 1s. So far, we have been using trial and error to find the arbitrary threshold value that best captures our desired shape of the objects.

(Image by Author)

From this image, we might choose a thresholding value of 0.40 to 0.60 since it captures most of the leaves in the tree. However, this method is subjective. Let’s see if we can automate determining the thresholding value by looking at the intensity values histogram.

(Image by Author)

From the histogram, we can see that there are two distributions in the image. This also explains why the optimal value in the trial and error method is in the range of 0.40 to 0.60 thresholding value. However, what if I told you that there is an automated way to determine this threshold value? This solution is called the Otsu’s method.

Otsu’s method assumes that the image is composed of a background and a foreground. This method works by minimizing the intra-class variance or maximizing the inter-class variance. Let’s see how this works.

(Image by Author)

Amazing, right? Now, we could bother less on determining the best threshold value in binarizing the image! However, we should ALWAYS remember this method’s underlying assumption: the image is composed of a background and a foreground. This means that if we take a look at the histogram, there should only be two separable distributions.

Now, dividing the background and the foreground can be really useful. However, most of our image segmentation problems is not a background-foreground problem. For example, we cannot simply use Otsu’s method in segmenting the Chico fruits from the leaves — this is because both are in the foreground of the image.

To address this problem, we should look at the color spaces of the images. As I have mentioned in my previous posts, any color is simply a combination of varying pixel intensity values in the image’s channels. Therefore, to segment a specific color in an image, we must use the thresholding method on the particular ratios of pixel intensity values in the three channels. Let’s try this in the RGB color space.

(Image by Author)

Notice how the background has a high-intensity value while the objects themselves have a low-intensity value in all of the channels. This is because the background is the sky — which technically is the source of light. Thus, it naturally has a higher intensity value. This makes segmenting the image to background-foreground easier. However, this also makes segmenting images in the foreground much more challenging because of the much tighter pixel intensity range.

(Image by Author)

Difficult, right? Even if we used area_opening to clean the image, it still does not show good results. Now, let’s try if we can make this easier by using the HSV color space.

(Image by Author)

Notice how the HSV color space can visualize the image’s objects much better than the RGB color space. Let’s try to use thresholding methods using this color space.

(Image by Author)

See? Now, that’s easier! This can be attributed to the Hue channel of the HSV color space that clearly identifies the objects on the images based on their hue.

Ultimately, the choice of what color space should be used to segment the objects in the images is upon the discretion of the data scientist. I hope that by reading this post, you were able to get the intuition needed to implement your own image processing projects!

In summary

We have explored how to properly segment images to the background and foreground using the trial and error thresholding method and the Otsu’s Method. Moreover, we have also explored how the RGB and HSV color spaces can help pinpoint and segment objects in images.

Want to learn more? Check out my GitHub repository at this link!

--

--