Converting Between Color Spaces
Converting Between Color Spaces
Now that you've been introduced to different color spaces, let's practice converting between them. This is a first
step to thresholding color images or adjusting the contrast of color images, which you will learn about later in
this course. Work through this script section by section to practice converting an image from RGB to alternative
color spaces and back to RGB.
Table of Contents
Import and display a color image..............................................................................................................................1
Convert to alternative color spaces.......................................................................................................................... 1
Examine the matrix values and data types............................................................................................................... 1
Display the converted images...................................................................................................................................2
Display the image correctly in RGB.......................................................................................................................... 2
img = imread("1_SnowPoolsBefore_20180113_md-2048px.jpg");
imshow(img)
Convert to HSV
% hsvImg =
Convert to YCbCr
% ycbcrImg =
Convert to L*a*b*
% labImg =
img(1:10,1:10,3)
hsvImg(1:10,1:10,3)
ycbcrImg(1:10,1:10,3)
labImg(1:10,1:10,3)
1
The results look completely different! Also, take a look at the workspace to see the different data types of the
matrices.
Notice how the original image and the YCbCr representation are both of type uint8, but the HSV and L*a*b*
values are of type double. This is important to remember as you perform your own color space conversions.
imshow(hsvImg)
imshow(ycbcrImg)
imshow(labImg)
Always convert back to RGB when working with images in different color spaces before displaying them.
% rgbImg1 =
% rgbImg2 =
% rgbImg3 =
2
imshow(rgbImg1)
imshow(rgbImg2)
imshow(rgbImg3)
That's much better! All of these images look identical to the original image. Do note that changing the images
back to RGB does not change their datatypes, so two of the three new RGB images are of type double.