This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
T'e goal of t'is la-orator or is to get 'ands,on e)perience it' image processing. To do
so ou ill 'a"e to learn t'e image,processing en"ironment# MATLAB and t'e DIPimage
tool-o) for multi,dimensional image processing. To facilitate a 5uic start t'ere ill not -e
an in,dept' e)planation of all t'e features in t'e softare. 6e ill -riefl present t'e t'ings
ou need at t'is moment. 6e 'a"e mared t'e sections t'at e)plain somet'ing a-out t'e
en"ironment it' t'e sm-ol so t'at t'e stand out. Let us start it' a s'ort introduction to
t'e MATLAB en"ironment.
( 6AT7A8
T'is section is to mae ou familiar it' MATLAB7 if ou alread are sip t'is section.
MATLAB is a computing8programming en"ironment especiall designed to or it' data
sets as a 'ole suc' as "ectors matrices and images. T'ese data sets can -e treated t'e same
as a mat'ematical e)pression of scalar "aria-les. MATLAB pro"ides onl a command lineand grap'ics capa-ilities. T'e idea is t'at ou gi"e it commands t'roug' t'e command line
'ic' are e)ecuted immediatel. T'e MATLAB snta) ill -ecome clear during t'is
la-orator session. +ere 9ust a fe s'ort comments#
a = b;
ill cause 'ate"er is in "aria-le b $a scalar or an image& to -e copied into "aria-le a.
6'ate"er as in "aria-le a gets lost. If ou omit t'e semicolon at t'e end of t'e command
t'e ne contents of a ill -e printed $scalars "ectors and matrices ill printed in t'e
command indo 'ereas images displaed in a separate indo&. If max is t'e name of a
function t'en
a = max(a,b);
ill call t'at function it' t'e "alues a and b as its parameters. T'e result of t'e function
$its return "alue& ill -e ritten into a o"erriting its pre"ious contents. If no e)plicit
assignment is done t'e output of a function ill -e put into a "aria-le called ans#
max(a,b);
is t'e same as
ans = max(a,b);
It is possi-le to use t'e result of a function call as a parameter in anot'er function#
a = max(max(a,b), max(c,d));
% DIPimage
DIPimage is t'e tool-o) e ill -e using under MATLAB to do image processing. T'is
section taes ou t'roug' t'e most rele"ant features.
:ou ma 'a"e noticed t'e indos t'at appeared around t'e screen 'en ou started
MATLAB. T'e one on t'e top,left is t'e ;%I $;rap'ical %ser Interface&. T'e ot'er indos
are t'e ones used to displa t'e images in. T'e ;%I contains a menu -ar. <pend some time
e)ploring t'e menus. 6'en ou c'oose one of t'e options t'e area -eneat' t'e menu -ar
c'anges into a dialog -o) t'at allos ou to enter t'e parameters for t'e function ou 'a"ec'osen#
T'ere are to as of using t'e functions in t'is tool-o). T'e first one is t'roug' t'e ;%I
'ic' maes it eas to select filters and its parameters. 6e ill -e using it "er often at first
-ut graduall less during t'e course of t'e afternoon. T'e ot'er met'od is t'roug' t'e
command line.
6'en sol"ing t'e pro-lems in t'is la-orator session e ill -e maing te)t files t'at
contain all commands e used to get to t'e result $e ill call t'em e)ercise command files&.T'is maes t'e results reproduci-le. It ill also a"oid lots of repetiti"e and tedious or. 6e
recommend ou mae a ne file for eac' e)ercise and gi"e t'em names t'at are eas to
recogni=e.
If ou start eac' file it' t'e commands
clear
dipclf
t'en t'e "aria-les and t'e figure indos ill -e cleared -efore our commands are
e)ecuted. T'is a"oids undetected errors.
Edit a MATLAB command file
To open t'e editor tpe
edit
T'e MATLAB editor ill -e started. 6e ill tpe $or cop8paste& t'e commands e ant to
e)ecute in it. T'ere is a >?un@ menu item under t'e >Tools@ menu. It can -e used to let
MATLAB run t'e file currentl -eing edited. :ou can also e)ecute t'e script - tping its
6e need to load an image $from file& into a "aria-le -efore e can do an image processing.
T'e left,most menu is called >0ile I8@ and its first item >?ead image $readim&@. <elect it.
Press t'e >Brose@ -utton and c'oose t'e file cermet.ics. 'ange t'e name of t'e output
"aria-le from ans to a. Co press t'e >E)ecute@ -utton. To t'ings s'ould 'appen#1& T'e image cermet is loaded into t'e "aria-le a and displaed to some figure
» a = readim('c:\matlab\toolbox\dipimage\images\cermet.ics','')
Displayed in figure !
T'is is to s'o ou t'at e)actl t'e same ould 'a"e 'appened if ou 'ad tped t'at
command directl in t'e command indo. Tr tping t'is command#
b = readim('cermet')T'e same image ill -e loaded into t'e "aria-le b and again displaed in a indo. Cote t'at
e omitted t'e '.ics' e)tension to t'e filename. readim can find t'e file it'out ou
'a"ing to specif t'e file tpe. 6e also didnt specif t'e second argument to t'e readimfunction since '' denotes t'e default "alue. 0inall - not specifing a full pat' to t'e file
e ased t'e function to loo for it eit'er in t'e current director or in t'e default image
director.
op t'e command as printed - t'e ;%I into t'e editor $6indos# select it' t'e mouse
trlF to cop t'e te)t7 go to t'e editor trlFG to paste. %ni)# select it' t'e mouse go to
t'e editor and clic it' t'e middle mouse -utton to paste.&
To suppress automatic displa of t'e image in a indo add a semicolon to t'e end of t'ecommand#
a = readim('"dna');
Cote t'at t'e contents of a c'anged -ut t'e displa is not updated. To update t'e displa
simpl tpe#
a
' Image filtering
T'e >0ilters@ menu contains a series of image filters. A filter is a neig'-or'ood operations in'ic' t'e output "alue is a function of t'e "alues in a local indo around t'e current pi)el.
B multipling t'e image - to e multipl -ot' t'e lo and 'ig' fre5uencies. T'e lo
fre5uenc components are t'en su-tracted again t'us remaining in t'eir original intensit.nl t'e 'ig',fre5uenc components are effecti"el multiplied - to.
T'e function dip-ist $under t'e ><tatistics@ menu& plots t'e 'istogram of an image. pen a
ne image# sca7 in t'is image are -loodcells of a sicle cell anaemia patient s'on. Plot t'e
'istogram of t'e image sca. :ou ill notice t'at t'e loer / gre,"alues are not used as
are t'e upper 4. orrect t'is using t'e function stretc- $under t'e >Point@ menu&. To see t'e
difference it' t'e original image mae sure t'at t'e displa mode is set to >Cormal@. Plot
t'e 'istogram of t'e ne image.
T'is stretc'ing met'od is "er sensiti"e to noise. 0or e)ample set a single pi)el in t'e
original image a to 1. T'is can -e accomplis'ed - inde)ing. It is not "er important 'o
t'is ors e)actl since manipulating an image in t'is a is not part of t'e course. Tpe#
a(!,!) = !
Co plot t'e 'istogram again. :ou ill not notice t'e difference. +oe"er t'e stretc'ing
algorit'm ill. Plot t'e 'istogram of t'e stretc'ed image to see t'is. 6' is t'e loer part of
t'e 'istogram flatH
?epeat t'e pre"ious se5uence of commands it' t'e loer and upper percentiles in t'e stretc'
function 1 and // respecti"el. T'is causes t'e loer and upper 1 of t'e gre,"alues to -e
clipped -efore stretc'ing.
T'e 'istogram of sca is also "er poorl distri-uted. T'is is a feature of most images. It
simpl means t'at some gre,"alues occur more often in t'e image t'an ot'ers. <ometimes
t'is is not desira-le for e)ample 'en comparing images ac5uired under different lig'ting
circumstances. Appl t'e -ist&e"ualie function $also on t'e >Point@ menu& to t'e image
JscaJ and t'en plot t'e 'istogram again. Is in t'is image a stretc' or an e5uali=ation of t'e
'istogram prefera-leH
a = readim('sca') b = -ist&e"ualie(a)
?epeat t'e pre"ious se5uence of commands it' t'e image maan one of t'e earl images of
our moon.
a = readim('maan')
b = -ist&e"ualie(a)
Plot t'e 'istogram -efore and after 'istogram e5uali=ation. Is in t'is image a stretc' or an
e5uali=ation of t'e 'istogram prefera-leH
Thresholding
Load and stretc' t'e image sca again into "aria-le a. T'e o-9ects in it are clearl defined
and are eas to segment. T'e t-res-old function contains a couple of different algorit'ms to
do t'is. T'e simplest one fi)ed re5uires ou to pro"ide a parameter# t'e t'res'old le"el.
T'e ot'er t'ree estimate t'is parameter -ased on t'e 'istogram of t'e image in different as.
0or t'is image t'e parameter is not "er critical7 lets use 1. T'e red portion of t'e
segmented image is t'e o-9ect and t'e -lac is t'e -acground. n t'is image it goes all
rong# t'e -acground 'as -ecome t'e o-9ect and t'e particles 'a"e -ecome 'oles in t'e
o-9ect. T'at is -ecause gre,"alues larger t'an t'e t'res'old are considered as part of t'e
o-9ect. ne solution is to in"ert t'e image -efore or after t'res'olding. T'is can -e done it't'e negation operator for t'e -inar image $/t-res-old(a,'fixed',!!)& or t'e minus
operator for t'e gre,"alue image $t-res-old(a,'fixed',!!) note 'o t'e t'res'old
"alue s'ould also -e c'anged&. Anot'er a is to c'ange t'e t'res'olding operation. 6e can
use t'e relational operators to do t'res'olding $ 0 1 == /= etc&. In our case#
b = a 0 !!
Co e 'a"e a -inar image recogni=a-le - t'e red coloring of t'e displa. T'is leads us to -inar morp'olog.
) 8inary morphology
T'ere are dedicated operations for -inar images. T'e point operations not $/& or $2&
and $3& )or $xor& can onl -e issued directl onto t'e command line. T'e -inar
morp'ological filters can -e found under t'e >Binar 0ilters@ menu.
0or e)ample e can use t'e >Binar pening@ $ bopening) to remo"e small o-9ects. In t'e
same a e can use bclosing to remo"e small 'oles in t'e o-9ects. 6'at is t'e effect of
t'e >Edge condition@H 6'at connecti"it gi"es t'e -est results for t'is imageH 6'at is t'eeffect of t'e specified num-er of iterationsH
Co load t'e image sca stretc' and t'res'old it $K1*!&. Mae sure t'at t'e t'res'olding
operation retains t'e 'ole o-9ects. T'e cells 'ic' are not full inside t'e picture s'ould -e
remo"ed. T'is can -e done - t'e function remo"e edge o-9ects $under >Binar filters@&.
6e ill use t'e seleton operations to separate t'e 'ealt' from t'e sicle cells.
%se t'e bs4eleton function $under t'e >Binar 0ilters@ menu& to create a seleton of t'e
o-9ects. 6'at is t'e influence of t'e Edge onditionH 6'at does End,Pi)el ondition
controlH
<ince most of t'e normal cells 'a"e a reflection 'ic' is also "isi-le in t'e t'res'oldedimage t'e seleton image s'os a circle for a normal cell. A sicle cell ill s'o a straig't
T'e picture re"eals one straig't line se"eral ellipses and some cur"ed lines $originating from
to cells o"erlapping&.
* The Fourier transformLoad t'e image 5dna1 into "aria-le a. %nder t'e >Transforms@ menu ou ill find t'e
forard and in"erse 0ourier transforms. Appl t'e forard transform to t'e image in "aria-le
a, and store t'e 0ourier spectrum in "aria-le b.
b = ft(a)
T'e result loos lie a 'ite cloud in a -lac -acground7 t'is is -ecause t'e default displa
mapping is not t'e most ade5uate. Tr linear stretc'ing. Co all ou 'a"e left is a single dot
in t'e middle. T'e dnamic range is "er large. Logarit'mic stretc'ing is usuall emploed to
loo at 0ourier spectra.
Co sitc' on t'e >Pi)el testing@ mode and loo at t'e "alues in t'e spectrum. T'e "alues
are comple). 6'at ou see as an image is 9ust t'e amplitude of t'e spectrum. Co e ill
decompose it into a real and an imaginar part. 6e ill do t'is t'roug' t'e real and imagcommands $t'ese are not in t'e menus&. 6rite t'ese and t'e pre"ious commands into a
command file $e assume t'at - is t'e 0ourier spectrum&#
re = real(b)
im = imag(b)
:ou ill notice t'at t'e image im contains real "alues. 6e need to multipl it - i. If ou
o"errote "aria-le i it' an image ou can use . If ou also o"errote it clear t'em it'
Co e are read to do some measuring. <elect t'e measure function in t'e >Analsis@
menu. T'e -9ect image is t'e la-eled image la. T'e ;re,"alue image is t'e original
image -efore segmentation7 it ont -e used - t'e measurements e ill mae so it can -e
left out. <elect 'sie' as t'e measurement. If ou lea"e -9ect IDs empt all o-9ects ill
-e measured. Put t'e output in a "aria-le called data. Co
s = data.sie
is a MATLAB arra it' t'e si=es of t'e o-9ects. Co tpe
dip-ist(s,#,*!!!%,!!)
T'is ill create a 'istogram for t'e si=es. T'ere are o-"iousl to si=e categories. Lets sa
t'at si=es up to 1 are for t'e nuts and larger si=es for t'e -olts. T'ere e)ist as of doing
t'is automaticall -ut e ont go into t'em no. 6e ill use t'e function mrs*ob to
paint eac' o-9ect it' t'eir measured si=e. 'oose t'e la-el image as t'e input and dataas t'e measurement data. 6e can no t'res'old t'is image at t'e c'osen "alue of 1 to
retrie"e t'e -olts. T'e nuts can -e o-tained - xor,ing t'e original -inar image and t'e -olts
image#nuts = xor(b,bolts)
6e could do t'e same t'ing it' ot'er measurements of t'e o-9ects lie t'e lengt'
$'feret'& t'e si=e of -ounding -o) $'dimension'& or t'e perimeter $'perimeter'&. Tr