Understanding Images in Matlab: 1 Colormaps
Understanding Images in Matlab: 1 Colormaps
April 3, 2007
Colormaps
Images
Images are displayed using two statements: image() and imagesc(). We will get familiar
with these statements in what follows. An image in Matlab is a data matrix that contains
information about the pixels in the image. Usually a colormap is needed to display an image.
There are three types of image data matrices: indexed images, intensity images and RGB
images.
1. Indexed images: If an image is represented by a matrix X in indexed mode, then the
element Xij represents the color of the pixel Pij as an index into the colormap. For
example, if Xij = k; then the color of pixel Pij is the color represented by row k of the
color map cmap. This means that the values in the matrix X must be integer values
in the range [1 length(cmap)].
2. Intensity images: This type is usually used in monochormatic displays, e.g. gray
colormap. Data in the matrix X do not have to be of any specic numerical type and
they are rescaled over a given range and the result is used to index into the colormap.
For example, the statements
>>imagesc(X, [0 1]); colormap(gray)
assigns the smallest value 0 to the rst color (black) and the last value 1 to the last
color (white). Values in X are then used to obtain an index into the color map by
proportionality. If scale is omitted, its default is [min(min(X)), max(max(X))].
3. True colot or RGB images are created from m n 3 arrays containing RGB triples.
So X (i; j; :) contains the RGB values that specify the color of pixel Pij : In this case
no colormap is needed. To display such an image we use
>> image(X)
The following examples should make the above discussion clear.
>> load clown
%load a le called clown.mat (the image)
>> who
%determine the variables associated with
clown
X
caption
map
>> size(X)
%determine the size of X
200
320
(this means that clown is indexed)
>>X(20,160)
77
(the color of pixel (20,160) in the clown
image is map(77,:))
>>map(77,:)
.9961
.8672
.7031
(a whitish color)
>>image(X);colormap(map)
%display the image of the clown using the
colormap "map"
>>colormap(gray)
%cahnge the colormap to black and white.
2.1
Image Files
Image data can be saved to les and reloaded into Matlab using the save load commands.
For example, if youve created an image data matris X use
>>save myimage.mat
X
to save it. If youve created a nonstandard colormap (say "map") use
>>save myimage.mat
X
map
to save the colormap. To reload your image, use
>>load myimage
If no colormap was saved, the current colormap is used.
Matlab also supports several industry standard image le formats using the "imread"
and imwrite "functions". Information about the contents of a graphics le can be obtained
using the "imnfo" function. These three image functions support the formats: bmp, hdf,
jpg (or jpeg), pcx, tif (or ti), xwd.
The calling syntax for imread is the following.
[X, map]=imread(lename,fmt)
for indexed images
X=imread(lename,fmt)
for intensity images or truecolor images
Following are some examples
>> J = imread(cat.jpg);
%read truecolor image data from a JPEG
le
>> [X, map] = imread(icon.bmp,bmp)
%read bitmap image and colormap
>> G = imread(grayday,ti)
%read grayscale intensity image
>> [H, hmap] = imread(hootie.ras,hdf)
%read hdf image and colormap
The calling syntax for imwrite is the following
imwrite(X,map,lname,fmt)
for writing an image X and its associated colormap
map into a le named lename
imwrite(X,lname,fmt)
for writing intensity images or truecolor image in X into
lename.