0% found this document useful (0 votes)
28 views3 pages

Understanding Images in Matlab: 1 Colormaps

This document provides an overview of working with images in Matlab. It discusses colormaps, which are arrays that define colors used to display indexed images. Images can be displayed using image() or imagesc() and come in three types: indexed using a colormap, intensity using scalar values mapped to a colormap, or truecolor RGB images. Examples are given to load image data from files, determine image properties, and display images using different colormaps. Functions like imread and imwrite can be used to read images from and write images to file formats like JPEG, BMP, and TIFF.

Uploaded by

Bogdan Manea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Understanding Images in Matlab: 1 Colormaps

This document provides an overview of working with images in Matlab. It discusses colormaps, which are arrays that define colors used to display indexed images. Images can be displayed using image() or imagesc() and come in three types: indexed using a colormap, intensity using scalar values mapped to a colormap, or truecolor RGB images. Examples are given to load image data from files, determine image properties, and display images using different colormaps. Functions like imread and imwrite can be used to read images from and write images to file formats like JPEG, BMP, and TIFF.

Uploaded by

Bogdan Manea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Understanding Images in Matlab

April 3, 2007

Colormaps

A colormap is an m 3 array, each row of which contains the [R G B] levels of an individual


color using numbers in the range 0 to 1: For example, the row [1 0 0] represents red, [0 1 0]
represents green and [0 0 1] represents blue, [1 1 1] represents white, [:5 :5 :5] represents
medium gray and so on.
Using colormaps
The MATLAB statement colormap(M) installs the matrix M to be used as the colormap
in the current Figure window.
Displaying colormaps
The pcolor function can be used to display a color map. For example, the statements
>>c=[1 0 0]
>>colormap(c)
>>pcolor([1:2;1:2])
produce a gure consisting of one rectangle painted in red.
Built in colormaps
Matlab has a number of buit in colormaps, each one, by default is a 64 3 array. The
one that most concerns us is the gray color map. It displays monochorme graphics
ranging from black ([0 0 0]) to white ([1 1 1]) in 64 grades. To see the contents of the
gray colormap type
>>gray
Other colormaps are hot, cool, winter, summer,.... These colormaps accept an integer
argument m. For example, the statement
>>hot(8)
rescales the range of colors of the colormap hot into 8 colors instead of 64. To see these
colors type
>>colormap(hot(8))
>>pcolor([1:9;1:9])
1

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.

You might also like