You are on page 1of 236

Chapter 3

Curve Fitting and Statistics


One of the big differences between working on purely numerical solutions
and working with data from experiments is that the experimental signals are
often more complex than expected. This is not improved by the additional
noise usually found in measured signals. Analyzing this data often requires
that trends in the data be extracted and ideally expressed as a simple
mathematical function. Determining an appropriate mathematical func­
tion is the domain of the many curve-fitting techniques that are commonly
used.
Complex curves can also be created from many simpler mathematical
functions by adding them together. The individual components of these
synthesized curves can be explored separately. We have already shown that
there are ways to introduce noise into numerical functions and that the noise
in data can be filtered to reduce its impact. Adding noise to a synthesized
curve can make the curve more like an experimental data set.
The process of determining whether a curve fits a data set requires the
development of metrics to use for comparison. These metrics provide a measure
of the quality of the fit between the curve and the data. One simple metric that
we will develop will provide a "goodness of the fit" test. This simple test relies
on the curve through the data being, on average, equidistant from the curve so
that the sum is zero.
In this chapter we will introduce sorne simple tools for working with
complex signals contaminated with unwanted information. The appropriate
statistics such as the mean and the standard deviation will be used, and we will
go farther by showing that we can find the trend in the data and remove this
bias so that we can look in more detail at the noise signa!.

3.1 y>olynomial Synthesis and Curve Fitting


Mathematical functions can be summed over a common range of inputs' to
generate or synthesize a curve. Polynomial functions are composed of a number
of terms with a variable of interest raised to sorne power and a coefficient

37
t9
p3lEls sr ozrs 3lu e8erur ¡r{t eSurols 3tu Jo osodrnd áq} rol ezrs eli:
"{lrJEduJ
aql aJnpoi ot steltuoJ olu,fteurq JSn selg e8plur'lelouei uJ elqelr¿^e aulo¡¡c
sluaura,rolduu pu€ seinl€oJ alour pup erotu su Surpuedxa ,{lsnonurlnoc :q tr:
nl)as 3sn ur cle leql -sslg aSuurr jo sed.{t aq1 por:ad .ro top E reUB ,(1¡ensn pur'
elueuolu eq] urqlr,\\ pslprrpur sr elu oql Jo cd,{1 oq:¡ 'stuc1s,(s 8ur1e:cdo ,(ueru
ul selu eqt ,{uldsrp puu puel .{1}oa:ro: ot elqp cq o1 po:mbar ore sue¡8ord
cgnads 1er¡1 superu qlll,\\ 'se¡g .{:uurq e:e salg e?eur uorurrroJ lsoul ari-¡
solll 36eul l'9
'Irplep ur pa,ro¡dxa sr 'e8eur ue pue Jallu ¿ Jo uorlnloluo¡
CZ sr^lolur q:rq,tr '3ur:etlg e8eut ¡nrledg AV-11VI l Sursn sa8eurr sso¡o¡d
pue az,{¡uue ol suuJtrl snou¿^ eJnporlur ueql pue slprxroJ uoruuroc eroru
oql ur solu e8uurr etrep elu,\\ pue peer o1 ,\\otl uJ¿s[ IILU a,tr.tatduqr snp u1
'Jlqpsn pue alqrssocce o¡ou Surssa¡old aFeutt or¡uur ol sllo^Aqlel\
uro,U elqelre^p ele ,sexoqlool pc1lec suer?ord Jo suortJelloc cgrceds leqi
{.ror!\ Jo eoJ¿ tupuodur ur? qJns sr slql gv1lv¡l 3o serltltqedec eqt .{q
pslroddns IIe,r sr teql IIr{s lullutueqleur e sr Sursse¡o.td e3eru1 ,- elep oql rollr-I
pue llelndrueu o1 s¡r¡s Sursse:ord-¡euárs pue -sSerur selnber srq¿ . elep
alqusn Állecgrtuaros tJertxr o1 peutro3:ad aq lsnru IJo,{\ IEUotlrpp¿ :tuelJrlJns
lou sr errole e8uur u¿ Sur,teq ual3o clrnb 'Suueeut8rrJ pue ¡JuorJs rrJ
'solro ,\\eu ]JnJlsuoJ ueJ 1I lo
r's:lPLuJoJ
lurcraururor re¡ndod lsoru jo esn oleut uÉc,'gV'IJVtr{ pue'esn ur ,{¡uoturuor
selg eScuu Jo sod,{l ,(ueur e:e J-IJtlI sJeqlunu jo s,{e.r:n ol pollo^uo:r 'a r
'pezrlrBrp eq p¡noo ruer?o1or¡d rulU ¿ tsql po.ra^ooslp ls.rU,{ar¡t arurs sa¡g e?erur
lutr8rp jo onl€^ aql u,{\ouI a^pq sJourouoilsV alqrssod auroceq suorle8rlsa,rur
.loqlo puu c¡ucrcs Sultsa¡olur 'sa¡g a8eurr aql etelndrueur pue csn o1 ,(1r¡rqe
erll rpr^\ peurqruoJ uerl,u :sJrnlJld 1sn[ uer¡l e]orrr sr el¿p eSerur 3o acu€punq?
srqJ 'sn qlr,{\ punoJp elup Jo ser¡1ul;nb a?:u1 ,{:rur sn s}a[ leql ua1s,{s o8e:o1s
u spr,Lo-rd osle souoqd IIoJ suorsuorrtrp o,r\l ur cos eM pllo,tt eql e.r:ase:d pur:
arnldu: 1eq1 selgeieuu 3o qs?,lr u p:]pelJ sprl sl?rJrupa auoqd ¡1ar 3o csn eq1
burssecoJd obetul pue sobetul
g raldeqc
T

Chapter 5 lmaqes and Image ;

in bytes (B) or megabytes (MB). ln order to work u'ith graphics files, it is fname:'rec.:
important to understand that each color element, or pixel, is just a nuntber oL M - imreao ( :. ::-
a sct ol numbers, and a picture is r.rothing more than a matrix of numbers.2
lmagc files can be separated into two classes, raster and vector graphics. te¡ Separ.ale ::
Raster graphics arc typically a rectangular array of numbcrs that represent the redPt ane - i: -:
various colors in the image, with the fir'st pixel located in one comer olthe itnage greenP Lane - :-:
and then lollowing a row colun.m otder until the final pixel is reached. Raster bfue Pl ane : ::: -
graphics will be thc prirnary focus ol this chapter. Vector graphics, as the namc
implies, alc based on vectors used to represent positions of objects in the irnage
!¡ ? Dcne
plane. Compalcd to raster graphics, vector graphics make t¿rsks such as scaling,
rotation, and translation ol objects considerably easier; however, vector graphics The r¡r,::.-:,
are not as effcctive lor representing everyday images taken with current digital file in this ca s;
carrcras. Most display and printing dcvices work with raster infonnation. semicolon is r¡:
Fortunately. conversior.r from vector graphics to raster graphics is straightfor- returned in thitt -
n'ard. although raster images usually rcquire larger file sizcs thau vector images. ,n" a*""r1i6r.i rr:
Iu a rastcr image the overall color of each pixel is defined by a specific v¿rriables can b:
intensit¡r levcl lor each of the colors red (R), glccn (G), and blue (B). The intensity panel.
level is a numbcr typically between 0 and 255.2 Using this RGB model, tlie color The cottr er.
blue would be rcpresenled by the number triplet (0, 0. 255), red by (255, 0, 0) and by-column-b¡ -pi.
green by (0, 255, 0). Other colors, such as purplc (128, 128, 255), make use of would provide ar
intensity levels in each plane. Onc of the easiest rvirys to explore how colors are thc red, green. '
madc rip in images is io use a graphics program such as Microsof'tt Painl to matlix are listed
make a small, single-color image and then decompose the imagc into numbers. The pixel int
Some of the more common image or graphics hles that MATLAB is able to used lor mather
work with arc BMP. GIF, JPEG, PNG, TIFF, and thcre are many more. In MATLAB dctcr
this chapter we u'ill work with just two of these file typcs to introduce some ol require data t¡ ¡.,;
the basics of wolking with images. In most cases, the MATLAB Help will let unsigned integcr
you take the basic skills that wc introduce here and adapt them to work with mathematics. p..
other file fom.rats. Example 5.1 th.'
4
The MATLAB built-in function to read in an image file is rmread 1¡ .r'3 numbers with ¿
This function reads in an image file of a defincd lormat and places the values 2, and 3. The c.-
into a matrix. Example 5.1 shows how to use amr:ead O to load a bitmap file lrom color plar'::
and store it in a matrix as a set of numbers that can be uscd lor calculations. plane 3 is blr,r.-.
Several new idcas are included here that will be explained further. rnatrix M are in:
type double anc
Example 5.1 Use imread ( ) to load a file and separate out color planes. type double. :i
t!¡ M 5 1.m mathematical .:
? sr'rr 2 25 16 occur when inte
? Load a frle lo a vecLor. Should ther.
!ó? Hou se keep ing values in each..
cfear aff; cic; t clears the lrorkspace & command l',/indow using the :-:-
reconstructcd iil
!¡:. Load a fife to a matrix next section.
'lIoIlJeS lxeU
3rl1 rrr u,\\oqs Jq lll,r\ ss33o.rd srql ( ) af ffaur ! ql[.t\ po^es uorll polsnllsuoJ¡l
áq lsnu xlrletx euEld-oerqt clSurs áqt puP 'uorlJunJ o 8ru-!n eql 3uIS11
sJe83]ur peu8lsun ()1 {3€q pelio^uoc eq lsnru seclJlllru aql Jo qslre uI sánlP,\
Iecuelxnu cql 'alg € o1 Ir?q e8etul aql eA¿s 01 psau ¿ cq 3i3rll plnoqs
'pasn ar¿ senle^,reSelur u3rl,,!\ rnJJo
ul?J lpql sio-ria uorlpcunJl lnoqp uJacuos llloLlll,tr suollPJado lectleuteqleru
ur pesn eq upo sasuluru aueld roloc oqt nI plllp aql 'o¡qnop ad,{1 'saue
Surcg'lducs eql uI sru?u xt.r1cru cgtcads-loloJ E uI pelols pue alqnop ad,{t
elep eq] o1 pouJ^nos a:e puc ed,{1 PlEp .81uln, srll Jo .{ll¿nlul oJP t\l xlrlPr 'l:
oq] ur s3nle^ eq1 peluescrdc.t sI JqnJ roloi gCU srlt os 'an1q sr g suuld 'suorlelnJIP¡
pue 'ueer3 sr Z eunld 'c8erut :no.¡o eueld pe.l oql sI qclq,tt '1 aueld ro¡oc utol3 :¡g derutrq r'
suurnloc pu¿ s,\\oJ eql .Io ll¿ lJa[3s ol pJsn sf ( I ' : ' : ) l.J epo3 átll € pul? '¿ ssnlB^ 3q1 S.l
'l se pegrluopr c,ru scueld iolot ee,IrlJ ttotl¿ztueSro letreds e qll,rt s-Isqunu O P:= '
gtum Jo lJolq p sp I I xr.rt€r eqt olut popeol sr aJr¡ a8uut aql ¡ g eldruexE ¡.¡.,
uI lsol sr uortlltüroJul [Euol1ol?{.(uÉ st'} 'uolst^lp ,{1le¡nrrlred 's]tleuláq}€ru qlh{ llo.\\ Lrl
loJ luerua^uoJ lou sr lÉlII.roJ slql lnq'(,91lüln,.Io 81urn,) sreSalur pauStsun tel 1¡,n dlag
,{q paluase:da.r }seq rle ¿}ep e1r3 scrqde:g per.¡rcads aq ol sad,{l ulup e.ttnbe:
JO otUOS 3fr1¡
lou seop puu pelueserdel áq plnoqs ¿11?p otll ,'lrorl sáutullslep BV-IIVfAI u1 ::our iur'
- uoueurroJur flrsuslnr ro¡oo :pt,to-rd ol Jerller lnq sJllerueqleru JoJ pasn ()1 elqB sr g\
áq ot ls¿¡sd,{t tou sr sálg sctqderS eql ur po.¡ots uorleturoJtlr lsxtd eq¿ ' aqunu o1
,sIq g re8elur peuSrsun ol sJeJal qltq,t\',81uln, su polsll 3.IE t{u}elx ol ]ulud ¡ ]-lLr
l l eql ur senlpl olll lpqt aJrloN sen¡u.t .{ltsuatut ánlq pu€ 'uea:8 'pe: aqt eJ¿ sioloJ \\!
eJe ssupld € aql o-rJr[,\\'E x 0¿ x 0¿ suolsueullp Jo xllletu N ue epl^oid plno,tt
JO esn 3{eLU
:8erur roloc ¡axrd g¿ x g¿ e Surpuo¡ 'aldutexc log xt:1eur eueld-,(q-uun¡oc-,{q puP (0 '0 '!!.
{\ol e s0lll0l3 t\l YLI1?rü áql olul JIIJ dtr lg Jtll Jo uols.Ioi\uoo 3qJ loloJ erll 'leFr
'leuud
,{1tsua1ur -rq1
rl\opur^A pueruuroJ 3q1 olur ueql 3uld,{1 ,{q pat:adsur eq ueJ selqBLIP^ :rgrceds e ie-
escql laued aoedsl:o¡¡ ECII BV'IJV1\¡ eq] ur .{trldstp lll,tt tlottnrexe oql 'sa8¿u¡.rol¡:
ñuunp paleo:r s3lqrlue¡. tueJelJrp aql:o'pe1ut.td eq IIr,{\ ruII leq} uI peuin}o.r -roj1r¡8re:1s <
uoflúLuJoJur erll 'epoJ Jo Jnrl ¿ Jo pue 0q1 ruou pe^orual sI uoloJlruss 'lIoIlB LtrrO-J U i
rql Jl l IAtr
xrJluru e olur lrtPp 0q1 spPo,I pue elu dl lf, € ¡sec slql uI olq
1e1Érp ¡u:.i-lr',:
.¡o edfl cql pu€ aut€uellJ eql slndul sÉ se{Ét uoltJunJ o pe3-¡tLIr erlf scrqderi rotr
'3ur1ecs sr: q:
auoo 91 a8euu oql ur
etxPlr 3ri1 sP '
! I \F,'.' : )Iit) aTqnop: aueldanTq lels¿u pJr{Jr
! \(7,1i1| )t{) eTqnop - euel¿uae:6 :8eur aqt -¡t' .
!( lI' . ': )N) aTqnop: auETdpa,r eqt lu:sa:dr.r
s aue Td roToc afe.tredos +?ó 'scn¡dc:8 :rrt.
_ srsqurntr -l\
?
1(,dluq, aurpu] ) peo-rur : I^I .ro Jeqtunu ri
i ,duq'pa:. - oureuJ sI 1r 's3Jr"J srr
q9 5ulssecud abeul pue sedewl q taldeqC
Chapter 5 lnages and lmag.

5.2 lmage Commands in o function. :


¡he image ploi
Working with irnage files is an integral part of MATLAB.' and numerous The appr..-,
built-in functions support thc rcading, writing. and display ol irnagcs. To one image t¡ p:
derronstl-ate hon' these commands can bc used. Examplc 5.2 changes a red bitmap: rr e c,-..-r
image to blue ancl saves tl.rc file to a new name. More detailed leatures of the cbange both th:
commands are avaiiable in thc MATLAB Help files. savedl
The reirl pr
Example 5.2 Use ¡,4ATLAB image commands to turn a red image to blue. an image tc' n.
i;:' M 5 2.rn reconstructe¡ ::
'¡ SljlT 2-25-16
-¡ Tu¡ns red amage b-ue
!: !¡ Hous e keeping 5.3 lmage Sir
cfear ali; clc; '; cieaIs Lhe r,rorakspace & conmand win{tow Working u ith .:
files can be r ei.
r.r; toad a f i-e to a matr.ix or 3ooo x lt)l)
U - lm re ad ( ':red . bnp' , 'bmp' ); 3 numbcrs li¡r:
the file. tlie m;::
:::. SeparaLe cof
or pfanes header that c¡:-
.eaP-ane:M(:, |, !) ; complession i.'
greerPfane --14(:, : ,2) ; inlorrnation c..:
bfuePlane:M(:,:,3); reduction in ::-
compressed di-:
coior pfane order
rl-¿ Sr,rap
infomation L..
N(:/:,1)-greenPlane; compression r'--:
N(:,:,2):bfuePiane; tion process i: :
N (:, :,3) : redPfane;
Iarge pictur. j:
e. Shor,¡
ijlagc, not all .

image s
the image b¡ re:
figure ; lmage (M)
(1 )
singlc pirc1. I::
figure (2) ; rmagesc (N) ; degrading th.- r:
!¿:; Save lmag;e f
Figure :. 1 -
iLe as an arra\ ol :
imw¡ i le (N, 'biue.bmp', 'bnp' ) ;
has an inder l,-.,
of a supcr-prr:
the super-pirel
The conversion to a blue in.rage is failly simple: the order in whicl.r outlined bor.
the inrage planes were placed back into the new matrix N was switched removcd" it u l.
hom the original order so that the blue plane now has values and the The result is t:-.
red plane has zclos. Sincc wc are not doing any real mathematical Conbining
operalions on the values in the matrix, thcrc is no necd to cor.lvclt coding skill th.:
thcm from their original uintS data typc. The lmage O and imagesc o reduction plt'i:
functions were used to display thc in.rages. Thc dilftrence between the imagc to a sur.,
'ozrs elg ls e{us e qlr,\\ o3ulllr tállPtlls P ot aSPLur 3rl1 usJAteq :-
nlr lJe^.uoJ 01 Moq s,\\oqs qclq.r\'€'9 elcfruexE uI p3]uoseld sI ss33o.td uoIlsnpa.i O c s o6elu pu
s8ulllr srq] turoJ,rod ot lduJs V'reJ os p3l¿rlsuornap uaaq seq u?qt llr{s Surpol us^uo3 ot p:¡
áloru ál1 I u se^lo^rrr lexrd-Jodns e o]ur sloYld I¿npr^Ipul 3tl1 SululquroJ Ie3rletIIaqtEru-
'e8erur Ieyrd E x t p ot pácnper sr e8¿tut Iaxld Tl x Zl ¿ql t¿rll sI llnseJ oqf 3ql pue senlP.,
',{ur:e ezrs-pecnpeJ ',r\eu eqt ur (¿'Z) uoDpcol ol{t }p aprsel III'{\ tl 'peloLuo-I peqcl,\^s sP \\ \
a:¿ sec¿ds eJlya ar.lt pue petreoJc st lextd-ledns stql ueq¡A xoq poulTtno qclq,[ ul Lrpl rr
{rep oq1 ul slexrd 6 eqt;o eSurc,te aql .¡o pesoduroc eq ¡L¡Á }¿qtr lextd-:adns aqt
'papeqs osl€ a:e ¡axtd-radns tl.¡o
Jo Je1u33 :q] 1€ sr (s '9) le pelecol ¡exrd eq1
.ratuJ3 orll aq ¡r,tr teqtr slexrd reqlo pepeqs st pue (¿ '¿) 1¿ patecol xepul ue sPq
¡exrd-redns tsrr; árl1Jo roluoJ aqJ 13o,{1.rsue1ut uP qtl^ slaxldJo,{¿.t¡t: uu se
ur\\orls sl aueld e8uur oqJ poruroJ ele slaxtd-:adns s,lroqs ¡ g ern?rg
'uaercs ¡slndluoc arll^\oq
uo o8etur orll SurppJ8ep
]noq¡r\ peJnpeJ oq upc o¡] J8ptur erll Jo ozts eql ',{¿,tr stqt u1 lexrd :13urs
u olur s¡rxrd 3o sdnorF Sutua¡.uoc .to slaxrd aql jo outos Sut,toute: ,{q e?eurt sqt
Jo ezrs erp olnpo.r rl1 uourtuoo sl ¡ os 'pasr.l eq ue:r slaxtd aq:l Jo IIe 1ou 'a8eurt
:q] ulrql slaxrd:e,ne3 srTq leql lolluotu e uo pe,{eldsrp eq o1 sr e:n1crd o3ru1
p
JI nlpp passe:druo:r eq1 uro{ aSuur oql :ololal ol pa:tnba: st sse¡o¡d uorl
-JrlJlsuoJo-r e 1nq 'a8eur I¿urSrjo ¡ql Jo uollpulro;Ul eq1 sutulsl uotsserdruo¡
ssc¡sso1 a8erur eqt jo esn pue oqt .IoJ pol¿Jelol eq uec letll ssol uorteuuo;ut
Jo tunoure cq1 ,{q peuruualep sr ¡ ''e'r :teruoJ elg cql uo spuedep pesserdruoc
aq uur olu p qJrq,¡ o1 aar8ep rqJ elU etep aql .]o ezfs 3ql ul uotlJnps.I
r? ul sllnseJ qJrq,{\'po^orrrar Suteq e?cutt 1auÉr.ro eql ut psuleluoc uoIleuLIoJUI
uo sarlor trorsse¡duroc .{sso1 ,{ssol pul? ssalssol :sonbtuqcel uotssa:duto¡
e8eur jo sed,{l o.nl s.re o.rsqJ - uollpulloJul l¿uotltppe sul¿lttoJ l€q1 Jopeerl
e sr er:qt 3r r:3lel ua^a aq UEJ srtlt pu¿ 'gI I 8 [ st JZIS JIU Lun LUIUIU erlt 'JlU aq]
ur sr ¡axrd rl3pa Jo loloJ qJea Jr pup 'roloc llq-t¿ ut lextd qcea JoJ s.Ieqlllnu t
puÉ Jolol q¡pe JoJ requnu trq-8 ue e^eq J,{\ JI azls ut slaxtd 0002 x 000t Jo
sJaxrd uor¡¡tu 9 qruo:ddu,{1rsea uec:3eurt auoqd 11ac y e8.re¡,{.re,l eq uec se¡g
aiuurr lpql',{larueu',{¡reap,{ra,t lutod cuo otuoq s:,rup sa8erut qlr.{\ 8ut¡:o^A
sloxld-Jadns pue szls obeurl €'9
'suor]las lxeu erll ur pe]?ilsuouráp oq IIt^\ srqJ e?urul u¿ olul palcnrlsuocar
uaqt pue ,{¡ecrtuueqtpu uo pol€.rodo eq upc lpql s.reqtunu o:¡ a3erut ue an¡q o1 ae
tláAuol ol ,{ r3ql sl suorlJun-J
lrg¿ osorll Jo ro,,rrod lpá.r ¡qJ
flV'IJVhl .
¡p0^es
Jq ol olqJo od,{l oql s¿ Ile,,rr su eureu elg eql uo üorsuálx; elu eql r¡loq:8ueqc
eql Jo sainlB¡J P:
ol erns eq lsnf icgdf u o1 1r pcSueqc ,(1rsue se 1snI e,ler¡ plnoJ e,r :d¿Lulrq pe: e se3ueqr
t] oq ot peq alu pe^¿s aql 1Érll uoseel ou sI 3i¡ql rsqlouu o1 ad,{1 o8erur :uo ;,
ol so3€rur -lo i
ruroJsuert ot pasn Jq oslr? plnoo ¿ E eldurexg aqt ur pasn qceo:dde:q1 snorsrunu puP
'¡o1d a8uur aqt
ur pJsn sloloJ orll .]o SurluJS eq.l sl,roddns O csabeur lpql sr suorlJulu o,\\l
6uEsecu¿ e6eal pue sa6eut g )atdeqc
L9
68 Chapter 5
nages and lrr.)

I 11 1 11 11
I 11 1 11 111 11
1 11 111 11
1 111 11 -,ra n e
1 111 11
1 1
Erirnrpl.-:
1 1111 1 11 ar r ay and t.'; -

1 11111 11 11 11 .'r'ery tl.titd :r.


gre¿1tl) redui:
1 11111 11 11 11 atvcragc ol n::-
1 11111 11 11 11 irnf gesa r :--
larger pirel:.
F¡gure 5.1 Construction of the super-plxel frorf smaller pixels of the originai
climensions.
mag e.
Thc net:
nealcst nclgir:
Example 5.3 Reduce an image file size by using super-pixels.
:cr O loop: I
thc M arra\.
,: Ii 5 3.nL a sun]l]l¿ltiolr ..

i SIir 2-25 16 figurcs using t:.


!a Averag= paxers to a smaffer m¿tr1¡ grayscale nr.'.
.a::r tiousekeepr:lg by using thc gr
clea¡ aLf; cfc; : cfears the '!or{space ! c¡mm¿¡o ,,]i¡do-- in the 'Shou r:
two images .Li
;!: Ccnf lgiure mat¡ices value of the t::
14:rand(10tC); This pr.oc.'
lrMax, cMaxl : slze (M) ; tl.re desir-ed le:
r: !; Scan ancl lep.ace

sum: C; sr 1; sc !;
Ícr r:2:3: rMar
'at c:2:3:cl'llax
-of s- :1
ior t: -1:1
sum: s,J.n + M (r+ s, c+t );
end
e 1ci €:
(sr, sc) - sun/ 9,'
x.-
1: l
sum: C; sc:sc', i; !óincr counte r
end Figure 5.2 A ::
sr:sr+1; sc I; :; with pixelation ¿-
informat on iLra :
-ond
'luo.tedde st uo[eltxtd o] onp uotleut.lo]ut
jo ssol eql'suotsuaultp lmtsÁqd elues aql ut u¡ oqs ueqM.uoDJnpa.t ozts pue uoBelaxtd qlt,4
(lq6u) a6eurl e x e eql ol (ga) ebeur OOt x 001 leurouo eql lo uosuedLuoc V 2.9 arnbrl
0€ oz 0r 00r 08 09 0t 0¿
CI
OI
'pe^erqc€ sr cZIs elu.to uorlnlosoJ peJrsep eql
Illun 3lU o8urur u9.ro xulpur l? uo sourrl IE.raAos unlel oq uuJ sseJold srqJ
Jr
'pepoeu raqurnu paxu e le 1es rq plnoJ lr lnq 'e8eurr lsrq orll.lo enle^
lexrd runurxeu aqt sr ¡cxrd eJuaJoJeJ srrlt o-raH (1'1) laxrd aql lu se8eurr o,n1
eql uoo,r\laq ecuere-¡o.r flrsua¡ur uoutrüoJ e sepr,r.o:d uollJes ,sllnsei,roqS, orll ur
eurJ lslu eqJ ' (Ierb) deuroToc uorlcun3 Surdduu ro¡oc alecs,{er8 eql Bulsn,{q
lre1q o1 seuol ,(e:3 qinorql elq,r ruor3 aSuu s.ro1o: 1eql Surueeru e1els,{e:8
ll ol pJus^uoc uaaq e^pq saiuur cq¿ pul?rxtrro3 O csobeurr er¡l Sursn sa:n8g
crqde:8 se pe,{e¡dsrp eJÉ slJs ptep o,\\t oqJ slcxrd euru eql Jo uortpruruns e
cpl,r.o.td sacrpur 1puú s rrJqt r¡lr,r ,sdoo¡ loJ, o,\\t lsul sq1 pue ,,{e:re ¡41 erp
q8nolp dels s¡Jrpur o pue I rrrqt qlr,u ,sdoo1 roJ, o,rt ls.lu eqJ .sdool g -¡ol
patsau -rnoJ qinolql p:qsqdurorce sr lsxrd relueJ rqt pue s:oqqSreu lsaleau
lqira rqt 8ur8e:e¡¿ pue ,{u-r:u cq1 qSno:q1 Surddels Jo poqteru eq.l.
'suorsueurrp
leur6r.io eql ]o =
llsie,\o Jurps eql 8ui^pq s¿ u,\\oqs oru so8urur qloq oJurs .s¡exrd r:F.re1
:,req o1 s-ruodde 1r¡3r.r sqt uo ,{e:le ,nau eql ¿ g 3rg ur uorlcun; ( ) c s abeur
¡ql Sursn pa1lo1d e:e s,{¿llp qtog ,{u.l:e ler ur eq1 ur slexrd euru 3o a8urc,ru
eql sr ,{prre ,lrau eql ul slcxrd aql 3o qJue álaq,r ,ozrs ,{utla pecnpe.t ,{11ee.ri
e sr tlnsei :r¡1 sloqqireu tselpáu áqt Sur8e:c¡.¿ pue lurod pJrqt ,,{.re^a
t¿ ,{ei-re oql q?no.rqt Surddals ,(q tos etpp oqt Jo ezls eql sáJnpái pue ,{¿llp
a,Ienbs p ur sJequnu tuopul?l Jo ,{er.te yerlrur up selpl g.g aldruexa
3UOC:
I (.{ef 6) deu.roTor j (N)csabeulr 1 (¿) arntrr__
I (,{e.rb ) deu¡roIoc I (r{) c s abEu.rr i ( 'i ) ernb r_
1(I,I)r^r: (I,I)N j((:)N)rpur- (I /T)¡,
slTnssf Moqs ?i:
g )etdeLtC
70 Chapter 5 lmages and lm¿.

5.4 Golor Models and Conversions Results of rr


There are many \\ays to present color inlormation to the viewer. Thcsc
c:0
nrethods include using truc color and using false color images. where the
M:0.846
colors are altered to highlight particular inlormation in an imagc. There are
also dillerent ways to represent the colors that arc being used. One of thcse
Y - 0.64 ,-

mcthods. RGB. lias already been introduccd, Others include HSV and
K - 0.368
CMYK color nodcls, or tables that provide dillerer]t ways to var)' color.
These colol models pt'ovidc different ways to model color, but sincc the range This ls a
of color lcally doesn't changc very much to tlre obscrver. thcsc other determine u hi¡
Ícprcsentations are just transforms of other models. The transformatiol] the tht'ee coltrl:
between RGB and CMYK proceeds as fbllou's: be used to del:
defined as a c¡ .
/R G B\
nrrxl^-,^-.^-1.
either bc ll an::
K-l \ /\\ /\\ /\\/
(5.1)
MATL.IB :
color schente.
intensity 1e,,.-1 r
imagc, as thc i:-.:
(5.2) lront whitc to i.
IK
lor a figule is;
coior map ker .

(s.3) Color rlaps u r,.

5.5 Spatial Fi
(5.4) One of the mo:r
Thesc filters ¿i rc
1o smooth or sl-.
These equatior]s can bc constructed into a fturction that will return the ap¡rly these tú.
CMYK values lor an RGB input. This function is shown in Example 5.4. introduccd in Sr-,
size of the tll.-. ::
Spatial trlre:
Example 5.4 Use the conversion function for RGB to C|\4YK. i.e.. a spatiai da:.
functron lC, M, Y, K I : RGB2CYMK (R, G, B) a point. In Sec::,
-a;:¡ I'{ 5 4.rn smooth nois\ d¡:
!i SI\'T 2 26-16 filter did reduce ::
!¡ RGR -)> CI4YI{ The simplest f ur,.

'i Conve r s Lon sharpening. Sntt r
RGB: fR/255; G/255; B/2551 ; fliters tcnd ¡o cn:
K-1 rnax(RGB) ; tion of the filter :
c: (1-RGB(1) K) / (1-K) ; shows tlre o¡-ser::
M: (1-RGB(2) K) /(1 K); applications.: Th:
Y- (1 RGB (3 ) - K) / (1-K); lliter.2 In the h:-i
E i.i sharpening Lapla.-
I
#
oql tnq 'peJunouoJd ssol sr lcol]o Suruedftqs eql 'l < V loJ luerceldel Suruedreqs i
e Jo rruoJ er{l uI sr rettu eq} ¡I:v uoq/\\'rellu lsooq-q8rq 3q1 uI ¿ ro}lu
lsooq-q8rq € Jo ru:oJ eql ur pesserdxe sI xuleru Surued:eqs eq1 .'suorluoqdde
Suruad:uqs pue Surqloorus roJ xulerü Jollg eql 3o uoqezrue8ro eq] s,troqs
arnSg er¡1 3o eprs pueq-lqSrr eql pue 'los €l€p oql uo sseco.¡d rotrlg oql Jo uoq
-ezrue8ro eql s,{\oqs t S 3!C Jo eprs pueq-Uel aq¿ se8ps e¡u¿que 01 puot s.rállg
Surued-reqs e¡q,,lt's:e¡g 8uu-rnlq se u,trou{ osle are s:e11g Surqloous Suruod:¿qs
pue Surqloours eJe 1ás ¿lep eql 01 perTdde oq uec lpql suortounJ tse¡durrs sq¿
'suorsuárürp o,ul ur pssn 0q UEJ srallu r¿lrurs esrouJo s130]J0 eql aJnpel prp rá1lg
eql 'Álalolduroc peJeAoJeJ 1ou se,r pu8rs purSuo eql eIq¡A etep,{srou qloorus
ol pesn sp,lr lpql pecnpoJlur s",^\ rálfu Sur8e¡el¿ u¿ '€'¿ uorloas uI lurod e
Jo enle^ eql ol¿¡nclmar o1 slurod elep Surpunorms áql sesn lás elep ¡erleds e ' e r
lslexrd 3o ,{e¡¡e ue 01 uorllunJ leJq€ureqleru e ssqdde 3uue11g 1e4ed5
'e8erur er{l. Jo {oo[ eql ,(Jrpoiu o] s¡ esod:nd sql 'elu oqt Jo azrs
eql SuronpeJ Jo asodrnd aql 8ur^eq ueq] ráqleJ tnq '€'S uorlJes ur pecnporlur '¡ 9 e¡duer3
qceo.rdde eq1 ur pe^lolur ssoql 01 ru¡nurs Á:e,t ele suoDrunJ oseql ,{[dd€ eql uJnleJ IJr.\\
o1 Sursn aq [[r,r e,r leql s¡decuoc crs¿q eqJ 'e3¿ur ue usd:eqs :o qtoorus ol
pasn oq u¿J l¿ql suoflounJ lsorl¿uieqleru uuo3red o1 peuSrsep eJe sJellu eseql
's.ra11g (t E)
ler¡eds3o osn oql sr Surssaco:d a8erurlo spoqlaru l3oJrp lsoru eglJo euO
buue¡¡rg le¡eds 9'9
'suorlcunJ pele¡er-e8eur Sursn s:e no,( :e^.eueq,\\ á[qe¡e^e eq 1¡t,,lr sdeu.r rolo3 (s's)
's:aqlo Suoruu ',Jerürüns, pue '.1e[ '.reddoc, ere,{¡t o] sp:o,,n,{e¡ deru:o1oc
Ieuonrppu euos uorlcunJ O d€xrroloc eql Sursn pellojluoJ sr ernSg e :o3
duu roloc aq¿ sroloc fer3 3urue1:ep -¡o seues e q8norql lculq ol álrq,lt uro{ (¿ s)
seSueqr :oloc pa,(eldsrp orll ' l ot 0 uor3 se8uuqo an1e.t ,{1rsue1ur oql se 'o8elur
o¡ers,(e:8 e ur'qceo:dde pasn,{¡uouuroc stql.¡o elduexe ue sy'1e,r.e1 ,{1rsua1ur
eql uo pes€q laxrd e 3o rolo3 aql a8uuqr sdeu Joloo aseqJ 'sauráqcs ioloc
luareJ1rp ,{eldsrp tpqt suorldo deru roloc Jo raqunu e sepr,rord SV'IIV¡¡ (t
(: s)
l'pesn eq plnoc )atu LUJOJ oql Jo 'pesodsu€rl aq rorllrs
plnoc gDU Joqlre 'lolco^,\\or e áJo,{\ lrJr 'Je^e,^Aoq :rolma urunloJ e se peuUap
sr gCl{ l€ql ac4o¡1 a1du1 eq1 Jo onle^ runrurxeur eqt ourruJstep ol pesn áq
u€c uorlJunJ O xÉu¡ aql 1eq1 os xrrleur e¡8urs e olur peceld a:e sJoloo aelrlt aql uollBrIIloJsu¿Jl J
'eldurrs srql e{eur oJ 'lseq8rq sr g ro 'C '1¡ Jo senle^ eql Jo qcrq,tr ourruJotep Jáqlo oseql .l¡ u
ol peou op no,{ q8noql¡e 'uorsrá^uoJ pre,tr.ro31q3re:ls ,ften e sr srq¿ á8ueJ eql aJurs ln
':oJoc ,{¡¿,r o1 s i
pue ASH epnlru
88t'0 - t sseql Jo euo .p¡s
Lb9'A:L s:e e:eq¿ .o8eur
9tB'0-h oql e.raq,ü 'sa;eLur
0:a) 3SárlJ le,{or,\ ¡r{l
(S9, tZ, 9ET ) X¡{CZsgli : [x, I,r{, C] 6qruun¡ Jo sllnseE
,L dussaxu¿ a6eal pue sadewl I reldeqC
72 Chapter 5 Images and lmag..

lrnaoe Dáta column of the


starting at ( 1.1 r

11'1 111 the image u ith


Smoothing either of these r
111 111
1t9 1t9 1t9 in.rage.
111 111 Each time
1ts 1t9 1t9
location is cali
1111
1t9 1t9 1t9 summation ol t:
11 111 111 the data set tha:
to the final r alLi
11 111 111
0 -1 0 showsa5xi.
11 11 1 111 half at a valu.-
-1 A+4 -1
11 11 1 111
0-1 0 Example 5.5 P:
1'l 11 1 111
Sharpening
?i? 5 5.::.
M
111 5-2- '
',: SIriT
? Tunabfe ::.
F¡gure 5.3 (left) A matrix populated with 1s showing how a 3 x 3 filter would be positioned
on the matrx. The dark center shows the pixel location where the result of the filter
mult plication is placed in a new matrix. (right) Two filters used in spatial filtering.
? Hou se ke a. r:
cfear a--; :
inragc u ill appear bdghter. The opposite of the sharpening filter is the snoothing ?t Matr.r- :-
filter. uüich reduces the high-f'r'equency variations and blurs the image. Thc siZe:25; .: .
smoothing fllter shown in Fig. 5.3 has the 3 x 3 matrix populated with ls, so it M-zeIoSl::::
musL bc divided by 9 so that thc brightncss ol thc filter does not inctcrse. M(l1:size , -
The proccss of spatial filtering that is being described is also knowt.t ¿ts lrM,cMl:- .
discrete convolution and involves the gene¡ation of a ncw matrix based on
applying a spatial filter matlix to the larger image data set. The flltcr matrix ; Fif ter l.:--::
passes over the data matrix one pixel at a time, and the element-by-element F- | 1 1 - ,
product of the two matrices is summed. This result is placed into thc resulting 11
matrix. This process is shown mathematically as 11 -
1_,
c(r,r) - |
lt- ti I
\) lll,.l,l+M(r-/r.t
t: t"
.lt), (5.5 )
lrF,cFl -.- -
noRm- s.r:: i :

where C(r, c) is an element of thc nen' matrix. / is a square filter matrix of size ri noRrn {= :

N-2nt- 1, and M holds the elemcnts being included ln the original irnage
matdx. ¿¿ Scan. ..:
The application of the 3 x 3 filter matrix to the data is shown in Fig. 5.3. s - ilocr (.i ,
rvhcre the first calculation has the filter centered on the in-rage data point r:R : slart, .
located at row - 2, colunn:2 or (2,2). By starting at this location rathcr for¡-s]-:r,:
than at the (I,1) location, all of rl.re filter elements are within the inage. Had iorc-:.
the proccss started at the point (l.l) in the imagc. the top row and lafJcft T:.',
]/l O 1{rllEluqns alEuj 1 I (s+il:s-c's+l:s :r)l,il-I U3[-JeJ puP \(rj
S-lr"¡¡: lf elS - c .iol pe¡1 aSurur :qr
S-l/il.l:llelS:J lo-: leqlEr uorlll¡ol !
s.tr3Junoc :las !¡ jJ-reJS - Ec jl-rEls : u.r lurod PtEp ¡;rL::
ls+ i:f:rets I (z/Jf ),looTf -s 't g 8lC uI u \1!,t
aceTdel Pue a^To^uo¡ 'ueos 1;i
aSeur Jeut-r-ro :..
1pué iT:ur8Qu l0 :> u¡Uou -l- xlnetlt l..t
ozls Jo
J ur ssnTe^ llns ? l((:)J)LLIns-uuoLl
xef^lc /xpl^tl 01 szrs u6rssec'j I (J) ezrs - IJJ /Jrl
]I T T ] I I (E E)
IIT iI
IIIII
TTIII 8ul1¡rse: tr¡] crl'.::
I I I I t I :J luerue a-,{q - I u : r::.
¡
a.Íenbs aq Jsnul xr,rletr{:rell-!J :i
YrJll?m ,r3tlrl ¡ql
uo pSsPq xutlrri:
xE,l,,lir 'xet\lr oJ azrs ubrsse e,: I (r{) azrs - [I^]3'tilf, sl?u,{\ou{ o.!lf r:
I ot -rTPLI fas != 1I - (llZ/azts),rooTl:Il 'lszrs:Tl)I,,1 'JSee.IJUt 1a:
eo:raz jo xr:rfPl/il I i (aZrs)soraz:U j (azrs)so]3z =t¡ 'lr os 's qtr.\\ p¡t:.
I
xilrJElll 3-renbs Jo ¿zas i !92-azts cq1 e8uur aqr .
dn las xr.trler.il i-.. Sutqloorus arp sr:
¡opura puEllnxotr ! ailedst{,roM or{f s:reeT¡ q: ic , il I lTe -reaTa
6urdaelasnoH:. '6uuollU ?
rellrl oql Jo llnsa, :
p€uorlrsod aq p nc',
:reflrl Terleds aTqeunl '
9I_LZ g JMS .
u|g g I^l :: !: .L
'rallu9x9eqlrMlas elep p 1o 6uua1¡r1 ¡eueds urolod 99 aldutexf
'0 1B J[¿q puP I Jo anle^ e lu JlPtl
qlr,{\ ',{llslle'}ul ut ¡tlds st l¿ql les lr}?p e uo 8ullJE ra11¡ lerleds E S n s,toLl\
g E e¡durexg tq8re,,lr pu¿ uirs lextd lrllU aqt,{q pa¡o.t1uoc sI anle^ [PuU otll o]
lexrd las ulep qcueJo uortnqriluo¡ eql Jellu eql ,{q pere^o. sl luql ios elup otll
Jo s,{¿Jreqns eqt ueá,{\l¡g uortuJlldtllnLu }u:ura¡a-,(q-luaua1a 3I.l1Jo uolll?tllttlns 6-
eql sr uoqPlnslÉJ oqf xllletu ,t\au P uI paJols puP p¡18lnJI€J sI uolleJol 6,,
loxrd eql JoJ anle^ 'reu e'1extd P tlo pJuolltsod sI Jollg áql ,.t,t,'"l.tn_,
oql olul lqanoiq Sutaq st uotluuuojul IPIcUl e 'sotlJ€olddP o,\\] eseql Jo rJtlllé eL-
ul senle^ Iexrd e3p3 3q1 alerrldnp o1 Jo '(SuIppPd olez) solsz qll{\ oáPrul rt{1
(
3o s:8pc cr¡1 puetxa .rerltre ot Jf,ltl¿.Id uouttuoc P sI 1I 'perlsep sl l' [ ) 1¿ SullJels
3-rerl,\\ esec 3rl1 UI los elBp erll Jo lJo rq plnor\ xtJl€tu lellu 3q1 Jo ulunloJ
Duissaioj.J o6eq pue sabeal g )atdeqC
Chapter 5 lmages and lmage
74

*F; if the sum is 0. a ,


N: T. fiftered natrix
Z
in the next sectio
R (rR, cR) :sum(N(:)) /noRm;
The third sec
co-cR+ l; ?lnc courLó" coding, where n
end accounting of inc
rR: rR +1; cR: slart; ? incr rR reset cR
for the new spati
end F matrix. The n
spaces, their pu¡
?g Show re su 1Ls
The T matrix is a
ligure (1) colornap (gray)
; imagesc (M) ; ;
while the F mat
figure (2) ;imagesc(R) ;colorrnap (gray) ;
column position i
step, in which rl
* Done
element. The \ r
value. The locari
This script is fairly complex and shows many interesting and useful controlled b1 rF
concepts. Functionally, the script is in four sections, with each section appropriate.
identiirable by the double percent symbols 'o/'|%' . The first section is the MATLAB h¿
header, where the only active code is for clearing variables and workspace' convolution ol rh
The fourth section shows the two images on the scleen with only a command result of runnin_s t
to set the colors used to display the images. the righthand im
The second section %% Matrix set up does exactly as that first line the left. Because t
indicates: the matrices to be used are set up. Even though we are talking about calculation, thel
images in this script, the starting point will be numerical matrices' The around the right-l
diménsion of the data matrix is defined using the 'siZe' variable MATLAB
has a si ze ( ) function, but by capitalizing just one letter in the function name, Example 5.6 Use :
we can use the function as a variable. Two matrices are created next: M,
M_5_6.m
which will be used to hold the original data, and R, which will be used to hold
Z%
sWT 5-27-1€
the processed data. Both M and R are initially set up to be 25 x 25 element
%

matiices with the elements set to zero. Approximately half of the values in the
¿ Tunable spa: _

M matrix are set to 1, creating a sharp transition between the values. This line
?3 House keec _:.:
is sufficiently interesting to look at in detail:
c lear al1; c-:;
M( f1: sizel , [1: ffoor (síZe/2) ] ) :1; 2 set haff to 1
¿* Matrix se-_ _!
The commands in brackets identify regions in the matrix; [1: size] siZe-25;: s r::
r"l - zeros ( st _- ;
identifies rows l through 25' and l1: ¡1oo r \size/2) I identifies the columns
beginning with the first and running through half the size value, 12.5, then
M([1:saze]/ -_:-
rounded downward using the floor O function to 12. The size g function R = size (M) ;
passes the matrix size to two variables that hold the maximum size lrM, cMl : sizs '1.

information. Each of these variables is tracked individually so that the data


set matrix is not required to be square. ? Fi Lter Mat:_:.:
The firlter matrix F that will be used is lequired in this case to be square, F: I 111 _
but it can be any dimension provided it is smaller than the data matrix. The 111,_
sum of the values in the matrix is calculated as an 'if statement' to ensure that 1111_
III i, l¿ql alnsus o1 . t
IIITi eql xl.tl¿ru U1:;
I T T I I ] :J 'c:enbs aq ot:.,
elenbs aq JSnur -r,rleh] falT lJ :l
elep aqt lPrll ¡:
azTs : /l^I.r AZIS UINUII\T?
xel^I. ?xptil.i oJ ozts u6rSse !: I (t¡f ) ll^lc LL:
I (t^l)32rs - U uorlcrrlu | =
Iof fTeqtes I jI = \[(¿/3zrs),rooT]::Tllazrs:Il)t^i ueql 'c ¿l '¡nlr
Soj¡az Jo !trlel/{ : 1 (aZrS)so:réz -U 1 (32rs) so,raz:tr\ surunloJ aqt s:r:
rT-rJeu erPnbs Jo azTS !¡ :gz-azts Iazrs:r] :\rJl
dn 1as xt.r1e¡ ;.:,
r"lopur¡4 pueuuroJ ecEdsl.ror¡ aql sf ÉaT:l lJTc lT-e ]e¿-¡
! i
burdealasnoH:j:,
surT srqJ ssn l| .
,iélTrf Tetleds sTqeunJ :r. aql ur sanlE.\ .-i:i:
9I-¿Z-Q JMS :: luJurelc sa : i!
ur'9 E I^l !: e. ploq ol pr.sn ¡q
'tr 1 :lxeu ps]lr:.::
ra]lLr. q x q e Outsn las elep e lo Ouuollll lelleds ulJolod ol O ¿^uoc asn g'g alduJexS '3utPu uorlJun-J ..
'e8eLttr puuqJqFrr Jql punolP BV'IJVI I '¡Iq':
srll ssJr-rlELrr
isploq lJelq 3q1 ur Énrllnssr 'oraz ]P pezrlPrlrur urPmá.r .{eq] 'uorlu¡ro¡er
tnoqe 3ur¡¡er ...r
Jq] Suunp poreld3J ]orr s-ra,{\ slu3rxclJ YIJleuI o^\l rolno oq1 esn¿JaB UJI srll "
ourT lslu tPq'¡ i:
uo a3nrur leurir.ro aq] ur e8po diprls Jrlt o.l pásoddo su a8etul pouq-tq8l.r cr{l
ur sioloJ uorlrsuejl eql elrloN t 9 trj uI u,\\orls sr lduls slql Suruun,r.Jo llnse-r
pucrumoc p ilu.-
eql 9 E sfdLuexa ul u,$oqs sr? 'xlrluru ¡8crur puu letlu I] Jo rrorlnlo^uo¡ 'ecuds¡:o,n pur:
ala.rJsrp ¡ql sulrojl3d tr,lq] ( ) z^ucc uorlcunJ ur-1[nq e sPq BV-1JV]i{
'31Pr.rdo.rddP
srll sr uorl¡¡s ti
uorlces r.lJll¡ Llt:
se lose.r puE polualu¡lJur 3.ru qf,rq,r !s3r1JP^ uJ pue u.r ,{q pJflo.rluo-r
¡n3esn pue iur r<
sr xlllr?Lu u Jrll ur psJpld oq ot lurod €lep,\\Jrr 3rl1 Jo uorll?Jol oqJ enle\
,ul{ou. eql ,{q p3zrl1?nuou puP peuruns 3-rE senl?A xr.rleur N eql lrrJursl-l
,{q tueruela ieqteSol porldrtlnlu eiu sarr,rturu C pu€ I oqt qorilir ur 'd:ls
el€fpeuielur uc sr xuletu N srJl sJSuuqc xr.rlPm PlPp Jrll ur uorlrsod rrurnlo¡
pus i\\ol 3rl1 sP ss8uPrlJ YrJlcrlr J aqt'luPlsuor si{Pls xrileru I erll 3 q.\\
teql ]daJXJ'yulprrr j erll se azrs aurps srll 8nr^prl yrilauqns e sr rulr?nl I Jr{f
'Ylil€ru aql qSnoiql suorsuourrp o.\\1 rtl uPJs ol 8'urJq sJsod]nd Jreql 'saJÚals
uulllor puE,{1o-r
eql ur slolünoc e^rJp sdool .roJ, o,{\1 lxeu 3qJ xululrr j
eqt Jo ezrs eql qlrÁ\ ,{.re,r ¡¡,u lulod srr{f ¿tep pálál¡U ,{¡¡urluds eql io-l
^\au
lnrod inrl:e1s lJe.r-roc eql orulrlJolep seurl o,{\1 lsJU orlf seJrpulJo Éurlunooll'
1n¡a:ec ,{ra,r aluos SurpnlJur ':re1d 3ur¡e1 sre siurqt .(ueur a,req.r 'Surpor
p3i\lo^ul lsoLu áql sr 3ceTda] puE 3^io^uoc ? uEcs : !.r uorlJos pltql eql
'epo¡ Jo uorlJss txeu 3qt ur
-ruelJ oLuocJq IIr,{\ srql,roJ uosPei 3qJ
pelnlrlsqns sr I Jo anln^ P '0 sr rulls oql -lr
5ussacd¿ e6ew¡ pue sabeu 9 retdeqO
76 Chapter 5 lmages and lmage

11111
111111;
lrF/ cFl - size (F) ; ?assign size to rMax, cMax
noRm- sum (F (: ) ) ; % sum values in F
_f n oD,r" -_ 0; noRr _'; e.td,. 15

U ? Scan, convolve and replace


C: conv2 (M. F, 'valld' ) ;
%Z Show re sult s
figure (1) ; lmagesc (M) ; colormap (gray) ; Figure I
f igure (2 ) ; imagesc (c),' cof ormap (gr:ay) ;
f11gure (3) ; subpfol (I,2t7) ; imagesc (M) ;coforrnap (gray) ; axis
square; One of the r
subplot (1,2,2) ;rmagesc (C) ; colormap (gray) ;axis square; 'Unsharp Mask'
works is to firs¡ r

: Done from the origin:


When added to i
sharpening the ir
A basic sha¡
ing matrix in the
is applied to a st,
Fig. 5.6.
The usefulne
image and the I
unsharp mask ti
image to pror id
masking is shos
s 10 1s 20 25 s10152025 Many differe
Figure 5.4 (left) Original image and (right) the filtered image. Not¡ce that there is a black effects can be us
band in the f¡ltered ¡mage, as the filter was not appl¡ed to the outer row of p¡xels, where the
intensity was kept at zero.

The script in Example 5.6 includes the conv2 g function approach, and the
results of applying the hlter are shown in Fig. 5.5. The conv2 function g 5l
supports several keyvords that allow the user to tune the way the function is
applied. The keywords include 'full', 'same', and'valid'and change the resulting
shape of the matlix. The 'valid' key,vord returns the convolution without using
zero padding.
The usefulness of spatial filtering becomes apparent when working
directly with images, as the filtering effects can be masked by the viewing
resolution. The effect of many hlters is difficult to show except in electronic
images, where it is possible to zoom in on features of interest. An image- F¡gure 5.6 (left) O
filtering techniq te lhat does have clearly evidelt effects uses sharpening Notice on the right-l
masks, which enhance regions of abrupt change. made to stand out f
'punololceq oql uJorj lno puels ol epeu
pue pa}3alas uaaq seq aoueqc dals aql Jo uor6a.r aql leql a6eu] pueqJqou aql uo actloN
Suru:d.teqs sasn
rallrJ 6uruadreqs oql 6ur^ldde.ieue ebeur aql (lq0u) pue aOeL!r leutorJo (Ual) 9.9 €Jn6tJ
-aSerur uy l s:.i..
sz0zEr0rs 9¿ OZ 51 01 S
rruo-r1la1: ur 1d;:
ñur,ner,r aqt iq I
3nr1:o,u u:q rr I
Sutsn 1noq1r t ut r
3u11¡nser eq1 aiur
sl uonJUnJ ¡qt i:
uorlsulg oz i:_
aqt pue 'qrpo:rl.l:-
aql a.iaq¡ 'slox d ,:
ur s3rl3rlls3r? ,roJ sP IIa,t\ sP Sursseroid PlPp rrlrluorss.roJ pasn oq uec s1J3lJ3 )celq E sr eteql le,:
eql pue'oaelur ue ol perldde Jq uer siolluJo suorluulqtuoj luera.lJrp ,{ueIN
9¿ a¿
'¿ g 31.:l Jo oprs pu¿q-]q8rJ oqt uo u,roqs sr 8ur{sr}ur
d.ieqsun jo sllnsrr 3qt jo a¡durexe uV lrrrurocuuqua aq] apr,lo:d o1 a8eurr
prrr¡19 ueneldel eqt oi peppe sr a8eurr ¡eur8r-ro orll 'ratlu ¡seru dreqsrm
u¿ Jo ulloJ orll uI 0 < V 'sr ]pqt 'pesealcur sr la^e[ tsooq oql puu J8¿uri
uu ol par¡dde sr uorl,lr ueos sq u¿o Je][U SuruedJ€qs srqt
tr Jo sseulnJosn rqJ
'9 9 '3!J
3o epts puuqlq8u rq] uo u,\\oqs tlnsar oql Jcs e,\\'uorlJunJ dals u o1 pcqdde sr
retlu slql uaq¡A 0 - V eieq.u 'g g 3rg 3o cprs pueq-]qiu .la,r\ol sqt ur xutsru 3ur
-ued:eqs oql s€ rr,rorls sr rlcrq,r -'ueroeldp-I oqt sr:al1g Suruad:eqs lsuq y
'a8cuu aql Suruadrcqs
snqt 'pespe,rour eiu aSeulr eql ur s¡ead oql 'aSDLnr leur8uo aqt ot poppe u:q¡A
'a3eutt paqloours arl] e^oq¿ ar1 teqt s¡ead orll ulelqo o1 1r:ur8r"ro eql uol-I
a3r:ur paln¡q erlt lJeJtqns uJql pue cSuur aq1.¡o .{doc e :n1q tslU ol sr sllo.\\
.lJtilJ :rlt ,{p,r arlf a8€ur or-¡} ur s¡eed arueque ol posn sr pue,lset\l d.reqsu¡. 1¡,-= a a .
Jo aluuu lunsnun Jrll spq slatlu Suruedruqs 8urls::a1ur eloul oql Jo JrrO
sTxe ! (.i\a:. .:
.pro¡ Áa)
,prle^, aql 6ursn O ZAuoc 6uruunr jo sllnsou 9'9 arnorj
0agr0rs5z0asr0rE
LL 6ussecoL¿ o6ewl pue sa5eui 9 )eldeqC
Chapter 5

Part
800 800

200 400 600 800 200 400 600 800

Figure 5.7 (right) Or¡ginal image and (left) the sharpened image.
Chaptels 6 thror
enhancing images. Being able to construct and apply custom image filtels is a often encounte¡e
powerlul skill and can also be interesting. developing soluti,
visualization tool
5.6 Practice Problems
1. What 3 x 3 flrlter would return the same image that you started with?
2. What is the ellect of the filter 0.5*[0 0 0; 0 I 0; 0 0 0]?
Answers
1.[000; 010;000]
2. It reduces the brightness by half.

References
1. D. Hanselman and B. Littlefield, Masrering MATLAB@ 7, Prentice-Hall,
Upper Saddle River, New Jersey (2011).
2. R. C. Gonzales and R. E. Woods, Digital hnage Processing, Second
Edition, Prentice-Hall, Upper Saddle River, New Jersey (2002).
3. H. Moore, MATLAB@ Jor Engineers, 4th Edition, Prentice-Hall, Upper
Saddle River, New Jersey (2014).
4. R. Pratap, Gexing Started vvitlt MATLAB 7: A Quick Introduction for
Scienrisrs and Engineers, Oxford University Press, Oxford (2006).
Chapter 7
Spectrometers
Spectroscopy can be thought of as working with rainbows, i.e., the separation
of white light into its component colors, which range from red to violet.
Seeing a rainbow, or even better, double rainbows, has great aesthetic value
but is also part of the science behind analyzing light. On the beauty side, cut
glass crystals such as those that might be used in a chandelier will also show
the colors of the rainbow. Refining the shape of these crystals for use in
science has led to new insight in many fields.
The light intensity as a function of wavelength is known as a spectrum and
is used in many analytical systems to determine the composition of materials.
lt is well known that the sun produces a wide range of wavelengths and that
the water vapor in the Earth's atmosphere blocks many wavelengths from
reaching the surface. The areas of the spectrum that can penetrate the
atmosphere are known as atmospheric windows. 1-5 Metallurgy has benefitted
from the use of spectrometers for determining temperatures of highly heated
metals to ensure that the metal processing happens at the right temperatures.
Optical spectrometers can be made usi�g prisms, which rely on refraction
and gratings. Gratings use diffraction to separate out the various wavelengths.
Hybrid devices known as grisms are made from combinations of gratings and
prisms and have the advantage of making very compact instruments.
In this chapter we will model the sorne of the key equations used m
spectrometers and how they relate to the final spectrum.

7.1 Dispersion in a Material


The index of refraction of a material is wavelength dependent and typically
will have a higher index of refraction at shorter or blue wavelengths than will
redder or longer wavelengths of light. The results is that if a beam of white
light-one that contains ali wavelengths-pa.sses through a thickness of glass
at a non-normal angle of incidence, the emerging light will have separated into
its component colors. This separation of colors is known as dispersion and is
shown in Fig. 7 .1.

95
Chapter 11
Polarization
An object viewed through certain crystals will appear as a double image. Such
crystals demonstrate double refraction; i.e., the two directionally dependent
refractive indices through the material result in one image being displaced
from the other. l-4 The explanation of this effect led to our current
understanding of polarization effects.
A wide range of devices incorporate polarization; one of the most
common of these in use today is the liquid crystal display. Liquid crystals were
discovered in the late l 800s and have only recently become the primary
display technology.4 More traditional devices based on polarization include
Pockels cells, Nicol prism, Glan-Thompson prism, and more.3
Polarization, which can be induced by reflection, is found in nature, an
example of which is sunlight glancing off of a body of water. A similar effect
can be created by light interacting with a collection of thin glass plates. It is
important to appreciate that there is a difference between simple reflection
and a polarization effect on reflected light.
Polarization of light is an important topic and often does not receive the
consideration that it should be accorded in many optic courses. Here, we are
interested in the mathematics involved in calculating polarization using
matrix methods. The two formalisms, the Jones calculus and the Mueller
calculus provide an effective means of determining the influence of a polarizer
on a beam of light.
In this chapter we will see how two different yet related matrix approaches
are applied in polarization calculations and how to set up the calculations in
MATLAB®. Interestingly, under certain conditions these two matrix
approaches can be transformed from one to the other.

11.1 Polarized Light


The double image seen in a calcite crystal placed on a typed page from a book
is one of the more visually striking images associated with the phenomenon of
polarization. Double images were first reported in 1669 by Erasmus

155

You might also like