# Color Spaces in MATLAB | MATLAB Tutorial

#### Color spaces – What do they mean?

Color spaces are a lot like color maps in the sense that they are used to change the color scheme of a plot, a surface or even an image. They are basically a matrix specifying the relationship between the existing RGB values of a pixel in the original image and the new color values of the same pixel in the modified image. There are a large number of color spaces available. The three most common ones are RGB (used in computer graphics), YIQ, YUV or YCbCr (used in video systems), and CMYK (used in color printing).

In this tutorial, we will explore the two main methods of converting the color space of an image from one colorspace to other. These two methods are:

• Using inbuilt functions like rgb2hsv
• Writing your own functions specifying the relation between colorspaces.

#### Difference between colorspaces and colormaps

The one very common question that every image processing enthusiast faces is what the difference between color spaces and color maps is. Both have the same function of transforming the color scheme of an image. Though, the difference between both is much more fundamental.

Where color map change the color scheme by mapping the data values to existing colors in the map, color space makes the changes using a set of mathematical relationships.

Also, the changes made by color maps are pseudochanges i.e. the original color scheme of the image is not updates. On the other hand, color spaces update the color scheme of the image to a much deeper level by updating the pixel values.

#### YUV Color Space

YUV Color space can be represented in terms of RGB values as follows:

Y = 0.299 R’ + 0.587 G’ + 0.114 B’

U = 0.492 (B’ – Y)

V = 0.877 (R’ – Y)

This system of equations can be written in terms of matrix form as:

Similarly, for converting RGB to YUV, the following matrix equation can be used:

#### YIQ Color Space

RGB Color space can be converted to YIQ Color space using the following matrix equation:

Similarly, for conversion of YUV color space to YIQ Color space the following matrix equation is used:

Note: The Y component is same in both color spaces.

#### YCbCr Color Space

The components in YCbCr color space are usually represented by (Y601, Cb, Cr). The matrix equation for converting RGB Color Space to YCbCr color space is:

The same matrix equation can be manipulated to find out RGB color space if YCbCr color space is given.

## NTSC Color Space

In the NTSC color space, the luminance is the grayscale signal used to display pictures on monochrome (black and white) televisions. The other components carry the hue and saturation information.

NTSC color space is defined using three components – Y (luminance) and I and Q (chrominance).

RGB Color space can be converted to NTSC Color space using the following matrix equation:

#### RGB to YCbCr – rgb2ycbcr

We will use the inbuilt function rgb2ycbcr() to convert an RGB image to YCbCr color space.

```RGB = imread(‘board.tif’);
YCBCR = rgb2ycbcr(RGB);
imshow(YCBCR);
title(‘Image in YCbCr color space’);
```

Figure: Image in YCbCr Color Space

#### YCbCr to RGB – ycbcr2rgb

Just like rgb2ycbcr, MATLAB inbuilt function ycbcr2rgb() can be used for converting YCbCr color space to RGB.

% Continuing from previous example

```rgb_new = ycbcr2rgb(YCBCR);
figure;
subplot(121);
% Display original RGB image
imshow(RGB);
title(‘Original RGB Image’);
subplot(122);
% Display RGB Image after conversion
imshow(rgb_new);
title(‘RGB Image after conversion’);
```

Figure: Recovering original RGB image using ycbcr2rgb function

#### RGB to NTSC (television) – rgb2ntsc

```% Continuing from previous example
NTSC = rgb2ntsc(RGB);
imshow(NTSC);
title(‘NTSC Color space’);
```

Figure: Image in NTSC Color Space

#### NTSC to RGB – ntsc2rgb

```% Continuing from previous example
rgb_new = ntsc2rgb(NTSC);
figure;
subplot(121);
% Display original RGB image
imshow(RGB);
title(‘Original RGB Image’);
subplot(122);
% Display RGB Image after conversion
imshow(rgb_new);
title(‘RGB Image after conversion’);
```

Figure: Recovering original RGB image using ntsc2rgb function

## Other similar built-in functions

Just like built in functions discussed above, MATLAB has some other built in functions having similar purpose.

 rgb2lab Convert RGB to CIE 1976 L*a*b* rgb2ntsc Convert RGB color values to NTSC color space rgb2xyz Convert RGB to CIE 1931 XYZ rgb2ycbcr Convert RGB color values to YCbCr color space lab2rgb Convert CIE 1976 L*a*b* to RGB lab2xyz Convert CIE 1976 L*a*b* to CIE 1931 XYZ xyz2lab Convert CIE 1931 XYZ to CIE 1976 L*a*b* xyz2rgb Convert CIE 1931 XYZ to RGB ycbcr2rgb Convert YCbCr color values to RGB color space ntsc2rgb Convert NTSC values to RGB color space

#### Creating custom functions for changing colorspace

MATLAB built in functions (discussed above) use the matrix equations to convert the color spaces. We can build our own functions for converting one color space to another, following the same idea.

#### RGB to YUV Color Space

```% Load image
% Extract R, G and B components
R = RGB(:,:,1);
G = RGB(:,:,2);
B = RGB(:,:,3);
% Apply matrix equation to convert colorspace
[Y;U;V] = [0.299, 0.587, 0.114; -0.147, -0.289, 0.436; 0.615,&nbsp; -0.515, -0.100]*[R;G;B];
% Create new image
YUV = RGB; % copy original image
YUV(:,:,1) = Y;
YUV(:,:,2) = U;
YUV(:,:,3) = V;
imshow(YUV,[]);
```

#### YUV to RGB Color Space

Instead of finding new matrix equation, we will just use the concept of inverse of matrix to find out RGB components from given YUV components.

```% Continue from previous example
% Apply matrix equation to convert colorspace
conv_matrix = [0.299, 0.587, 0.114; -0.147, -0.289, 0.436; 0.615, -0.515, -0.100]; % Matrix used for conversion
% Find inverse of the conversion matrix
conv_matrix_new = conv_matrix^(-1);
[R;G;B] = conv_matrix_new*[Y;U;V];
% Create new image
RGB_new = YUV; % copy original image
RGB_new(:,:,1) = R;
RGB_new (:,:,2) = G;
RGB_new (:,:,3) = B;
imshow(RGB_new,[]);
```

Similarly, for other color space transformations, custom functions can be made using matrix equations.

#### Chromaticity diagram

Chromaticity is an objective specification of the quality of a color regardless of its luminance. Chromaticity consists of two independent parameters, often specified as hue (h) and colourfulness (s), where the latter is alternatively called saturation, Chroma, intensity, or excitation purity.

Chromaticity diagram basically shows all possible colors than can be produced in that color space. It is prepared by following these steps:

• For every wavelength in the spectrum, calculate (XYZ) from the CIE color matching functions.
• From XYZ, calculate (x, y)
• Plot (x,y) for all wavelengths in spectrum.
• This generates a horseshoe shaped diagram.

All physical colors lie inside the horseshoe.

Figure: Chromaticity Diagram for CIE. (Source: hyperphysics)

##### Anushi Maheshwari

Anushi is pursuing graduation degree in Electronics & Communication from HBTI, Kanpur (Now HBTU). She has a keen interest towards Competitive Programming & loves to solve coding Challenges.

### Recommended Posts

##### Test Post

17 Feb 2018 - Tutorial

##### Counters using conditionally executed subsystems | Simulink Tutorial

16 Feb 2018 - Simulink, Tutorial

##### Regulating drum boiler pressure

16 Feb 2018 - Simulink, Tutorial