You are on page 1of 9

12/16/11 Introduction to image processing in Matlab 1

1/9 amath.colorado.edu/courses/5720/2000Spr/Labs//matlabimpr.html

Introduction to image processing in Matlab 1
b Kristian Sandberg, Department of Applied Mathematics, Universit of Colorado at Boulder
Introduction
Thi hee i a idci h hade iage i Maab. Whe ig ih iage i Maab,
hee ae a hig ee i id ch a adig a iage, ig he igh fa, aig he daa a diffee
daa e, h dia a iage, cei beee diffee iage fa, ec. Thi hee ee
e f he cad deiged f hee eai. M f hee cad eie hae he Image
processing tool bo iaed ih Maab. T fid if i i iaed, e er a he Maab . Thi gie
a i f ha be ha ae iaed e.
F fhe efeece iage hadig i Maab ae eceded e Maab' he be. Thee i
a eeie (ad ie gd) -ie aa f he Iage ceig b ha ca acce ia Maab'
he be.
The fi eci f hi hee ae ie hea. The a dead h he eeed cad
, i caef hgh he eae gie a he ed f he hee. Oce ca ge hee
eae , eeie ig faie iage!
Fundamentals
A digia iage i ced f piels hich ca be hgh f a a d he cee. A digia iage i a
ici f h c each ie. We i ee i deai ae h hi i de i acice. A ica ie f
a iage i 512-b-512 ie. Lae i he ce i ee ha i i ceie e he diei f
he iage be a e f 2. F eae, 2
9
=512. I he geea cae e a ha a iage i f ie m-b-n
if i i ced f m ie i he eica dieci ad n ie i he hia dieci.
Le a ha e hae a iage he fa 512-b-1024 ie. Thi ea ha he daa f he iage
cai ifai ab 524288 ie, hich eie a f e! Hece, compressing iage i
eeia f efficie iage ceig. Y i ae ee h Fie aai ad Waee aai ca he
ce a iage igifica. Thee ae a a fe "ce cieific" ic (f eae e
cdig) edce he a f daa eied e a iage.
Image formats supported b Matlab
12/16/11 Introduction to image processing in Matlab 1
2/9 amath.colorado.edu/courses/5720/2000Spr/Labs//matlabimpr.html
The fig iage fa ae ed b Maab:
BMP
HDF
JPEG
PCX
TIFF
XWB
M iage fid he Iee ae JPEG-iage hich i he ae f e f he ide ed
cei adad f iage. If hae ed a iage ca a ee f he ffi ha fa i
i ed i. F eae, a iage aed mimage.jpg i ed i he JPEG fa ad e i ee ae
ha e ca ad a iage f hi fa i Maab.
Working formats in Matlab
If a iage i ed a a JPEG-iage dic e fi ead i i Maab. Hee, i de a
ig ih a iage, f eae ef a aee af he iage, e ce i i a diffee
fa. Thi eci eai f c fa.
Intensit image (gra scale image)
Thi i he eiae a "ga cae iage" ad hi i he iage e i ih i hi ce. I
eee a iage a a ai hee ee eee ha a ae cedig h bigh/da he ie a
he cedig ii hd be ced. Thee ae a eee he be ha eee he
bighe f he ie: The double ca ( daa e). Thi aig a faig be ("a be ih
decia") beee 0 ad 1 each ie. The ae 0 ced bac ad he ae 1 ced
hie. The he ca i caed uint8 hich aig a iege beee 0 ad 255 eee he bighe f
a ie. The ae 0 ced bac ad 255 hie. The ca uint8 eie gh 1/8 f he
age caed he ca double. O he he had, a aheaica fci ca be aied
he double ca. We i ee ae h ce beee double ad uint8.
Binar image
Thi iage fa a e a iage a a ai b ca c a ie bac hie (ad hig i
beee). I aig a 0 f bac ad a 1 f hie.
Indeed image
12/16/11 Introduction to image processing in Matlab 1
3/9 amath.colorado.edu/courses/5720/2000Spr/Labs//matlabimpr.html
Thi i a acica a f eeeig c iage. (I hi ce e i ih ga cae iage
b ce hae eaed h ih a ga cae iage i a he icie h
ih c iage.) A ideed iage e a iage a aice. The fi ai ha he ae ie a he
iage ad e be f each ie. The ecd ai i caed he color map ad i ie a be diffee
f he iage. The be i he fi ai i a ici f ha be e i he c a ai.
RGB image
Thi i ahe fa f c iage. I eee a iage ih hee aice f ie achig he iage
fa. Each ai ced e f he c ed, gee be ad gie a ici f h ch f
each f hee c a ceai ie hd e.
Mlifame image
I e aicai e a d a eece f iage. Thi i e c i bigica ad edica
iagig hee igh d a eece f ice f a ce. F hee cae, he ifae fa i a
ceie a f ig ih a eece f iage. I cae che ih bigica iagig ae
i hi ce, a e hi fa.
Ho o cone beeen diffeen foma
The fig abe h h ce beee he diffee fa gie abe. All these commands
require the Image processing tool bo!
Image foma coneion
(Wihi he aehei e he ae f he iage ih ce.)
Opeaion: Malab command:
Ce beee iei/ideed/RGB fa bia fa. dither()
Ce beee iei fa ideed fa. gra2ind()
Ce beee ideed fa iei fa. ind2gra()
Ce beee ideed fa RGB fa. ind2rgb()
Ce a ega ai iei fa b caig. mat2gra()
Ce beee RGB fa iei fa. rgb2gra()
Ce beee RGB fa ideed fa. rgb2ind()
The cad mat2gra i ef if hae a ai eeeig a iage b he ae eeeig he ga
cae age beee, e' a, 0 ad 1000. The cad mat2gra aaica e cae a eie ha
12/16/11 Introduction to image processing in Matlab 1
4/9 amath.colorado.edu/courses/5720/2000Spr/Labs//matlabimpr.html
he fa ihi 0 ad 255 (if e he uint8 ca) 0 ad 1 (if e he double ca).
Ho to conert beteen doble and in8
Whe e a iage, hd e i a a uint8 iage ice hi eie fa e e ha double.
Whe ae ceig a iage (ha i efig aheaica eai a iage) hd ce
i i a double. Ceig bac ad fh beee hee cae i ea.
I=im2double(I);
ce a iage aed I f uint8 double.
I=im2uint8(I);
ce a iage aed I f double uint8.
Ho to read files
Whe ece a iage a ih, i i a i f f a fie (f eae, if d ad
a iage f he eb, i i a ed a a JPEG-fie). Oce e ae de ceig a iage, e a
a ie i bac a JPEG-fie ha e ca, f eae, he ceed iage he eb. Thi i
de ig he imread ad imrite cad. These commands require the Image processing tool bo!
Reading and riting image files
Operation:
Matlab
command:
Read a iage.
(Wihi he aehei e he ae f he iage fie ih ead.
P he fie ae ihi ige e ' '.)
imread()
Wie a iage a fie.
(A he fi age ihi he aehei e he ae f he iage hae ed
ih.
A a ecd age ihi he aehei e he ae f he fie ad fa ha
a ie he iage .
P he fie ae ihi ige e ' '.)
imrite( ,
)
Mae e e ei-c ; afe hee cad, heie i ge LOTS OF be cig
cee... The cad imread ad imrite he fa gie i he eci "Iage fa
ed b Maab" abe.
12/16/11 Introduction to image processing in Matlab 1
5/9 amath.colorado.edu/courses/5720/2000Spr/Labs//matlabimpr.html
Loading and saving variables in Matlab
Thi ecion eplain ho o load and ae aiable in Malab. Once o hae ead a file, o pobabl cone
i ino an ineni image (a mai) and ok ih hi mai. Once o ae done o ma an o ae he
mai epeening he image in ode o conine o ok ih hi mai a anohe ime. Thi i eail done
ing he command sae and load. Noe ha sae and load ae commonl ed Malab command, and
ok independenl of ha ool boe ha ae inalled.
Loading and saving variables
Operation: Matlab command:
Sae he aiable X . sae X
Load he aiable X . load X
Eamples
In he fi eample e ill don load an image fom he eb, ead i ino Malab, ineigae i foma and ae
he mai epeening he image.
Eample 1.
Don load he folloing image (b clicking on he image ing he igh moe bon) and ae he file a
cell1.jpg.
12/16/11 Introduction to image processing in Matlab 1
6/9 amath.colorado.edu/courses/5720/2000Spr/Labs//matlabimpr.html
Thi i a iage f a ce
ae b a eec
icce a he
Deae f
Meca, Cea ad
Deeea Big
a CU.
N e Maa ad ae e ae i he ae diec a ed fie. (Y ca chec ha fie
diec cai b ig ls a he Maab . Y chage diec ig he cad cd.) N
e i he fig cad ad ee ha each cad de. (Of ce, d hae e i he
ce gie i he cde afe he % ig.)
I=imread('cell1.jpg'); % Load the image file and store it as the variable I.
whos % Tpe "whos" in order to find out the sie and class of all stored variables.
save I % Save the variable I.
ls % List the files in our director.
% There should now be a file named "I.mat" in ou director
% containing our variable I.
Ne ha a aiabe ha ae i Maab a ge he ffi .mat.
Ne e i ee ha e ca dia a iage ig he cad imshow. Thi cad eie he iage
12/16/11 Introduction to image processing in Matlab 1
7/9 amath.colorado.edu/courses/5720/2000Spr/Labs//matlabimpr.html
poceing ool bo. Command fo diplaing image ill be eplained in moe deail in he ecion "Ho o
dipla image in Malab" belo.
clea % Clea Malab' memo.
load I % Load he aiable I ha e aed aboe.
ho % Check ha i a indeed loaded.
imho(I) % Dipla he image
I=im2doble(I); % Cone he aiable ino doble.
ho % Check ha he aiable indeed a coneed ino doble
% The ne pocede c o he ppe lef cone of he image
% and oe he edced image a Ied.
fo i=1:256
fo j=1:256
Ied(i,j)=I(i,j);
end
end
ho % Check ha aiable o no hae oed.
imho(Ied) % Dipla he edced image.
Eample 2
Go o he CU home page and don load he image of camp ih he Rockie in he backgond. Sae he
image a pic-home.jpg
Ne, do he folloing in Malab. (Make e o ae in he ame dieco a o image file).
clea
A=imead('pic-home.jpg');
ho
imho(A)
Noe ha hen o ped ho i pobabl aid ha he ie a 3005043. Thi mean ha he image a
12/16/11 Introduction to image processing in Matlab 1
8/9 amath.colorado.edu/courses/5720/2000Spr/Labs//matlabimpr.html
loaded as an RGB image (see the section "RGB image above"). However, in this course we will mostly work
with gray scale images, so let us convert it into a gray scale (or "intensity") image.
A=rgb2gra(A); % Convert to gra scale
whos
imshow(A)
Now the size indicates that our image is nothing else than a regular matrix.
Note: In other cases when you down load a color image and type whos you might see that there is one matrix
corresponding to the image size and one matrix called map stored in Matlab. In that case, you have loaded an
indexed image (see section above). In order to convert the indexed image into an intensity (gray scale) image,
use the ind2gra command described in the section "How to convert between diIIerent Iormats" above.
Ho to displa an image in Matlab
Here are a couple oI basic Matlab commands (do not require any tool box) Ior displaying an image.
Displaing an image gien on matri form
Operation: Matlab command:
Display an image represented as the matrix X. imagesc(X)
Adjust the brightness. s is a parameter such that
-1<s<0 gives a darker image, 0<s<1 gives a brighter image.
brighten(s)
Change the colors to gray. colormap(gra)
Sometimes your image may not be displayed in gray scale even though you might have converted it into a gray
scale image. You can then use the command colormap(gra) to "Iorce" Matlab to use a gray scale when
displaying an image.
II you are using Matlab with an Image processing tool box installed, I recommend you to use the command
imshow to display an image.
Displaing an image gien on matri form (ith image processing tool bo)
Operation: Matlab command:
Display an image represented as the matrix X. imshow(X)
Zoom in (using the leIt and right mouse button). oom on
Turn oII the zoom Iunction. oom off
12/16/11 Introduction to image processing in Matlab 1
9/9 amath.colorado.edu/courses/5720/2000Spr/Labs//matlabimpr.html
Eercise
Load o faoie image ino Malab (if i i on an of he foma decibed in he ecion "Image foma
ppoed b Malab" aboe). No epeimen ih hi image, ing he command gien in hi okhee.

You might also like