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.
2. Use the rgb_to_hsv function to convert the individual RGB components to HSV.
The 2nd and 3rd elements are identical in all three matrices and are set to 255.
3. Use the rgb_to_hls function to convert the individual RGB components to HLS.
The 2nd and 3rd elements are identical in all three matrices and are set to 127.5 and 255.
4. Use the hsv_to_rgb and hls_to_rgb functions to verify that converting the HLS/HSV Blue vectors returns the original individual Blue component.
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.
2. Augment the HSV components and transpose the result to create a 3 x 256 array.
3. Use the hsv_to_rgb function to create a 3 x 256 RGB matrix of palette entries.
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.
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.
(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.
2. Create the H, S, and V components of the image matrix:
3. Plot the newly created image:
(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.
(strike_rgb.bmp)
2. Convert the RGB image to HLS and HSV.
(strike_hls.bmp) | (strike_hsv.bmp) |
3. Convert the HLS and HSV images back to RGB.
(strike_rgb2.bmp) | (strike_rgb3.bmp) |