- To MATLAB, any 2D array can be represented as an image, with each element of the array representing a single pixel(picture element) in that image.
- This is most commonly done with the imagesc function.
To understand this function, we must first review the concept of colormaps.
- A colormap is a table establishing the relationship between a matrix element's value and the color of the pixel representing it.
- MATLAB provides a series of predefined colormaps, but it is also easy to define a new one.
- Each MATLAB colormap is a 64 x 3 matrix, in which each row contains the red, green, and blue (RGB) components of a single color.
- Thus MATLAB colormaps can represent up to 64 different colors in all.
- A simple MATLAB colormap is 'gray'. This table maps numeric values between 0 and 1 to 64 shades of gray between black and white.
- The current colormap for a figure is set using the colormap function, and the list of MATLAB colormaps is provided by the 'help colormap' command.
The imagesc function (short for "image scale") accepts the data to be displayed as an argument, determines its span from minimum and to maximum values, and maps this range of values onto the 0-1 input range of the color map.
Thus imagesc will display the matrix you pass it using the entire colormap range.
Since white, black, and shades of gray all have R, G, and B values in equal proportion, the 'gray' colormap can be defined so:
>> values = [0:1/63:1].';
>> gray = [values values values];
MATLAB includes some images for demonstrations that can also be used for exercises. (Image ©2002 Mathworks, Inc.) Here we examine one of these images with two different colormaps, and add a colorbar to show the relationship of color to the underlying data:
load spine % Load a medical image included with MATLAB
imagesc(X) % Display a scaled image of the data, held in matrix X
colormap(gray) % Select the gray scale colormap
colormap(bone) % Select the color map for X-ray images
colorbar % Display the color bar