Robust color detection

Robust color detection
voyata / Shutterstock

Reliably detecting colors can be a challenging task when the shape of the observed object is curved and/or has inconsistent light from all directions. In the photo shown we have dices for which we can say safely that the colors on each side should be very similar. If we were to inspect the RGB colors of left and bottom sides of the first dice (1 and 3) the results would be very different.

Values where the black dots were excluded from the evaluation were:

Side with 1 point: RGB (248, 215, 201) Side with 3 points: RGB (123, 93, 81)

Color detection
Averaged colors from sides 1 and 3

Using RGB color space is great for building cameras and television sets but not a perfect tool for telling the colors apart. A better way would be to convert the image to HSV (hue, saturation, value) color system. HSV separates the light intensity (value) from the other two dimensions (hue and saturation). Hue scale goes from 0 to 360 (degrees), and saturation and value from 0 to 100.

HSV color space cylinder
This file is licensed under the Creative Commons

Color values using HSV space:

Side with 1 point: HSV (17, 18, 97) Side with 3 points: HSV (17, 34, 48)

Let us now show the intensities separated by channels:

Dice shown in separated RGB channels
Dice shown in separated HSV channels

First image shows intensities of each RGB channel and the second one each HSV channel. Looking at the first image in the second row (hue), we can see that the transition between the sides is very hard to detect meaning they are in fact the the same hue/color (17). There are some differences in saturation channel (18 vs. 34) and as expected the largest difference in the value/light channel (97 vs. 48).

If we were working on a task where similar colors should be clustered together we could use a color space like CIELAB which would give us the following result for the same surfaces:

Side with 1 point: LAB (241, 125, 143) Side with 3 points: LAB (174, 129, 146), where the L represents the light dimension.

Of course, this method has its limitations. Overexposed parts of the image cannot be reliably converted to “real” color because the information is irreversibly lost because of the sensor saturation.