opencvpart05.pdf

Upload: tieu-bao-vi

Post on 04-Jun-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 openCvPart05.pdf

    1/7

    SeeingWithOPenCImplemnting Ei enfa emonth's'rticleexPlainedthe face recognitionalled igenfaceorks'month's rticle oncludesothheOpenCVeriesnd heigenfaceopicwith a detailedlookat a completerogmmorimplementingigenfaceithopenCV.

    r rere 's a brief ecaP f astmonth'sl{artlc e expla ing how eigenfaceI lworksEgenface onsists i iwo Phasesllearningnd ecoqntion.n he earnrngphase, ougiveegenldce neor rnoreface magegor eachperson oLlwant tto recognize.hesemagesre aledhetraningmages. the ecognlt0nnase'whenyougiveegenface face mage'responds y tellingyo! whch traLn gmages "closes{'tothe new ace nrage.Egenfacegeshe raln g magesto "learn" a face mode Thls iacemodel s created y applyng a methodcaled Prlncipa omponentsna srs

    (PCA)toeducehe dlmensona y" ofthesemagesEigenfaceeflnesmaged mensionalltys he n!mberof pixelsLn n mageThe owerdimens na tY epresen_talion hal eigenfacends durng helearnlnghases caleda slbspacenthe recognit ionhase,t reduceshed mensionallty f the input magebJ/"projecting"it onto the sLrbspacetfound uringearningProjectngontoa subspace' eansindinghe closestpoint n that subspace. l ter theunknown ace magehasbeenProjected, eigenface a Lrates the dlslancebetweent andeach ralnlngmage. ts

    nq mage. o! canthen ook up whichperson eigenfacedentlfied.SettingUp oqEigenface

    In use,you' lprobablywant tocombineigenlacewlth the facepresentedn Palt2of thls series TosmpLilyhe example code for thisl ' l l be assum gyou a readyhaveaset of trainingmages nd a set

    Ele E-dit Fqrmat !ie^ lLelP1 s1r' 1. gm2 s2 i t .pgn4 s4,i . pgm

    Ele Edit Fqrmat1 s1.11. gm2 s2lL- pgn4 s: lr i1.pgm1 s1 'JZ . gm2 s2 2 -pgn4 s4 2 -pgn1 sl.r3. pgm2 s213 .p }n4 s4//3.pgm1 s1l4, pgn2 s2/4. pgtr.4 s4 /4 - Pgttl1 s1./5.pgm2 s2 /5 .pgn4 s4 /5 . pgm1 s1 /6 ,pgn2 s216.pgtn4 s4 /6 .pgn

    a t I F o j e c r e d r e s r ' a c e r ,

    iFrcuRE. Th"

    44 sERVo5.9oo7

  • 8/14/2019 openCvPart05.pdf

    2/7

    ace magesmust6e exactly he samesize,FIGURE.Theprintusagehelprfunction.

    and cwEigerDecomposite),that are defined n cvaux.h.The primary ariablesoreigenface re definedat ines8-15. One of the datatypeshere, i,,t4a ,sonewe havenusedbefore. his s OpenCV matrixdatatype. matrix ontains tableofdata,arrangeds ows ndcolumns.lf you only need o hold datatemporarily ithinyour program, nordinarystyle rrays usualy l i t teeasiero usehanc\/nlat. utthe \,l|ardataiypeanbenlcewhen ouwant otake dvariagef OpenCVunctionsorworking ithmatixdata. he nes e'lluseareOpencv'sersstenceunctions.With hese, oucanstorematrix atawitha sing ine f code. eadlngt backintoyourprogramsater5 ustaseasy.Here,'veused Carrayor \,vo arables(f acelmgAra and eiga]1vectArr) andc .r4aror several thers.TheMin0 funct ion lmpy eadsthe input string, hen callseither heleam0 method or the rccosnize0method. Figure 3 shows theprintusage helper unction.

    plintusraa l) wo id Pr in tusageo: ]: p r i n t f ( ' l s aqe r e iqen f ac e < c om and> \ n " ,

    valid comands are\n"t r a i n \ n , i

    t

    For heexampesn hisartce, 'vea free,publjcly vallableacedatathe OllvettlResearchab s(ORt)Database. he URL s isted n thend Resourcesidebar.Toset up or uslng igenface,nzipORL atabasen ihe samedirectoryhisdata'contains0 face maqes or each40 subjects. hese reorganizedntod rectorles, ameds1's40.Eachcontains10 images,namedpgm1o.pgm. l l heORLmages rehesame lze 92x 112pixes.You'l laso need wo input i les:and est,txt,Flgure showsanf thesenput i les. oth l lesthe same ormat:personnumber,ath o image ile.Youmayhavenoticed hat he l6ielt magesare he sameas theheseare useful esteigenfaceshouldive he rightanswerwhenyouecognize ne of its tralningf lt doesn't,you know J/ousome ebuggingo dolTo run the learnlngphaseof this

    prompt.To run thenase, nrer

    Fgures -10 ontainhe completeisting or a basic lgenface roTo keep he presentaton imple,omittedmostof the efforchecking.

    istingFlgure shows he top-evelsourceprograrno earnaceswith opencv'smethods. t line4, it includesjnt now,we'veonLyncludedhighgu.h But we l useunctions for face

    FIGURE. The eam0 function,Thk unctionmplcmntshelearninghasc f i5nface.

    The LearningPhaseFigufe 4 shows the lea 11ofunciion, which implements heearninqphase s our steps:1) Load he ralning ata ine6).2) Do PCA on it to find a subspace(l ine 6).3) Project he training acesonto thePCA ubspaceines2029).4) Save all the lrainlng information( ne32).a) Eigenva esb) Eigenvectorsc)The averagerainlng ace maged) Projectedacese) PersonD numbersThe next oursubsectionsnayzeeach

    : , / Io..lI n r r a i nFac es = loadFac e luqe r ay ( " r r a i n . t x t " ) ;i f l n r r a i n E a c e s < 2 )r fprintf (stderr,

    2 or nore training faes\n"L r i l npu t f i l e c o a in6 on r y t d \ n i , nT r a in r ac es ) ,

    r / ' , d c F . A . n : he L : 6L r r r , J a . es

    i / , p r o j e c t:, projectedTlalnFaceMa = cvcreateMat (nTrainEaces, nEiqens, cv-32Fcr) ;' _ f o r { i = 0 , i < . r r a i nFac eE ; i + + ): cvEigenDeconposite(

    r r facehsalrlrl /r I nE r gens ,j eigenvectar ,i pAwgrrar.Ing,.3 p r o jec t ed r r a i . Eac eM a t > da t a , f l + i l nE i s ens ) ;

    Lh . t : c 09n i : i o f, : s t o r eT r a in i ngD a t ao ,

    sERVos.9oo75

  • 8/14/2019 openCvPart05.pdf

    3/7

    loadtacerogA Y()int loadFacernqturay(char r firename)l F E t i m s l i s t F i r e = 0 ,

    cha ihs r i l enane l5 r 2 l ,in t i . a c e , nFac es = 0 r' / . pc r : r r ra rnp :L r r1 .

    : i n s f , i s t F i l e = f open ( f i l enane , ' r " ) ;

    I Hh i re ( f qe t6 ( insF i renane, 512 , i nq l i s t F i l e ) ) + + nFa . es rr r e { i nd ( i nq l i s t F i r e ) ,

    f ac e r nga r r = { r p l l naqe r i ) cvar loc ( nFac es r s i z eo f ( r p l r m aqe r ) ) ;r p{sonNunTluthMat = cwcleateMat( , nFaces, Cv 32SC1 );

    for(iFace=or iFacedata,i+iFace, imqF ename),/ , / 1 .ad rhe i r c : raqe

    i facrmgAlltiFacel = cvloadhase ( nqFi renane ' cv-loAD r&\GE-GFAYSCALE)I, f c l os e l i ng l t s lF i l ) ;

    of thesesieps n detalLoading Face mages orTrainingor TestThe oadFacelrcAray( tUnctron(Figure5) loads ace imagesandperson D nurnbers or both thelearnlngnd ecognit ionhases.The face mages assumed ereto be a the samesize are storedln the globai variable facermgarr;loadracelmqArray ) returns thenumber f face magesoaded.ThepersonD numbers restoredna C 'lv1at arlable,personNludrruthl4at."Truth" here refers o the Al term,"groundlruth." t meanshe valuesthls variableare the true {correcqvaues or each ace mage.During heearnlng hase,lhosere heonytypeof person D numberswe have.Butduring he recoqnit ionhase,heprogramwill have both ground ruthvaluesGpeclfiedn the file est.txt)andthe outpui from e genface. Havingboth alows us to eva ate howwelLelgenface oes under varylngcondltons.The crcrealeMal0 Tunclroncal led at l ine 16 - creates hepersonNunllfrtbMatariabe.Ths func_lion takes hreeparametershenumber46 senvo sqoo;

    of rows,he number f columns,ndthedatatypeor hematrix. na 32bitoperatingystem,he datatypeor amatrix f int valuess cnr' 2sct.Theshere tandsor"signed,'and l ndicateshat hematrx as ne hanne.(Arnatrlxanhave p o fourchannes.N/ultiplehannek Llowou o add athirddmensiono a matrix arlable.)The 6dqat datatype s a slrr.rch,wlth the raw datastored n the structelement dara; data ls de{ined as auion (the definltion s gven n theCXCOREocumentation), i th inrdata accessedas data.i . L nes22-23show one way to access nilatvalues asoffsetsrom thestartofthedatabufferN,4atrixows are aigned to starton four-bytentervals. he in-mernoryrow width, ln bytes, is storedin cviqat, rep, Sincewe're using afour-b),tedatatype with this matrlx(andaso,slncet hasonlyone row),we can gnorec\4taL. tep. But, i youcreatea matrix of, for exampe,chardata, you may need o take the stePsize nto accountwhen youaccesshedataeements.Finding he PcA SubspaceThecode o f nd h PCA ubspace

    FIGURE.Ihe loadFacelnArayfunction oadsac magesndpersonlDnumbersor both he earning nd

    ls n Figure6. l t cal ls he bu t ' inOpencv f nction for doing PCA,cvcaLcEisenobjects, at lnes27 36.The remainder f dopcA0 createsheoutput ariableshalwil hold he PCAresults when ca{alcEiqenobj cts (At I ne 8, the numbef f egenvalues s set o one ess hanthe numberof trainlng rnages. As explainedastmonth,his s hemaxmum umber felgenvalues/e an ind.)L n e s 1 1 - 1 5c r e a te h e g l o b a llmage afray eigenvectArr. Whencv{alcEigenobj cts ( returns,eachimagen his rayw lhold ne igenvector,on elgenface,"in otherwords.Notethat hese re loating-polntmages, thdatadepth= IPI,_DEPTH2F.At line 18, anothermatrlx s creaied eisenvalNlat. hs matrlxw llhodthe elgenvalues.he eigenva es arefoating-point umbers, nd we onlyneed one channel or ths, so thematrix ype is gv 32 c1. ThatgNesusa onechanne matr ix,with 32bi i ,floatingpolntdatavalues.To do PCA, he datasetmust irstbe "centered."For our face images,this means inding he average rnage- a n m a g e n w h ch e a ch PXe lcontains he averagevaLue ot thatpixel acrossal face images n thetraining ei.Thedatasets centered ysubtracting he average ace's plxelvaluesromeach rainlngmage.Youdonl haveo do hatyourself.thappensnsdecrcalcEigenobj cls 0.But you do need o hod onto theaveragemage,because ou'llneed tater o prolecthe data.so you'l needto alocate memory or the averageimage.The code or doing that s atl lne21. Note hat l lke he elgenvec-tors- this s a fLoatlng'pointmage.The last step before cal l ingclcalcEiqenobj cts ( is o preparedata structure a ed CL4emcliteria.The leldsn thisstructure peclfyerml_nation r i ter laor i teratve qorthmssuch as PCA. You can read moreabout c\temcriLeda optons n tne

    CXCOREocumentation. ere,we cansrnply el it to compute ache genvalue, hen stop,slnce hat'sall we need,

  • 8/14/2019 openCvPart05.pdf

    4/7

    FIGURE. doPca{)inds hePcAsubspace singOpencv!cdaLcEigenobjects o functlon,code or hat sat l lne24.Now hat l l the utput ar iabesrewe call cacalcEigenobj cls (sLrbspaceor theThe ast parameter,

    >da!a.fl, rs he po nterthe data vaues n eigenva at.we use he data.f l - f ied, not, sncethismatr ix ar iableoldsata.Training FacesNow hatyou'veounda subspaceyou can convert themages to points in thissexplalnedstmonth,his"prolectlng"the trainingOpencv unct ionor thisls caled crEigenDeconposite. l ine22).The Opencv uncilon namesare,Not only isprojectlonunctionoddly named,there'salso a funct ionnamedthat doesn'tprojectonto the subspace.l t does the oppose. t

    uncompresses)rojectednq i t back nto he or ig alThe corect name or doingot Projectionl

    v ^ " l n a o r l : ^ : . o i ^ n ' t t h .rainingmages.ne 19 n4 creates matrlx or thathe or loop,at llnes20-29,once forning mage.FaceModelThe sma bt of extra effort toOpenCVtcvnhr datatype ea yoff when t comes ime to saveFgur7 shows hecode or savlng llthe datayour earned ace representationsan XML ie usingOpenCVbull tpersistenceuncuons.At ine 7, the cal l to) opens ani le named acedata.xmL.heparameter o ihis funci ion

    mode. Here, t's( savAlthe learned faces 6n xML file using

    cv slDtAGE VtRrTE,Wnrcn rneans rocreateoroverwrte) thatie andopenit fof writing.To write basicclanguagedata -integers, loalng-pointvaues, andstrings n XML orrnat,you can usethe function c\4,irite O.

    Forexample,he cal to ot{riternt 0at ine 10 wrtes the nLrmber feigenvaluess

  • 8/14/2019 openCvPart05.pdf

    5/7

    IcvMat r trainPersonNudla - 0;f r o a t ' p ro jec tedres tFace - 0 ;

    r o a d t a c e l f t g h r a y ( i t e s t . t r l " ) ,p r i . t f l i t d t s t f a c e s I o a d e d \ n " , n les tFaces ) r

    if ( lroadrrainingoata(, ' r . j : . rprojectedTeslFace = (f Loat r)cwAlloc( .Eisensrsrzeof (f roat) ) ;

    , f o r { 1 =0 , idata i(i l;d a t - >d a t a . i l i Ne a l e s t l ;' p l i n t f ( i n e a r e s t = i d , r t n t h = l d \ n " , . e a t e s t , t 4 t h ) ;

    tLines 215add hreematricesndanimage o the same XML fi1e.Thebuifin persistenceunctionssavenotony the row and columndata,but alltheheadernformation,swel. HerestheXML hat ine13generates:

  • 8/14/2019 openCvPart05.pdf

    6/7

    0. fhe findNearestNeighbo )function computs h dktancefrom the projectd tst image to eachprojected rainingampleto find theclosst raining mage.dation heck o makesure hesehave a non ero valueouuse hem.To simpfy theode,'veomittedheseand

    va dationsteps rom theunction.After al the data are loaded,step n the recogn on phaseprolect each test image ontoPCA subspace nd locate heprolected rainlng mage.for loop at Ines 16-34of theon (Flgure 8)his f inal ste p.The calO, whrchhe est mage,ssimiaro thetion ode ln the 1eam0

    As before,we pass the nurnbergenva es n igens),and he arrayelgenvectorseigdvectAfi). Thiswe passa rest tmage,of a tra ning image, as theparameter. he output fromsile rs stored n avariab prcj ectedTestFace,here's no need o store heest image, ve used a cprojecledTestFace, atheran OpenCV atr ix.

    NeighborAs last month 's d lc leexpaned,recognzes"a face m.qe byorthe ralninqmagehai'sclos-n the PCA ubspace.ind g heexamplen a earned ubvery commonAl techniqueNearesteghbor atching.Figure 0 showshe code or theighbor luncton. tprojectedprojected rainlngThe distancebasis here isEucl ideanistance."s asto umn explained,o caculatedistance between twoou'dadd up the squared isea.hdimension,hen ake heHere,we takebut skip he square oot step.nal resut s the same, ecauseneighbor i th hesmalest istancehas hesmalest quaredlstance,we cansave ome omputationlme

    0ycompar g squareoaues.The for loop at lnes 6-22computeshesquaredistanceo eachprojectedrainng nrage, nd keepstrackai lnes 821)ofwhch ralningimages closest.The eturn aue s the ndex flhe closestrainingmage In thercccsaize function Figure ), thisreturnaluesused, t lne 1, o ookup the personD number ssociatedwith he nearestraininqmaqe.l a r a < r l aa ^ r ^ r ^ , r ^ , r f r ^m r ha

    reccanize0 functlon:

    m srecognizedsSubject nstead f 4.lmpnovingigenface

    Havinga framework lke thls fortrainng and testng wil l make i ieasieror you o add mprovementsoelgenface nd o test heireffects.Oneof the first mprovementsoumightwant to add s to changeheway dlstances measured. heoriginaleigenface paper used Eucl ideandistances etweenpoints,and that'sthe distance basls l 've used infindNearestNeighbors0. But ad fferent basis,caled l\,4ahalanobisdistance(after ts lnventot, usuallygives etter esults.one of the things hat happenswhen you projecta face image ontoihe PcA subspaces that eachdlmension receives certainamount ofstretch. heamountof stretch sn't hesame, hough, n everydirection.Thedlrections hat correspondo theargest elgenvalues et stretched armore than the dlrectionsassociatedwith sma er eigenvalues. ecauseEuclidean lsiancegnoresthis stretchng, using t to measure istancesapproximateyhe same s using nlyone eigenvectornd gnoring he restlit easy o switch rom Euclideanto lvlahalanobisistance. ustchangeNotmismaich:badl We image was

    = 2

    - 4- 2- 4= 1= 4

    inL f indNed estNerghbo I loat ' p o jecLedle6LFace)1i double leastDi6lsq = DBI_II4AX;: in t i . iT la in , iNearest = 0; fo ( iT la tn=0; iT la in

  • 8/14/2019 openCvPart05.pdf

    7/7

    . OpenCVon Sourcefoqelrltp.l / fiutcdogs.n'.l/ prolects/opencvllbr y

    . Offlclal opencv Useryrcuphttp://t.ch,groupe.yahoo,com/group/OpenCV

    . Turlt M" Pentfan4 A., Fdce ccognition using eigenfacesrProc. EEE onf.on Computer ision nd PdttrnRecognition,199l.

    ' oRL Databasewwt .cl.cam..c.uk/taalrchldtg/atGrchivaf aced8tlbasa,himl. Source ode in thisarticlecan be downloaded romwwa,cognotl6.com/oPncv/lervo.

    I ne 15 n findNearestNeigrbors tromdistsq += d ird i;

    distsq += d_iad_i/eiqenvalMat - >data 1 [i] tSwtchingo Mahalanoblsis tanceirninateshe ms_matcherrormentloned bove, ringing ecognition ccuracyup to 100o/oor these hreesubjects.

    Whereo GoFromHere?Thisartcle ntroducedevera ew OpenCVoncepts.oucangaina deeperunde6tandg of these romthe OpenCVdocumentatlon.hepersislencefunction5,he cferrncrlteriasrrircr, and he cvqat datatype redescr ed n deta in theCXCORE ocumentaton. he e genface unct ionsaredescribed theCVAUX ocumentation.heCVAUXocumen_

    tation sn't nked rom he documentatonndex age, utyoucan ind it in the documeniationubdirectoryamed et.f you want to incorporateigenfa.enio a systernhaidetctsacesn ive ideo, ou'llf stneed o detecttheface,henextract intoa separatemage.Since ach ace magemustbeexacty he same ize, he eatestway o do that s o definestandardze,say50x 50pixels,head f t me.Then,when oudetect face, oucanuse ode ike hls o extract nd eslzet:/Fe r pE. aoF- c Re. . - ' r j - t s -o .1en oRe. l seq. 0 ' .oselrEgeRor (prtrEi *pEaceRect ) ,IpIJMge * lFacelq -oCreatelcae( sTD SIzx, IPr,_DEPII 0U, 1 );sResizelphg, pFacelrg, cv_INER IREA );

    There remorecapabillt sbullt nto OpenCV nd many,many norecomputer ison programs ne cancreateusingthis ibrary. hope this shod seriesof a|ticleshas givenyou a taste of what's possiblewith OpenCVand perhapsmotivated ou o expore moreof its capabiittes.8e seeing oLr

    TTEERJtnnlncRollorTtut|rlrorfitcrvot

    De'o r orooor 'o-dspeeddrreLto_ 'rd.+eer 'r9i trf or / tdo Rdo Co' l . l c-dn,re. ' \ p l r - l?s s i- 's ospardte rush-type lectrlcmotorsmounted ightand eitwlthour mixingRDFR ualspeed control. Used n manysuccssfu ompetitivobots.Singleoystck operationupgoes vaight heod, own s revrs.ure ghtor left wir svehicle smotofs rn opposit irectons.In etween tckposi tons ompletey roportondl .LiSsn ke a servooyourFutaba,R,Niiec, rs m ar adio.CompdiibwithSyroster ingtdt, l izat ion,drous ot anddmpszesavdi lable.The RDFR4TE5V 754 per motor unt pctured abov.www.vantec.comlfrnvtrrc,,,,o,Tlll'ouu50 sERVos.eooT