9: Image Segmentation using Colors
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.
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.
The original image and its HSV individual channels can be seen. These will be used to set thresholds to generate masks.
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.