Main Content

Convert Between RGB and HSV Color Spaces

This example shows how to adjust the saturation of a color image by converting the image to the HSV color space. The example then displays the separate HSV color planes (hue, saturation, and value) of a synthetic image.

Convert RGB Image to HSV Image

Read an RGB image into the workspace. Display the image.

RGB = imread('peppers.png');
imshow(RGB)

Figure contains an axes object. The axes object contains an object of type image.

Convert the image to the HSV color space.

HSV = rgb2hsv(RGB);

Process the HSV image. This example increases the saturation of the image by multiplying the S channel by a scale factor.

[h,s,v] = imsplit(HSV);
saturationFactor = 2;
s_sat = s*saturationFactor;
HSV_sat = cat(3,h,s_sat,v);

Convert the processed HSV image back to the RGB color space. Display the new RGB image. Colors in the processed image are more vibrant.

RGB_sat = hsv2rgb(HSV_sat);
imshow(RGB_sat)

Figure contains an axes object. The axes object contains an object of type image.

Closer Look at the HSV Color Space

For closer inspection of the HSV color space, create a synthetic RGB image.

RGB = reshape(ones(64,1)*reshape(jet(64),1,192),[64,64,3]);

Convert the synthetic RGB image to the HSV colorspace.

HSV = rgb2hsv(RGB);

Split the HSV version of the synthetic image into its component planes: hue, saturation, and value.

[h,s,v] = imsplit(HSV);

Display the individual HSV color planes with the original image.

montage({h,s,v,RGB},"BorderSize",10,"BackgroundColor",'w');

Figure contains an axes object. The axes object contains an object of type image.

As the hue plane image in the preceding figure illustrates, hue values make a linear transition from high to low. If you compare the hue plane image against the original image, you can see that shades of deep blue have the highest values, and shades of deep red have the lowest values. (As stated previously, there are values of red on both ends of the hue scale. To avoid confusion, the sample image uses only the red values from the beginning of the hue range.)

Saturation can be thought of as the purity of a color. As the saturation plane image shows, the colors with the highest saturation have the highest values and are represented as white. In the center of the saturation image, notice the various shades of gray. These correspond to a mixture of colors; the cyans, greens, and yellow shades are mixtures of true colors. Value is roughly equivalent to brightness, and you will notice that the brightest areas of the value plane correspond to the brightest colors in the original image.