MATLAB Examples

# Convert RGB image to HSV and Explore HSV Color Space

This example shows how to convert an RGB image to the HSV color space using the rgb2hsv function. The example then displays the separate HSV color planes (hue, saturation, and value) of a synthetic image.

## Convert an RGB Image into an HSV Image

Read an RGB image into the workspace.

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

Convert it to the HSV color space.

```HSV = rgb2hsv(RGB); ```

## 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); ```

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.