Color Conversions

The RGB color format can represent any standard color or brightness using a combination of Red, Green and Blue components. For efficiency, this is typically stored as a 24-bit number using 8-bits for each color component (0 to 255) so that for example, White is made of 255 Red + 255 Green + 255 Blue. This is pretty much the same technique that nearly all computer screens have used for decades, and so it is the standard color format used in computer software. Unfortunately when it comes to computer vision, RGB values will vary a lot depending on strong or dim lighting conditions and shadows, etc. In comparison, HSV is much better at handling lighting differences, and it gives you an easy to use color value.

HSV means Hue-Saturation-Value, where the Hue is the color. And since color is not an easy thing to separate or compare, Hue is often represented as a circular angle (between 0.0 to 1.0 when stored as floats). Being a circular value means that 1.0 is the same as 0.0. For example, a Hue of 0.0 is red, a Hue of 0.25 would be green, a Hue of 0.5 is blue, a Hue of 0.75 is pink, and a Hue of 1.0 would be the same as a Hue of 0.0 which is red (again). Saturation is the greyness, so that a Saturation value near 0 means it is dull or grey looking whereas as a Saturation value of 0.8 might be a very strong color (eg: red if Hue is 0). And Value is the brightness of the pixel, so 0.1 is black and 0.9 is white. Unfortunately, there are different ways to represent HSV colors, such as whether a full brightness V of 1.0 should be bright white or a bright color. Most software chooses full brightness V to mean White, whereas OpenCV chooses full brightness V to mean a bright color!

(For a lot more info about HSV and other color spaces, go to HSL and HSV on Wikipedia).

HSV Color format in OpenCV

OpenCV is a great library for writing Computer Vision software using state-of-the-art techniques, and it is available for many Operating Systems, including Windows, Linux, Mac and even the iPhone. It’s main color format is RGB (actually it is BGRX, which is the bytes of RGB reversed and with a 4th byte of padding for efficiency, but generally referred to as RGB for simplicity). It also includes various functions to work with different color formats, such as to convert from RGB to HSV or YUV or LAB or XYZ, etc. However, OpenCV’s HSV format is different than what you would expect!

OpenCV’s RGB-to-HSV and HSV-to-RGB conversion only stores the Hue component as an 8-bit integer in the range 0 to 179, when it could have easily used 0 to 255. This means that if you use OpenCV’s cvCvtColor() function to obtain a HSV image, you will loose some of the color resolution, since it is basically storing the Hue as a 7-bit number instead of an 8-bit number. Also, if you process this HSV image based on color values you found from other software or libraries, it is likely to cause you problems, since most systems use a range of 0 to 255. For example, if you are writing a skin detector and want to find some good HSV thresholds, if you use any image editor (such as MS Paint or Adobe Photoshop) to read the HSV values, they will be different than the values you need for OpenCV.

Also, RGB colors can be converted to the HSV color space in several different ways, resulting in quite different values for the same color. So in OpenCV a Saturation of 255 is always a bright color, whereas it is pure white in most software. This makes it even more difficult to use other software with OpenCV, since the HSV format used in OpenCV is different to the format used in most image editing software.

Leave a Reply

Your email address will not be published. Required fields are marked *