shiva
shiva

Reputation: 2699

How does colormap work

I can't understand how does colormap work and how is it used.

I am completing a programming assignment, where I am provided with some code in which colormap is used, however I can't understand what exactly does it accomplish.

Here is a condensed code:

colormap(gray);
h = imagesc(display_array, [-1 1]);

Here display_array contains the pixel intensities of a grayscale image.

I read this article and slightly understood that colormap is a matrix with any number of rows and 3 columns with values between0 and 1.

I checked the values of the pixel intensities in my grayscale image and found values between -1 and 2. Now I can't understand how mapping occurs between this image and the colormap as mentioned in the above article. Just a high level idea of how this happens would help.

Upvotes: 4

Views: 2718

Answers (1)

Suever
Suever

Reputation: 65430

When displaying an image, it is necessary to establish a relationship between data values and the color of the pixels on the screen that correspond to those values. This is the purpose of the colormap. It literally maps a data value to a color.

How this mapping occurs depends upon the color limits of the axes. The color limits indicate which value of your data to map to the first value in the colormap and which to map to the last value in the colormap. You can adjust these limits for a given axes via the CLim property or via the caxis function.

By default imagesc (a scaled image) will set the color limits such that the biggest value of your data is used for the upper color limit and the smallest value is used for the lower color limit. This is the equivalent of

set(gca, 'CLim', [min(display_array(:)), max(display_array(:))])

In the case of the grey colormap, the first value is black and the last value is white meaning that any values less than or equal to the lower color limit will be shown as black, any values greater than or equal to the upper color limit will be shown as white and everything in between will be a shade of gray proportional to their value.

You can use the colormap function to specify any colormap that you want. grey is just a built-in colormap for grayscale images. The format of the colormap is that each row contains three elements (red, green, blue) and the lower color limit is mapped to the first entry, the upper color limit is mapped to the last and data is mapped linearly to all colors that may appear in between the two.

In the example that you have shown, however, you have specified the color limits as the second input argument to imagesc and manually forced them to be [-1 1]. This means that now -1 is mapped to the first value in the colormap (black) and 1 is mapped to the last (white). Since you have values larger than 1, all of these values will be forced to be white since the upper color limit is 1.

Upvotes: 8

Related Questions