Color Extraction Algorithm by HSV

Introduction

Color isolation can be achieved by extracting a particular HSV (hue, saturation, value) from an image. The algorithm is simple and the main steps are as follows:

The MATLAB code can be found on my github:
Computer Vision Projects

Algorithm Steps

Step 1 - RGB to HSV Conversion

We want to convert the image to HSV because working with HSV values is much easier to isolate colors. In the HSV representation of color, hue determines the color you want, saturation determines how intense the color is and value determines the lightness of the image. As can be seen in the image below, 0 on the wheel would specify a mild red color and 240 would specify a blue color. In MATLAB, the hue ranges from 0 to 1 instead of 0 to 360.

HSV Representation of Color - image from nmt.edu
The HSV distribution of the color wheel can be seen in the following image:
HSV Distribution and Histogram

Step 2 - Apply a Threshold Mask

To isolate the colors, we have to apply multiple masks. A low threshold and high threshold mask for hue, saturation and value. Anything pixel within these thresholds will be set to 1 and the remaining pixels will be zero. In my algorithm, I applied a mask to get all of the red hues in the color wheel as can be seen below:

Isolating Red Hues

Improvements/Other Methods

This method of color isolation may not work so well if the image is noisy. This is quite common because a camera uses an ADC which can create noise in an image. One potential solution would be to apply a Gaussian blur before running this algorithm.

Color segmentation can also be achieved using K-means clustering, however, this method may be slow as it can requires multiple iterations to get the correct color.