Functions > Image Processing > Image Color Tools > Example: HLS and HSV Color Systems
  
Example: HLS and HSV Color Systems
Use the rgb_to_hls, hls_to_rgb, rgb_to_hsv, and hsv_to_rgb functions for converting the color representation of images and color vectors.
For information on using this example, refer to About Image Processing Examples.
Converting Between RGB and HLS/HSV: Single Colors
1. Define the individual RGB colors.
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Convert the individual RGB components to HSV.
Click to copy this expression
Click to copy this expression
Click to copy this expression
The 2nd and 3rd elements are identical in all three matrices and are set to 255.
3. Convert the individual RGB components to HLS.
Click to copy this expression
Click to copy this expression
Click to copy this expression
The 2nd and 3rd elements are identical in all three matrices and are set to 127.5 and 255.
4. Verify that converting the HLS/HSV Blue vectors returns the original individual Blue component.
Click to copy this expression
Click to copy this expression
Creating Color Maps
You can use the hsv_to_rgb function to generate a color map palette for viewing a grayscale image in false color.
1. Generate a 256-color palette where the saturation and value are both 255, and the hue varies from 0 to MAXH.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Augment the HSV components and transpose the result to create a 3 x 256 array.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
3. Use the hsv_to_rgb function to create a 3 x 256 RGB matrix of palette entries.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
4. Transpose the RGB matrix to create a 256 x 3 matrix, and then place the index column on the left to make the 256 x 4 color map.
Click to copy this expression
Click to copy this expression
Click to copy this expression
5. Use the above color palette to view a grayscale image in false color, and experiment with different values of MAXH to see the effect on the image.
Click to copy this expression
Click to copy this expression
Click to copy this expression
(bridge.bmp)
(bridge_pal.bmp)
Viewing HLS and HSV Images
HLS and HSV images can be viewed in a picture region by setting the input type to HLS or HSV.
1. Define the size of an HSV image that you create from three separate submatrices.
Click to copy this expression
2. Create the H, S and V components of the image matrix:
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
3. Plot the newly created image:
Click to copy this expression
(color_wheel.bmp)
Converting between RGB and HLS/HSV: Entire Images
You can convert RGB images to and from HLS and HSV using the functions above.
1. Read in an RGB image.
Click to copy this expression
Click to copy this expression
(strike_rgb.bmp)
2. Convert the RGB image to HLS and HSV.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
(strike_hls.bmp)
(strike_hsv.bmp)
3. Convert the HLS and HSV images back to RGB.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
(strike_rgb2.bmp)
(strike_rgb3.bmp)