Time for action – color space transformation

In this example, we will demonstrate the usage of inherent MATLAB functions to transform a RGB image to HSV and to CIE-L*a*b*. For the first one, we will use rgb2hsv and for the second one we will use makecform and applycform. The following steps will do the trick:

  1. First we load our image:
    >> img = imread('my_image_color.bmp'),
  2. Then, we generate the HSV image:
    >> img_hsv = rgb2hsv(img);
  3. Finally, we will convert our image to CIE-L*a*b*:
    >> cform = makecform('srgb2lab'), % Make the transformstructure
    >> img_lab = applycform(img,cform); % Apply transform
  4. Now, let's demonstrate our results:
    >> subplot(3,4,1),imshow(img),title('RGB image')
    >> subplot(3,4,2),imshow(img(:,:,1)),title('R channel')
    >> subplot(3,4,3),imshow(img(:,:,2)),title('G channel')
    >> subplot(3,4,4),imshow(img(:,:,3)),title('B channel')
    >> subplot(3,4,5),imshow(img_hsv),title('HSV image')
    >> subplot(3,4,6),imshow(img_hsv(:,:,1)),title('H channel')
    >> subplot(3,4,7),imshow(img_hsv(:,:,2)),title('S channel')
    >> subplot(3,4,8),imshow(img_hsv(:,:,3)),title('V channel')
    >> subplot(3,4,9),imshow(img_lab),title('CIE-L*a*b* image')
    >> subplot(3,4,10),imshow(img_lab(:,:,1)),title('L* channel')
    >> subplot(3,4,11),imshow(img_lab(:,:,2)),title('a* channel')
    >> subplot(3,4,12),imshow(img_lab(:,:,3)),title('b* channel')
    Time for action – color space transformation

What just happened?

This was just a demonstrative example of basic color space conversions in MATLAB. The results were presented in a common figure, with each color channel isolated, so that you can get a better qualitative sense of what the alternative color spaces have to offer. Both HSV and CIE-L*a*b* separate color from brightness information. In the former case, the brightness channel is V (Value) and in the latter case, the brightness channel is L (Lightness). The remaining two channels in each color space include color information. The basic difference is that in the case of CIE-L*a*b* color space, the two remaining channels (a* and b*) are so-called color opponent dimensions. Channel a* assigns large values to red colors and low values to green colors. Similarly, channel b* assigns large values to yellow pixels and low values to blue pixels. In the case of HSV, color information is included in channels H (Hue) and S (Saturation). Hue is an angle given in degrees and Saturation is a length. Combined with Value, they define a cylinder of color shades.

Tip

It is useful to notice that the functions described previously do not produce the expected results, as they are normalized to fit the description of an image. Therefore, the color spaces produced by applycform, will include pixel values ranging from 0 to 255. The other three pairs of color space transformation functions (which exist also in older versions of MATLAB), will include pixel values ranging from zero to one. This should be something to beware of, especially if you try to reproduce results that are based in the original descriptions of color spaces (for example, expect the Hue to have values from 0 to 180, since it is an angle).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset