9: Image Segmentation using Colors

Different channel representations

From the original image of an orange bean bag on white tiles, the various representations can be seen. The gray scaled image with a threshold set to make it binary, the green channel, the saturation channel, and finally the masked and segmented image of only the bean bag. Similar to how a threshold is set in the grayscale image to make it binary, thresholds can be set to the color and HSV channels to filter the image to show only intensity values wanted.

Otsu’s Method

One method to automatically find the threshold in gray scaled images is Otsu’s method. Otsu’s method assumes that your image is composed of a background and a foreground. This method works by minimizing the intra-class variance or maximizing the inter-class variance. The HSV channels can be used to generate a mask as well, to be stacked on top of the original image to show only the segmented image.

A practical usage of this can be seen in the following example where certain fruits need to be identified based on their colors and other features, and counted.

HSV Channels

The original image and its HSV individual channels can be seen. These will be used to set thresholds to generate masks.

Segmenting, filtering, and counting blobs

It can be seen that strawberries have a hue greater than or less than 0.93 and 0.03 respectively, and a saturation greater than 0.68. The resulting mask is run through a DoG blob detection algorithm to count the number of blobs generated and therefore the number of strawberries present in the image.

For the lemons, color thresholds are set again, but the bananas and the mango/papaya remains in the image as well. Additional filters using the regionprops function are required such as the area and eccentricity which help distinguish a big mango and small lemons as well as long bananas and round lemons.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nisarg Nigam

Nisarg Nigam

MS Data Science student at Asian Institute of Management