Different Color Map and their transformation in MATLAB | MATLAB Tutorial

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.

Colormaps in MATLAB

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

ColorMap

Figure 1: Image after applying parula colormap

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

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

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:

ColorMap

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.

ColorMap

Original image

ColorMap

Image using hot colormap

ColorMap

Image using jet colormap

ColorMap

Image using imagesc

 

Figure 4: Application of colormap in scientific visualization


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.

Comments

  1. […] especially in order to improve its quality. It helps in improving its pictorial information for human interpretation, rendering it more suitably for autonomous machine perceptions and effective storage and […]

Comments are closed.