基于PCA的人脸识别opencv代码_第1页
基于PCA的人脸识别opencv代码_第2页
基于PCA的人脸识别opencv代码_第3页
基于PCA的人脸识别opencv代码_第4页
基于PCA的人脸识别opencv代码_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、基于pca的人脸识别opencv代码# inelude stdafx. h# include < stdio, h ># inelude < string h ># include "cv h # include cvaux h # inelude "highgui. hh # inelude "time.# include "iostreantusing the namespace cv;using namespace std./ / define several important global variablesiplima

2、ge * * fdcelmgarr = 0; / / pointers to training faces and test faces (points to different learning and recognition stages)cvmat * personnumtruthmat = 0; / / the id of the face imageint ntrainfaces = 0; / / the number of training imagesint neigens 二 0; / / the number of major eigenvalues that you tak

3、eip1image * pavgtrainlmg 二 0; / / the average of training face dataip1image * eigenvectarr = 0; / / projection matrix, which is the main eigenvectorcvmat * eigenvalmat 二 0; / / eigenvaluecvmat * projectedtrainfacemat 二 0; / / the projection of the training image/ / function prototypevoid learn ();vo

4、id recognize ();void dopca ();void storetrainingdata ();int loadtrainingdata (cvmat * ptrainpersonnummat);a float * projectedtestface;int loadfacelmgarray (char * filename);void printusage ();/ / the main function, which consists of learning andidentifying two phases, needs to be run twice, separate

5、d by the parameters passed by the command lineint main ()/ / learn ();recognize ();/ / the learning phase codevoid learn ()cout <<<<<<<<<<<<<">>/ / start timeclock_t start and finish;double duration;start 二 clock ();int i, offset;/ / load training im

6、agesntrainfaces 二 loadfacelmgarray (“ train. txt “);if (ntrainfaces < 2)fprintf (stderr,"need 2 or more training faces n"input file contains only % d rt, ntrainfaces);the return;/ / the main component analysisdopca ();/ / the training atlas is projected into the subspaceprojectedtrainfa

7、cemat 二 cvcreatemat (ntrainfaces, neigens, cv_32fc1);offset 二 projectedtrainfacemat-> step/sizeof (float);for (i = 0; i < ntrainfaces; i + +)/ / int offset 二 i * neigenscveigendecomposite (facelmgarr ineigens,eigenvectarr,0, 0,pavgtrainlmg,/ / projectedtrainfacemat-> data f1 + i * neigenspr

8、ojectedtrainfacemat-> data fl./ / the eigenvalues of the training phase, the projection matrix,etcare stored as. xml files for the teststoretrainingdata ();/ / end timefinish 二 clock ();duration 二(double) (finish start)/clocks_per_sec;cout < < "duration <<<<<<<<

9、<<"<<<<<<<<<<< > < < < </ / recognition phase codevoid recognize ()cout < < "the beginning of the recognition process" < < < < < <<<<<<<<>>>/ / start timeclock_t start and finis

10、h;double duration;start 二 clock ();/ / test the number of facesint i, ntestfaces 二 0;/ / the number of faces in the training phasecvmat * trainpersonnummat 二 0;float * projectedtestface 二 0;/ / load test images and return the test face numberntestfaces 二 loadfacelmgarray ( test txt “);printf (“ d te

11、st faces loaded n "、 ntestfaces);/ / load saved in. xml fileif (! loadtrainingdata (& trainpersonnummat)the return;projectedtestface 二(float *) cvalloc (float);for (i = 0; i < ntestfaces; i + +)int inearest,/ / projected the test image into the subspacecveigendecomposite (facelmgarr 叮.ne

12、igens,eigenvectarr,0, 0,pavgtrainlmg,projectedtestface);inearest 二 findnearestneighbor (projectedtestface);truth 二 personnumtruthmat(1).printf ()ij/ / end timefinish 二 clock ();duration = (double) (finish start)/clocks per sec;cout <<<<<<<<<<<<<<<<<

13、<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"><<<<< < </ / load saved training resultsint loadtrainingdata (cvmat * ptrainpersonnummat)cvfilestorage * filestorage;int i;fi

14、lestorage 二 cvopenf订estorage (“ facedata, xml "、0,cv storage read):if (! filestorage)fprintf (stderr, "can't open facedate xml n);return 0:neigens 二 cvreadlntbyname (filestorage, 0, "neigens", 0);ntra in faces 二 cvreadlntbyname (filestorage, 0, ,ntrainfaces,0);* ptrainpersonn

15、ummat = (cvmat*) cvreadbyname (filestorage, 0, z,trainpersonnummat", 0)eigenvalmat 二(cvmat *) cvreadbyname (filestorage, 0, eigenvalmat, 0);projectedtrainfacemat 二(cvmat *) cvreadbyname (filestorage, 0, ,projectedtrainfacemat,/, 0);pavgtrainlmg 二(ipllmage *) cvreadbyname (filestorage, 0, ,avgtr

16、ainimg/,, 0);the eigenvectarr 二(ipllmage *) cvalloc (ipllmage *)for (i 二 0; i < neigens; i + +)char varname 200sprintf (varname,"eigenvect_ % d” ,我);特征向量i = (lpllmage*)cvreadbyname(filestorage, 0, varname, 0);cvre1eas ef i1estorage(&filestorage);返回1;/ /存储训练结果 空白 storetrainingdata ()cvfil

17、estorage * filestorage;int 我;filestorage 二 cvopenfilestorage( “facedata。xml” , 0, cv_storage_write);/ /存储特征值,投影矩阵,平均矩阵等训练结果cvwritelnt(filestorage, “neigens” , neigens);cvwritelnt(filestorage, untrainfaces,? , ntrainfaces);cvwrite(filestorage utrainpersonnummat” , personnumtruthmatcvattrlist(0, 0);cv

18、write(filestorage eigenvalmat,eigenvalmatcvattrlist(0, 0);cvwrite(filestorage uprojectedtrainfacemat, projectedtrainfacemat cvattrlist(0, 0);cvwrite(filestorage “avgtrainimg” , pavgtrainlmg cvattrlist(0, 0);(我二 0;< neigens; + +)char varname200;sprintf (varname, “特征 vect_ % d” , i);cvwrite(filesto

19、rage> varname eigenvectarr 我,cvattrlist (0, 0);cvnormalize(特征向量i,特征向量i, 255, 0, cv_l2, 0);cvnamedwindow( “演示” ,cvjvind0w_aut0size);cvshowimage( “演示” ,eigenvectarr我);cvwaitkey(100);cv 标准化(pavgtrainlmg, pavgtrainlmg, 255, 0, cv_l 1, 0);cvnamedwindow( “演示” ,cvjvindow_autosize);cvshowimage ( “演示” ,pa

20、vgtrainlmg);cvwaitkey(100);cvreleasefilestorage(&filestorage);/ /寻找最接近的图像int findneaxestneighbor (浮动* projectedtestface)/ /定义最小距离,并初始化为无穷大双 leastdistsq = dbl_max、准确性;i,it rain, inearest 二 0;双10;(itrain 二 0;itrain < ntrainfaces;itrain + +)双 distsq = 0;(我二 0;< neigens; + +)浮动d iprojectedtest

21、face 我projectedtrainfacemat - >数据。fl (itrain * neigens + i);/ / mahal anob is算法计算的距离/ / distsq +二d_i * dl;/ /欧儿里得算法计算的距离(我);(我);itrain二 distsq;如果(distsq < leastdistsq)leastdistsq 二 distsq;inearest 二 itrain;/ /求阈值双max =0,阈值; int j;(j 二 1;< 1o;j + +)如果(max <j)max 二j;其他的max 二 max;jthreshold

22、 二 max / 2;/ / similarity ratioaccuracy 二 1 - leastdistsq/threshold;the cout <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<return inearest;/ / main component analysis

23、void dopca ()/ / terminate the algorithm criteriacvtermcriteria calclimit;/ / tectonic imagerycvsize facelmgsize;/ / set the main eigenvalues for yourselfneigens 二 ntrainfaces 一 1;/ / allocate the eigenvector storage spacefacelmgsize width 二 facelmgarr 0 - > width;faceimgs.height 二 facelmgarr 0-h

24、eight;/ / the number of assigned numbers is the number of eigenvalueseigenvectarr 二(ipllmage *) * neigensfor (i = 0; i < neigens; i + +)eigenvectarr i二 cvcreatelmage (facelmgsize, ipldepth32f,1);/ / assign the main eigenvalue storage spaceeigenvalmat 二 cvcreatemat (1, neigens, cv_32fc1);/ / distr

25、ibute the average image storage spacepavgtrainlmg 二 cvcreatelmage (facelmgsize, ipl_depth_32f, 1);/ / set the pca analysis to finish the conditioncalclimit 二 cvtermcriteria (cv_termcrit_iter, neigens, 1) / / calculate average images, eigenvalues, eigenvectors cvcalceigenobjects (ntrainfaces,(void *)

26、 facelmgarr,(void *) eigenvectarr,cv_eigobj_no_callback,0,& calclimit,pavgtrainlmg,eigenvalmat - > data. fl);/ / normalized sizecv normalize (eigenvalmat, eigenvalmat, 1, 0, cv_l1, 0):/ / load the enumeration of the txt fileint loadfacelmgarray (char * filename)file * imglistfile 二 0;char imgfilename 512.int iface,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论