Chromaticity Segmentation — Image Processing

Image Segmentation | OpenCV

As exemplified in my previous article, we segmented the objects in an image using Color Image Segmentation through the HSV color scale. But that is not the only way we can segment our images. For this blog, we look at the image segmentation process under the lens of chromaticity segmentation.

Why should one prefer Chromaticity over RGB/HSV Segmentation?

  • We can get the ratios between RGB/HSV channels.
  • We can use target patches consisting of secondary colors and a mixture of other colors.

We formally define chromaticity segmentation as the process of extracting objects from images using the chromaticity space of the RG channels. The latter make up a 2-dimensional color representation that neglects image information with respect to its intensity values. We do this by looking at the proportion of the different color channels and map it using the normalized RGB space. Hence, to compute for the RG Chromaticity of an image, we use the following equations:

We primarily just look at the r and g equations since from there we can intuitively compute for the b channel.

Let us put chromaticity segmentation into action using our good old friend — Python.

Install the following libraries before we proceed with the implementation:

The discussion will focus on this image:

Mixed Fruits | Drive (Click here to download the image)

Let us get started! Before going through the code, we outline the steps we shall undertake.

Step 1: Compute for RG Chromaticity of the image

  • This is done by using our equations defined in the introduction.

Step 2: Compute for the 2D histogram of the color values (original image)

  • This is by flattening both R and Chromaticity values and feeding it in the hist2d function.
  • Through this, it can be noticed what color or group of colors comprises our image.

Step 3: Select a reference image patch

  • Generate a patch from the object of interest. In this case, we are segmenting strawberries. So, the patch that will be selected will be of that of a strawberry.

Step 4: Compute for the RG Chromaticity of the patch

  • Repeat Step 1 but using the image patch in Step 3

Step 5: Compute for the 2D histogram of the color values (patch)

  • Repeat Step 2 but using the image in Step 3

So far, we have gotten the RG chromaticity values of the concerned images. Now, we proceed to the next step the — Parametric Segmentation.

Step 6: Parametric Segmentation

  • This step requires us to fit a gaussian distribution which will determine the pixels that belong to the color of interest. Before implementing this, the mean and standard deviations are calculated from the object of interest (reference patch). These are then fed to the gaussian distribution function.

The code implementation of Steps 1–6 are implemented as follows:

Brilliant! Let us proceed to segmenting our strawberries from the image.

Using the gaussian distribution function we have defined earlier, let us obtain the probability of a color being part of our image through both the R and G coordinates. We implement this using the code below:

As you have observed, similar to the color image segmentation method, there is a threshold that is being determined arbitrarily. Experiment and play around with the values and choose the value that will return the most desirable output. Also, in the final graph, although strawberries were clearly segmented, irrelevant information were still captured. This is where morphological operations come in handy to further clean the image so that the segmentation is as accurate as ever. To know more about morphological operations do check out my previous articles.

Great job! Little by little you are becoming an Image Processing expert! Keep pushing, you got this.

A kid who uses data to make a difference.

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