290 15 106MB
English Pages 236 Year 2017
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 JSn selg e8plur'lelouei uJ elqelr¿^e aulo¡¡c aql aJnpoi ot steltuoJ olu,fteurq 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: eieuu 3o qs?,lr u p:]pelJ sprl sl?rJrupa auoqd ¡1ar 3o csn eq1
arnldu:
1eq1 selg
burssecoJd obetul pue sobetul g raldeqc
T
Chapter 5
in bytes (B) or megabytes (MB). ln order to work u'ith graphics files, it
is
important to understand that each color element, or pixel, is just a nuntber oL 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. Raster graphics arc typically a rectangular array of numbcrs that represent the various colors in the image, with the fir'st pixel located in one comer olthe itnage and then lollowing a row colun.m otder until the final pixel is reached. Raster 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 plane. Compalcd to raster graphics, vector graphics make t¿rsks such as scaling, rotation, and translation ol objects considerably easier; however, vector graphics are not as effcctive lor representing everyday images taken with current digital carrcras. Most display and printing dcvices work with raster infonnation. Fortunately. conversior.r from vector graphics to raster graphics is straightforn'ard. although raster images usually rcquire larger file sizcs thau vector images. Iu a rastcr image the overall color of each pixel is defined by a specific intensit¡r levcl lor each of the colors red (R), glccn (G), and blue (B). The intensity level is a numbcr typically between 0 and 255.2 Using this RGB model, tlie color blue would be rcpresenled by the number triplet (0, 0. 255), red by (255, 0, 0) and green by (0, 255, 0). Other colors, such as purplc (128, 128, 255), make use of intensity levels in each plane. Onc of the easiest rvirys to explore how colors are madc rip in images is io use a graphics program such as Microsof'tt Painl to make a small, single-color image and then decompose the imagc into numbers. Some of the more common image or graphics hles that MATLAB is able to work with arc BMP. GIF, JPEG, PNG, TIFF, and thcre are many more. In this chapter we u'ill work with just two of these file typcs to introduce some ol the basics of wolking with images. In most cases, the MATLAB Help will let you take the basic skills that wc introduce here and adapt them to work with other file fom.rats. 4 The MATLAB built-in function to read in an image file is rmread 1¡ .r'3 This function reads in an image file of a defincd lormat and places the values into a matrix. Example 5.1 shows how to use amr:ead O to load a bitmap file and store it in a matrix as a set of numbers that can be uscd lor calculations. Several new idcas are included here that will be explained further. Example 5.1 Use imread
(
) to load
a file and separate out color planes.
t!¡ M 5 1.m ? sr'rr 2 25 16 ? Load a frle lo a vecLor. !ó? Hou se keep ing cfear aff; cic; t clears the lrorkspace !¡:. Load a
fife to
a
matrix
& command l',/indow
lmaqes and Image
;
fname:'rec.: - imreao ( :. ::-
M
te¡ Separ.ale
::
redPt ane - i: -:
greenP Lane - :-: bfue Pl ane : ::: !¡
? Dcne
The r¡r,::.-:, file in this ca s; semicolon is r¡: returned in thitt ,n" a*""r1i6r.i rr: v¿rriables can b: panel.
The
cottr er.
by-column-b¡ -pi. would provide ar thc red, green. ' matlix are listed The pixel int used lor mather MATLAB dctcr require data t¡ ¡.,; unsigned integcr mathematics. p.. Example 5.1 th.' numbers with ¿ 2, and 3. The c.lrom color plar':: plane 3 is blr,r.-. rnatrix M are in: type double anc
type double. :i
mathematical .: occur when inte Should ther. values in each.. using the :-:reconstructcd iil 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
ul?J
'pasn ar¿ senle^,reSelur u3rl,,!\ rnJJo
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
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 oq] ur s3nle^ eq1 peluescrdc.t sI JqnJ roloi gCU srlt os 'an1q sr g suuld pue 'ueer3 sr Z eunld 'c8erut :no.¡o eueld pe.l oql sI qclq,tt '1 aueld ro¡oc utol3 suurnloc pu¿ s,\\oJ eql .Io ll¿ lJa[3s ol pJsn sf ( I ' : ' : ) l.J epo3 átll € pul? '¿ 'l se pegrluopr c,ru scueld iolot ee,IrlJ ttotl¿ztueSro letreds e qll,rt s-Isqunu 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 loJ luerua^uoJ lou sr lÉlII.roJ slql lnq'(,91lüln,.Io 81urn,) sreSalur pauStsun ,{q paluase:da.r }seq rle ¿}ep e1r3 scrqde:g per.¡rcads aq ol sad,{l ulup e.ttnbe: lou seop puu pelueserdel áq plnoqs ¿11?p otll ,'lrorl sáutullslep BV-IIVfAI - uoueurroJur flrsuslnr ro¡oo :pt,to-rd ol Jerller lnq sJllerueqleru JoJ pasn áq ot ls¿¡sd,{t tou sr sálg sctqderS eql ur po.¡ots uorleturoJtlr lsxtd eq¿
,sIq g re8elur peuSrsun ol sJeJal qltq,t\',81uln, su polsll 3.IE t{u}elx l l eql ur senlpl olll lpqt aJrloN sen¡u.t .{ltsuatut ánlq pu€ 'uea:8 'pe: aqt
eJe ssupld € aql o-rJr[,\\'E x 0¿ x 0¿ suolsueullp Jo xllletu N ue epl^oid plno,tt g¿ x g¿ e Surpuo¡ 'aldutexc log xt:1eur eueld-,(q-uun¡oc-,{q :8erur roloc ¡axrd YLI1?rü áql olul JIIJ dtr lg Jtll Jo uols.Ioi\uoo 3qJ
{\ol e s0lll0l3 t\l
'leuud
rl\opur^A pueruuroJ 3q1 olur ueql 3uld,{1 ,{q pat:adsur eq ueJ selqBLIP^ escql laued aoedsl:o¡¡ ECII BV'IJV1\¡ eq] ur .{trldstp lll,tt tlottnrexe oql ñuunp paleo:r s3lqrlue¡. tueJelJrp aql:o'pe1ut.td eq IIr,{\ ruII leq} uI peuin}o.r uoflúLuJoJur erll 'epoJ Jo Jnrl ¿ Jo pue 0q1 ruou pe^orual sI uoloJlruss xrJluru e olur lrtPp 0q1 spPo,I pue elu dl lf, € ¡sec slql uI olq sÉ se{Ét uoltJunJ o pe3-¡tLIr erlf
rql Jl l
.¡o
IAtr
edfl cql pu€ aut€uellJ eql slndul
'saue
'l: 'suorlelnJIP¡
:¡g derutrq ssnlB^ 3q1
¡.¡.,
O
r'
S.l
P:= '
qlh{ llo.\\ Lrl tel 1¡,n dlag JO otUOS 3fr1¡
u1 ::our iur'
()1
'
o1
aqunu
g\
elqB sr
ol ]ulud eJ¿
¡ ]-lLr
sioloJ \\!
JO esn 3{eLU
puP (0 '0 '!!. loloJ erll 'leFr ,{1tsua1ur -rq1
:rgrceds
e
ie-
'sa8¿u¡.rol¡: -roj1r¡8re:1s < 'lIoIlB LtrrO-J U i 1e1Érp ¡u:.i-lr',:
scrqderi rotr '3ur1ecs sr:
q:
lels¿u pJr{Jr :8eur aqt -¡t' .
! I \F,'.' : )Iit) aTqnop: aueldanTq ! \(7,1i1| )t{) eTqnop - euel¿uae:6 !( . ': )N) aTqnop: auETdpa,r lI' s aue Td roToc afe.tredos +?ó
a8euu oql ur etxPlr 3ri1 sP '
auoo 91
1(,dluq, aurpu] ) peo-rur : I^I i ,duq'pa:. - oureuJ ?
5ulssecud abeul pue sedewl
q9
eqt lu:sa:dr.r 'scn¡dc:8 :rrt. _ srsqurntr -l\ .ro Jeqtunu ri sI 1r 's3Jr"J srr q taldeqC
Chapter 5
5.2 lmage Commands Working with irnage files is an integral part of MATLAB.' and numerous built-in functions support thc rcading, writing. and display ol irnagcs. To derronstl-ate hon' these commands can bc used. Examplc 5.2 changes a red image to blue ancl saves tl.rc file to a new name. More detailed leatures of the commands are avaiiable in thc MATLAB Help files. Example 5.2 Use ¡,4ATLAB image commands to turn a red image to blue.
i;:' M 5 2.rn '¡ SljlT 2-25-16 -¡ Tu¡ns red amage b-ue !: !¡ Hous e keeping cfear ali; clc; '; cieaIs Lhe r,rorakspace & conmand win{tow
toad a f i-e to a matr.ix - lm re ad ( ':red . bnp' , 'bmp'
r.r; U
);
:::. SeparaLe cof
or pfanes .eaP-ane:M(:, |, !) ; greerPfane --14(:, : ,2) ; bfuePlane:M(:,:,3); coior pfane order N(:/:,1)-greenPlane; N(:,:,2):bfuePiane; N (:, :,3) : redPfane; rl-¿ Sr,rap
.ó
e. Shor,¡
image s
figure ; lmage (M) figure (2) ; rmagesc (N) ; (1 )
!¿:; Save lmag;e f
iLe
imw¡ i le (N, 'biue.bmp', 'bnp' ) ;
The conversion to a blue in.rage is failly simple: the order in whicl.r the inrage planes were placed back into the new matrix N was switched hom the original order so that the blue plane now has values and the red plane has zclos. Sincc wc are not doing any real mathematical operalions on the values in the matrix, thcrc is no necd to cor.lvclt thcm from their original uintS data typc. The lmage O and imagesc o functions were used to display thc in.rages. Thc dilftrence between the
lnages and lmag.
in o function. : ¡he image ploi The appr..-, one image t¡ p: bitmap: rr e c,-..-r cbange both th: savedl
The reirl pr image tc' n. an reconstructe¡ ::
5.3 lmage
Sir
Working u ith .: files can be r ei. or 3ooo x lt)l) 3 numbcrs li¡r: the file. tlie m;:: header that c¡:complession i.' inlorrnation c..: reduction in ::compressed di-: infomation L.. compression
r'--:
tion process i: : Iarge pictur. j: ijlagc, not all . the image b¡ re:
singlc pirc1. I:: degrading th.- r: Figure :. 1 as an arra\ ol : has an inder l,-., of a supcr-prr: the super-pirel outlined bor. removcd" it u l. The result is t:-. Conbining coding skill th.: reduction plt'i: imagc to a sur.,
'ozrs elg ls e{us e qlr,\\ o3ulllr tállPtlls P ot aSPLur nlr lJe^.uoJ 01 Moq s,\\oqs qclq.r\'€'9 elcfruexE uI p3]uoseld sI ss33o.td uoIlsnpa.i s8ulllr srq] turoJ,rod ot lduJs V'reJ os p3l¿rlsuornap uaaq seq u?qt llr{s Surpol áloru ál1 I u se^lo^rrr lexrd-Jodns e o]ur sloYld I¿npr^Ipul 3tl1 SululquroJ 'e8erur Ieyrd E x t p ot pácnper sr e8¿tut Iaxld Tl x Zl ¿ql t¿rll sI llnseJ oqf ',{ur:e ezrs-pecnpeJ ',r\eu eqt ur (¿'Z) uoDpcol ol{t }p aprsel III'{\ tl 'peloLuo-I a:¿ sec¿ds eJlya ar.lt pue petreoJc st lextd-ledns stql ueq¡A xoq poulTtno {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 s,lroqs ¡ g ern?rg ur\\orls sl aueld e8uur oqJ poruroJ ele slaxtd-:adns ^\oq 'uaercs ¡slndluoc arll 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
3rl1 usJAteq :O c s o6elu pu us^uo3 ot p:¡
Ie3rletIIaqtEru-
3ql pue
peqcl,\^s
senlP., sP
qclq,[ ul
\\ \
Lrpl
rr
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 osorll Jo ro,,rrod lpá.r ¡qJ r3ql sl suorlJun-J tláAuol ol ,{ lrg¿
flV'IJVhl
.
an¡q o1 ae
¡p0^es Jq ol olqJo od,{l oql s¿ Ile,,rr su eureu elg eql uo üorsuálx; elu eql r¡loq:8ueqc ol erns eq lsnf icgdf u o1 1r pcSueqc ,(1rsue se 1snI e,ler¡ plnoJ e,r :d¿Lulrq t] oq ot peq alu pe^¿s aql 1Érll uoseel ou sI 3i¡ql rsqlouu o1 ad,{1 o8erur :uo ruroJsuert ot pasn Jq oslr? plnoo eldurexg aqt ur pasn qceo:dde:q1 E ¿ '¡o1d a8uur aqt ur pJsn sloloJ orll .]o SurluJS eq.l sl,roddns O csabeur lpql sr suorlJulu o,\\l 6uEsecu¿ e6eal pue sa6eut
L9
eql Jo sainlB¡J P:
pe: e se3ueqr
ol
so3€rur
;,
-lo i
puP
snorsrunu
g )atdeqc
Chapter 5
68
I
11
1
I
11
1
1
11
11 11
5.1
11
111 111
11 11 11
-,ra n
1
1111
11
11
11
1
11
11
1
11111
11
11
11 11
Construction
of the
e
Erirnrpl.-: 11
1
11111 11111
1
F¡gure
111
111
1
1
lrr.)
11
1
1
nages and
super-plxel frorf smaller pixels
ar
of the
mag e.
originai
r
ay and t.';
-
.'r'ery tl.titd :r. gre¿1tl) redui: atvcragc ol n::irnf gesa r :-larger pirel:. climensions.
Thc net: nealcst nclgir:
Example 5.3 Reduce an image file size by using super-pixels.
,: Ii 5 3.nL i SIir 2-25 16 !a Averag= paxers to a smaffer m¿tr1¡ .a::r
tiousekeepr:lg
clea¡ aLf; cfc; : cfears the '!or{space ! c¡mm¿¡o ,,]i¡do-;!: Ccnf lgiure mat¡ices 14:rand(10tC); lrMax, cMaxl : slze (M) ; 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
:cr O loop: thc M arra\. a sun]l]l¿ltiolr
I
figurcs using
t:.
grayscale
..
nr.'.
by using thc gr in the 'Shou r: two images .Li value of the t::
This pr.oc.' tl.re desir-ed le:
);
end e
€:
1ci
(sr, sc) - sun/ 9,' sum: C; sc:sc', i; !óincr counte r
x.-
end
sr:sr+1; sc I; -ond
1:
Figure :;
5.2
l
A ::
with pixelation ¿informat on iLra :
'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
0€
oz
0r
uosuedLuoc
00r 08 09 0t
V
2.9 arnbrl
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 'pepoeu raqurnu paxu e le 1es rq plnoJ lr lnq 'e8eurr lsrq orll.lo enle^ Jr 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
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 sloqqireu tselpáu áqt Sur8e:c¡.¿ pue lurod pJrqt ,,{.re^a
e sr tlnsei
leur6r.io
eql ]o
=
:r¡1
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
Chapter 5
70
5.4 Golor Models and Conversions There are many \\ays to present color inlormation to the viewer.
Results of rr Thcsc
nrethods include using truc color and using false color images. where the 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 mcthods. RGB. lias already been introduccd, Others include HSV and 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 of color lcally doesn't changc very much to tlre obscrver. thcsc other Ícprcsentations are just transforms of other models. The transformatiol] between RGB and CMYK proceeds as fbllou's:
K-l
/R G B\ nrrxl^-,^-.^-1. \ /\\ /\\ /\\/
lmages and lm¿.
(5.1)
c:0 M:0.846 Y K
- 0.64 - 0.368
,-
This ls
a
determine u hi¡ the tht'ee coltrl: be used to del: defined as a c¡ . either bc ll an::
MATL.IB
:
color schente.
IK
(5.2)
intensity 1e,,.-1 r imagc, as thc i:-.: lront whitc to i.
lor a figule is; coior map ker
.
(s.3)
Color rlaps u r,.
5.5 Spatial Fi (5.4)
These equatior]s can bc constructed into a fturction that will return the CMYK values lor an RGB input. This function is shown in Example 5.4. Example 5.4 Use the conversion function for RGB to C|\4YK.
functron lC, M, Y, K I : -a;:¡ I'{ 5 4.rn !i SI\'T 2 26-16
RGB2CYMK (R, G, B)
!¡ RGR -)> CI4YI{ !ó 'i Conve r s Lon
RGB: fR/255; G/255; B/2551 ; K-1 rnax(RGB) ; c: (1-RGB(1) K) / (1-K) ; M: (1-RGB(2) K) /(1 K); Y- (1 RGB (3 ) - K) / (1-K); E i.i
One of the mo:r Thesc filters ¿i rc 1o smooth
or sl-. ap¡rly these tú. introduccd in Sr-, size of the tll.-. :: Spatial trlre: i.e.. a spatiai da:. a point. In Sec::, smooth nois\ d¡: filter did reduce :: The simplest f ur,. sharpening. Sntt r fliters tcnd ¡o cn: tion of the filter : shows tlre o¡-ser:: applications.: Th: lliter.2 In the h:-i sharpening Lapla.-
oql tnq 'peJunouoJd ssol sr lcol]o Suruedftqs eql 'l < V loJ luerceldel Suruedreqs 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 's:e¡g 8uu-rnlq se u,trou{ osle are s:e11g Surqloous Suruod:¿qs
I # i
Surued-reqs e¡q,,lt
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 ,{e¡¡e ue 01 uorllunJ leJq€ureqleru e ssqdde 3uue11g 1e4ed5 3o '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 qceo.rdde eq1 ur pe^lolur ssoql 01 ru¡nurs Á:e,t ele suoDrunJ oseql ,{[dd€ 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 ler¡eds3o osn oql sr Surssaco:d a8erurlo spoqlaru l3oJrp lsoru eglJo euO
'¡ 9 e¡duer3 eql uJnleJ IJr.\\ (t
's.ra11g
buue¡¡rg
E)
le¡eds 9'9 (s's)
'suorlcunJ pele¡er-e8eur Sursn s:e no,( :e^.eueq,\\ á[qe¡e^e eq 1¡t,,lr sdeu.r rolo3 '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{ 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¡¡ (: 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 'eldurrs srql e{eur oJ 'lseq8rq sr g ro 'C '1¡ Jo senle^ eql Jo qcrq,tr ourruJotep peou op no,{ q8noql¡e 'uorsrá^uoJ pre,tr.ro31q3re:ls ,ften e sr srq¿
(¿ s)
(t
s)
uollBrIIloJsu¿Jl J Jáqlo oseql .l¡ u á8ueJ eql aJurs ln ':oJoc ,{¡¿,r o1 s i
ol
pue ASH epnlru sseql Jo euo .p¡s s:e e:eq¿ .o8eur
88t'0 - t Lb9'A:L 9tB'0-h
oql
0:a) (S9, tZ, 9ET ) X¡{CZsgli
e.raq,ü 'sa;eLur
3SárlJ le,{or,\ ¡r{l
: [x, I,r{, C] 6qruun¡ Jo sllnseE I
dussaxu¿ a6eal pue sadewl
,L
reldeqC
Chapter 5
72
Images and lmag..
column of the starting at ( 1.1 the image u ith either of these r
lrnaoe Dáta
r
11'1 111
111
111
111
111
1111
11 11
111
111
111
111
11
11
1
11
11
1
111
1'l
11
1
111
5.3
1t9 1t9
1t9
in.rage.
1ts 1t9
1t9
1t9 1t9
1t9
location is cali summation ol t:
Each time
the data set tha:
to the final
0 -1
0
-1 A+4
-1
0-1
0
half at a valu.Example 5.5 P:
5 5.::. 5-2- ' ? Tunabfe ::. ?i?
M
',: SIriT
(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.
inragc u ill appear bdghter. The opposite of the sharpening filter is the snoothing filter. uüich reduces the high-f'r'equency variations and blurs the image. Thc smoothing fllter shown in Fig. 5.3 has the 3 x 3 matrix populated with ls, so it musL bc divided by 9 so that thc brightncss ol thc filter does not inctcrse.
The proccss of spatial filtering that is being described is also knowt.t ¿ts 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 passes over the data matrix one pixel at a time, and the element-by-element product of the two matrices is summed. This result is placed into thc resulting matrix. This process is shown mathematically as
c(r,r)
r alLi
showsa5xi.
Sharpening
111 F¡gure
111
Smoothing
- | lt-
ti
I
\) lll,.l,l+M(r-/r.t t: t"
.lt),
(5.5 )
where C(r, c) is an element of thc nen' matrix. / is a square filter matrix of size N-2nt- 1, and M holds the elemcnts being included ln the original irnage
matdx. The application of the 3 x 3 filter matrix to the data is shown in Fig. 5.3. rvhcre the first calculation has the filter centered on the in-rage data point located at row - 2, colunn:2 or (2,2). By starting at this location rathcr than at the (I,1) location, all of rl.re filter elements are within the inage. Had the proccss started at the point (l.l) in the imagc. the top row and lafJcft
? Hou se ke a. r: cfear a--; :
?t Matr.r- :-
siZe:25; .: . M-zeIoSl:::: M(l1:size , lrM,cMl:- .
-
; Fif ter l.:--:: F- | 1 1 - , 11
11 1_, lrF,cFl -.-
noRm- s.r::
{=
ri
noRrn
¿¿
Scan. ..:
-
-
i
:
:
- ilocr (.i , r:R : slart, . for¡-s]-:r,: iorc-:. T:.', s
]/l
:r)l,il-I O 1{rllEluqns alEuj 1 I (s+il:s-c's+l:s S-lr"¡¡: lf elS - c .iol S-l/il.l:llelS:J lo-: s.tr3Junoc :las !¡ jJ-reJS - Ec jl-rEls : u.r ls+ i:f:rets I (z/Jf ),looTf -s aceTdel Pue a^To^uo¡ 'ueos 1;i 1pué iT:ur8Qu l0 :> u¡Uou -lJ 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 IIT iI IIIII TTIII I I I I t I :J a.Íenbs aq Jsnul xr,rletr{:rell-!J
I ot
:i
xE,l,,lir 'xet\lr oJ azrs ubrsse e,: I (r{) azrs - [I^]3'tilf, 'lszrs:Tl)I,,1 fas != 1I - (llZ/azts),rooTl:Il jo xr:rfPl/il I i (aZrs)soraz:U j (azrs)so]3z =t¡
U3[-JeJ puP \(rj pe¡1 aSurur :qr leqlEr uorlll¡ol ! lurod PtEp ¡;rL::
't g 8lC uI u \1!,t
aSeur Jeut-r-ro :.. xlnetlt l..t ozls Jo
(E E)
8ul1¡rse: tr¡]
p 1o 6uua1¡r1 ¡eueds
urolod
99
crl'.::
luerue a-,{q - I u : r::. YrJll?m ,r3tlrl ¡ql ¡
uo pSsPq xutlrri: u,{\ou{ o.!lf r:
sl?
'JSee.IJUt 1a:
-rTPLI
eo:raz
xilrJElll 3-renbs Jo ¿zas i !92-azts dn las xr.trler.il i-..
¡opura puEllnxotr ! ailedst{,roM or{f s:reeT¡ q: ic , il I lTe -reaTa 6urdaelasnoH:.
:reflrl Terleds aTqeunl ' 9I_LZ g JMS . u|g g I^l :: !: 'rallu9x9eqlrMlas elep
'lr os 's qtr.\\ p¡t:. I cq1 e8uur aqr .
Sutqloorus arp
sr:
'6uuollU
rellrl oql Jo llnsa,
?
:
p€uorlrsod aq p nc',
.L
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 eql sr uoqPlnslÉJ oqf xllletu ,t\au P uI paJols puP p¡18lnJI€J sI uolleJol loxrd eql JoJ anle^ 'reu e'1extd P tlo pJuolltsod sI Jollg áql ,.t,t,'"l.tn_,
66,,
oql olul lqanoiq Sutaq st uotluuuojul IPIcUl e 'sotlJ€olddP o,\\] eseql Jo rJtlllé ul senle^ Iexrd e3p3 3q1 alerrldnp o1 Jo '(SuIppPd olez) solsz qll{\ oáPrul rt{1
eL-
( 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
lmages and lmage
Chapter 5
74
*F;
if the sum is 0. a , in the next sectio The third sec coding, where n
fiftered natrix R (rR, cR) :sum(N(:)) /noRm; co-cR+ l; ?lnc courLó"
N:
T.
Z
end
rR:
rR
+1; cR: slart;
?
incr rR reset
accounting of inc
cR
end ?g Show re ligure (1) figure (2)
*
su 1Ls
colornap (gray) ;imagesc(R) ;colorrnap (gray) ; imagesc (M) ;
; ;
Done
This script is fairly complex and shows many interesting and useful concepts. Functionally, the script is in four sections, with each section identiirable by the double percent symbols 'o/'|%' . The first section is the header, where the only active code is for clearing variables and workspace'
The fourth section shows the two images on the scleen with only a command to set the colors used to display the images. The second section %% Matrix set up does exactly as that first line indicates: the matrices to be used are set up. Even though we are talking about images in this script, the starting point will be numerical matrices' The 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, we can use the function as a variable. Two matrices are created next: M, which will be used to hold the original data, and R, which will be used to hold 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 M matrix are set to 1, creating a sharp transition between the values. This line is sufficiently interesting to look at in detail:
for the new spati F matrix. The n spaces, their pu¡ The T matrix is a while the F mat column position i step, in which rl element. The \ r value. The locari controlled b1 rF appropriate.
MATLAB
result of runnin_s t
the righthand im the left. Because t calculation, thel around the right-l
f1: sizel , [1:
ffoor (síZe/2) ] ) :1;
2 set
haff to
:
Example 5.6 Use Z% %
¿
M_5_6.m
sWT 5-27-1€ Tunable spa:
_
?3 House keec _:.: c
M(
h¿
convolution ol rh
lear al1; c-:;
1
The commands in brackets identify regions in the matrix; [1: size] 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 rounded downward using the floor O function to 12. The size g function passes the matrix size to two variables that hold the maximum size information. Each of these variables is tracked individually so that the data set matrix is not required to be square. The firlter matrix F that will be used is lequired in this case to be square, but it can be any dimension provided it is smaller than the data matrix. The sum of the values in the matrix is calculated as an 'if statement' to ensure that
Matrix se-_ _! siZe-25;: s r:: r"l - zeros ( st _- ; M([1:saze]/ -_:¿*
= size (M) ; lrM, cMl : sizs
R
? Fi
'1.
Lter Mat:_:.:
F: I 111 _ 111,_ 1111_
ra]lLr.
elenbs aq
JSnur
xel^I. ?xptil.i oJ ozts u6rSse !: I
III i, IIITi I T T I I ] :J -r,rleh] falT lJ :l
(t¡f )
azTs
:
ll^lc
/l^I.r
I (t^l)32rs -
Iof fTeqtes I jI = \[(¿/3zrs),rooT]::Tllazrs:Il)t^i
U
Soj¡az Jo !trlel/{ : 1 (aZrS)so:réz -U 1 (32rs) so,raz:tr\ rT-rJeu azTS !¡ :gz-azts erPnbs Jo dn 1as xt.r1e¡ ;.:, r"lopur¡4
l¿ql alnsus o1 . t eql xl.tl¿ru U1:; 'c:enbs aq ot:., elep aqt lPrll AZIS UINUII\T?
¡: LL:
uorlcrrlu | = ueql 'c ¿l '¡nlr
surunloJ aqt s:r:
Iazrs:r]
:\rJl
pueuuroJ ecEdsl.ror¡ aql sf ÉaT:l lJTc lT-e ]e¿-¡ ! i burdealasnoH:j:, surT
,iélTrf Tetleds sTqeunJ 9I-¿Z-Q JMS :: ur'9 E I^l !: :r.
e.
q x q e Outsn las elep
e
lo Ouuollll lelleds ulJolod ol O ¿^uoc asn g'g alduJexS
'e8eLttr puuqJqFrr Jql punolP isploq lJelq 3q1 ur Énrllnssr 'oraz ]P pezrlPrlrur urPmá.r .{eq] 'uorlu¡ro¡er Jq] Suunp poreld3J ]orr s-ra,{\ slu3rxclJ YIJleuI o^\l rolno oq1 esn¿JaB UJI srll
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
eql 9 E sfdLuexa ul u,$oqs sr? 'xlrluru ¡8crur puu letlu I] Jo rrorlnlo^uo¡ ala.rJsrp ¡ql sulrojl3d tr,lq] ( ) z^ucc uorlcunJ ur-1[nq e sPq BV-1JV]i{ '31Pr.rdo.rddP
se lose.r puE polualu¡lJur 3.ru qf,rq,r !s3r1JP^ uJ pue u.r ,{q pJflo.rluo-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 eql ur slolünoc e^rJp sdool .roJ, o,{\1 lxeu 3qJ xululrr j
uulllor
srqJ
ssn
l|
.
aql ur sanlE.\ .-i:i: luJurelc sa : i! ploq ol pr.sn ¡q 'tr 1 :lxeu ps]lr:.:: '3utPu uorlJun-J ..
BV'IJVI I '¡Iq':
srll
ssJr-rlELrr
tnoqe 3ur¡¡er ourT
lslu
...r
"
i:
tPq'¡
pucrumoc p ilu.'ecuds¡:o,n pur: srll sr uorl¡¡s ti uorlces r.lJll¡ Llt: pue iur r< ¡n3esn
puE,{1o-r
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 pelnlrlsqns sr I Jo anln^ P '0 sr rulls oql -lr
-ruelJ oLuocJq IIr,{\ srql,roJ uosPei
3qJ
5ussacd¿ e6ew¡ pue sabeu
9 retdeqO
Chapter 5
76
11111 111111; lrF/ cFl - size (F) ; ?assign size to rMax, noRm- sum (F (: ) ) ; % sum values in F _f n oD,r" -_ 0; noRr _'; e.td,. U
? Scan, convolve and
lmages and lmage
cMax 15
replace
C:
conv2 (M. F, 'valld' ) ; %Z Show re sult s figure (1) ; lmagesc (M) ; colormap (gray) ; f igure (2 ) ; imagesc (c),' cof ormap (gr:ay) ; f11gure (3) ; subpfol (I,2t7) ; imagesc (M) ;coforrnap (gray)
Figure I
; axis
square; subplot (1,2,2) ;rmagesc (C) ; colormap (gray) ;axis square;
One of the r 'Unsharp Mask' works is to firs¡ 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 Many differe effects can be us
r
:
Done
s 10 1s 20
25
s10152025
5.4 (left) Original image and (right) the filtered image. Not¡ce that there is a black 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. Figure
The script in Example 5.6 includes the conv2 g function approach, and the function results of applying the hlter are shown in Fig. 5.5. The conv2 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.
g
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 imagefiltering techniq te lhat does have clearly evidelt effects uses sharpening masks, which enhance regions of abrupt change.
5l
5.6 (left) O Notice on the right-l made to stand out f F¡gure
'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 rallrJ 6uruadreqs oql 6ur^ldde.ieue ebeur aql (lq0u) pue aOeL!r leutorJo (Ual) 9.9 €Jn6tJ
sz0zEr0rs
9¿ OZ 51 01
S
Suru:d.teqs sasn -aSerur
uy
l
s:.i..
rruo-r1la1: ur 1d;: ñur,ner,r aqt iq I 3nr1:o,u u:q rr I
aiur
3u11¡nser eq1
t
Sutsn 1noq1r
ut
r
¡qt i: oz i:_
sl uonJUnJ
uorlsulg
aqt pue 'qrpo:rl.l:-
ur s3rl3rlls3r? ,roJ sP IIa,t\ sP Sursseroid PlPp rrlrluorss.roJ pasn oq uec s1J3lJ3 eql pue'oaelur ue ol perldde Jq uer siolluJo suorluulqtuoj luera.lJrp ,{ueIN '¿ 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 sseulnJosn rqJ tr Jo '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 oqt sr:al1g Suruad:eqs lsuq y -'ueroeldp-I '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¡. Jo aluuu lunsnun Jrll spq slatlu Suruedruqs 8urls::a1ur eloul oql Jo JrrO .pro¡ Áa) ,prle^, aql 6ursn O ZAuoc 6uruunr jo
aql a.iaq¡ 'slox d ,: )celq E sr eteql le,:
9¿
1¡,-=
a¿
a
sTxe ! (.i\a:.
a
.
.:
sllnsou 9'9 arnorj
0agr0rs5z0asr0rE
6ussecoL¿ o6ewl pue sa5eui
LL
9 )eldeqC
Chapter 5
Part 800
800
200 400 600 Figure
5.7
800
200 400 600
800
(right) Or¡ginal image and (left) the sharpened image.
enhancing images. Being able to construct and apply custom image filtels is a powerlul skill and can also be interesting.
5.6 Practice Problems 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]? 1.
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).
Chaptels 6 thror
often encounte¡e
developing soluti, visualization tool
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