Machine Vision Recipes: Isolating a Color in an Image

9Sphere AI
The Startup
Published in
4 min readAug 13, 2020

Image segmentation is one of the key processes in machine vision applications for partitioning a digital image into a group of pixels. There are many interesting ways one can segment an image. Take a look at the below image of candies placed in a particular order to form a word. And, If a robot with vision were a task to count the number of candies by color, it would be important for it to understand the boundaries between the candies.

Photo by Avinash Kumar on Unsplash

A direct thresholding to separate the foreground from its background will create overlapping candies making it hard for us to count them. Take a look the below grayscale image and the one after OTSU threshold.

Image in Grayscale
Image in Grayscale
Image after Thresholding

As we can clearly see that its almost impossible for a robot to count the candies because there is no way for us to define the boundaries between the candies. I even tried a variant of watershed algorithm but because of the presence of bright coloured candies the thresholding always failed.

This led me to the only solution which is how even a human being would do this task. Let us make our robot color aware because Grayscale algorithms are clearly not useful here. The image we are dealing with here is an RGB image with millions of colors where each pixel is a set of Red, Green and Blue channel intensity ranging from 0–255. The colors R (Red), G (Green) and B (Blue) have a close color correlation level which makes it difficult to segment. The trick is to transform RGB to a different color space where the boundaries between the colors are well defined. For this experiment I will pick the CIE-Lab color space which closely resembles how humans see colors.

CIE-Lab color space

While RGB color space is a cubical model, CIE-Lab color is a spherical model.

CIE-Lab color space

As depicted in the image above, we can easily isolate individual set of colors we are interested in. For example the Green color lies towards the negative axis of A axis. So a simple code to choose only green colors we can do it this way.

The output is:

Isolating green candies for A < -35

The range of yellow color is approximately in the ranges.

L_value > 70 and
A_value > -50 and
A_value < 0 and
A_value > 30 and
A_value < 100

The best way to find the boundary ranges is to visualise the colors using a scatter plot as shown below:

L a* b* colors spectrum

Here is a look at all the candies isolated from each other.

Though its still a challenging task but calculating the number of candies is much easier now. I will cover that in a different post.

You can find the entire code for the above recipe in my github account here.

I often blog about my research and finding on my personal blog here.

--

--

9Sphere AI
The Startup

Extract data from any document or image using Computer Vision Technology