Conversion Between RGB and HSV (Introduction to Video and Image Processing)

In this topic the conversions from the RGB color representation to the HSV color representation, and reverse, are derived. That is, we seek a conversion from [R, G, B] to [H, S, V], and one from [H, S, V] to [R, G, B].

Conversion from RGB to HSV

We recall from Sect. 3.3.2 that HSV is short for hue, saturation and value, and that value is defined as

tmp7470-317_thumb

We start by defining a sub-cube of dimension (V,V,V) inside the RGB color cube, see Fig. E.1(a). Since V is equal to the maximum RGB value the RGB point to be converted is located on one of the sides of this sub-cube. Imagine now that we define a plane perpendicular to the gray-vector and project the corners of the subcube onto this plane. This corresponds to placing your eye at [255, 255, 255] and looking at [0, 0, 0]. The result will be the hexagon illustrated in Fig. E.1(b). Each corner of the hexagon will point toward one of the corners of the RGB color cube and are therefore denoted R’, Y, G’, C, B’, and M, corresponding to red, yellow, green, cyan, blue, and magenta, respectively.1 In the center of the hexagon we will have black and white at the same point, denoted W. The RGB point to be converted is also projected onto the plane and denoted P.


The six corners of the hexagon have the same distance to W. From this follows that the distances between adjacent corners are equal to each other and to the distance from a corner to W. Since all lengths in the hexagon are equal we can scale the hexagon as we please. We choose to scale the hexagon so that all sides have the length V, which should be interpreted in the following way.

If we assume max{R, G, B} = R we know that the RGB point is located on the side of the sub-cube defined as R = V. This corresponds to one of the two sextants MWR or RWY. In these two sextants the “position” of P is given as (G, B), see

(a) The sub-cube inside the RGB color cube. (b) The hexagon defined when looking at the sub-cube from the point (255, 255, 255) and toward the point (0, 0, 0). The corners of the hexagon correspond to the corners of the sub-cube. The dashed lines indicate the lines not visible if the sub-cube is solid

Fig.E.1 (a) The sub-cube inside the RGB color cube. (b) The hexagon defined when looking at the sub-cube from the point (255, 255, 255) and toward the point (0, 0, 0). The corners of the hexagon correspond to the corners of the sub-cube. The dashed lines indicate the lines not visible if the sub-cube is solid

Fig. E.1(b), meaning that we can derive hue and saturation in the two sextants from Fig. E.1(b) and then directly express hue and saturation in terms of the RGB values. Similar arguments can be made for the remaining sextants.

HSV: Saturation

Looking at Fig. E.1(b) saturation is defined as the ratio between the distance from the W to P and the distance from W to P’, where P’ is the intersection between the vectors spanned by W and P, and R and Y. That is, saturation is given as

tmp7470-319_thumb

Since the triangles WPD and WPrY are similar,we have

tmp7470-320_thumb

From this follows that

tmp7470-321_thumb

We know from the definition of the sub-cube thattmp7470-322_thumband can see in Fig. E.1(b) thattmp7470-323_thumbThis    yieldstmp7470-324_thumbWe    can see that B = min{R, G,B} in the sextant where P is located and can therefore express saturation as

tmp7470-328_thumb

wheretmp7470-329_thumbSimilar    reasoning    for    the    other five sextants in the hexagon shows that Eq. E.5 is indeed a general equation that holds for the entire hexagon.

HSV: Hue

In Fig. E.1 (b) hue is illustrated as the angle betweentmp7470-330_thumbHue is, however, not calculated as an angle but rather as the following ratio:

tmp7470-333_thumb

where A, P, and D are defined in Fig. E.1(b). This definition is only valid when P is located in the sextant shown in Fig. E.1(b), i.e., then R = V and G > B .In this sextant hue will be a value in the interval [0, 1] where hue = 0 corresponds to red and hue = 1 corresponds to yellow. The calculated hue value is normally multiplied with 60° in order to obtain a hue value in the range of [0°, 360°[ when considering all sextants.2 Below we show how hue is calculated in the sextant RWY.

Looking at Fig. E.1(b) we can see thattmp7470-334_thumb and thattmp7470-335_thumbFrom    this    follows    that

tmp7470-338_thumb

Above we saw thattmp7470-339_thumband stated that the  “position” of P in this sextant is given as (G, B). Combining this with Eq. E.7 and converting the ratio into degrees we have

tmp7470-341_thumb

A similar geometric reasoning can be carried out for the sextant MWR where R = V and B > G resulting in:

tmp7470-342_thumb

Moving on to the sextant YWG where G = V and R > B we can derive that

tmp7470-343_thumb

For reasons that will be clear when we look at the next sextant, we rewrite Eq. E.10 in the following way:

tmp7470-344_thumb

For the sextant GWC where G = V and B > R we can derive that

tmp7470-345_thumb

We can see that Eqs. E.11 and E.12 are the same, which means that we only need one equation when G = V. The same holds for the last two sextants and the final equation for hue therefore becomes

tmp7470-346_thumb

wheretmp7470-347_thumbNote that hue is sometimes defined as a number in the interval [0,1 [. This is obtained by dividing by 6 instead of multiplying by 60. Sometimes the interval [0, 2π [ is used. This is obtained by dividing by 3 and multiplying by π instead of multiplying by 60.

Note that hue is undefined when no color is present, i.e., R = G = B. One could define it to be 0 (or some other value), but a better approach is often to define it as the hue value of the previous pixel. For a gray-scale image this will not make sense, but then again, no point in converting a gray-scale image into an HSV image in the first place! Note also that saturation is undefined in Eq. E.5 when (R = G = B = 0). We therefore make the following definition: S = 0 when (R = G = B = 0).

Conversion from HSV to RGB

The conversion from HSV to RGB depends on in which sextant the point is located. We can assess that by dividing the hue value by 60 and taking the closets integer equal to or just below. This directly provides and index K in the range: 0 < K < 5, stating in which sextant the point is.

When K = 0 we are in the sextant RWY and find R, G, and B in the following way. First we realize that R = V in this sextant. Next we use the fact that B = min{R, G, B} in this sextant. Inserting this into Eq. E.5 we get B = V · (1 — S). G is found using the following equation derived above:

tmp7470-349_thumb

Substituting we have

tmp7470-350_thumb

When K = 1 the point is located in the sextant YWG. In this sextant we know from above that G = V and B = min{R, G, B}. R is found using the following equation, derived above:

tmp7470-351_thumb

Substituting we have

tmp7470-352_thumb

For the remaining four sextants we end up with similar results. The last thing remaining before we can put it all together and derive a general conversion from HSV to RGB is a method to map from Hdeg to H. This is done as follows:

tmp7470-353_thumb

where LxJ means the floor of x.The final conversion is now given as

tmp7470-354_thumb

Next post:

Previous post: