# Different Color Map and their transformation in MATLAB | MATLAB Tutorial

### What is a Color Map?

A Color Map is an m-by-3 matrix of real numbers between 0.0 and 1.0. Each row is an RGB vector that defines one color. The kth row of the Color Map defines the kth color, where map(k,:) = [r(k) g(k) b(k)]) specifies the intensity of red, green, and blue.

MATLAB applies these color maps by mapping data values (in images, plots, surfaces, etc.) to the values (or colors) in the map.

Thus, if we take a sample colormap:

myMap = [0 0 0; % black

1 0 0; % red

0 1 0; % green

0 0 1; % blue

1 1 1;] % white

This means that it will map all the data values to either black, red, green, blue or white.

#### Built-in Colormaps

MATLAB has a large variety of colormaps available by default.

#### Custom colormaps (making your own colormap)

In addition to the available colormaps, one can define his/her own custom Colormap as follows:

myMap = [0 0 0; % black

1 0 0; % red

0 1 0; % green

0 0 1; % blue

1 1 1;] % white

This colormap can then be applied to an image or a plot etc.

### Applying colormaps in MATLAB

#### Using built-in function: colormap

The syntax for using colormap is:

```colormap(map_name)
```

Note: Colormaps can only be applied to grayscale images. So, before applying a colormap to an image, make sure that it is a grayscale image. To be on the safe side, add the following code snippet to convert an image to grayscale (if it is not already one):

```if (length(size(img)) &amp;gt; 2)
img = rgb2gray(img);
end
```

Let’s apply the following colormaps to some images:

1. Parula – (default)
```img = imread(‘test_img.jpg’); %reading image
img = rgb2gray(img); %converting to grayscale
imshow(img)
colormap(parula) %applying colormap
```

Figure 1: Image after applying parula colormap

1. Jet
```img = imread(‘test_img2.jpg’); %reading image
imshow(img)
colormap(jet) %applying colormap
```

Figure 2: Image after applying jet colormap

1. HSV (Hue, Saturation, Value) – Commonly used by artists
```img = imread('cameraman.tif'); %reading image
imshow(img)
colormap(hsv) %applying colormap
```

Figure 3: Image after applying HSV Colormap

#### Applying custom colormaps

We can apply custom color maps to a sample image (lena.jpg) as follows:

myMap = [0 0 0; % black

1 0 0; % red

0 1 0; % green

0 0 1; % blue

1 1 1;] % white

```img = imread('lena.jpg'); %Reading image
img = rgb2gray(img); %Converting the image to grayscale
imshow(img)
colormap(myMap) % Applying the colormap
```

Results:

Figure 3: Image after applying colormap (myMap)

#### Use of colormaps

The human perception isn’t built for observing fine changes in grayscale images. Human eyes are more sensitive to observing changes between colors, so you often need to recolor your grayscale images to get a clue about them. Colormaps make this possible for us.

Similarly, colormaps can also come in handy in scientific visualization. Sometimes, it’s not possible to extract data from grayscale images. In such a case, pseudo coloring using colormap is a good alternative.

Original image

Image using hot colormap

Image using jet colormap

Image using imagesc

Figure 4: Application of colormap in scientific visualization

##### 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