收藏 分销(赏)

[人脸识别Step]在opencv下作人脸检测.doc

上传人:精*** 文档编号:2631809 上传时间:2024-06-03 格式:DOC 页数:18 大小:764.04KB 下载积分:8 金币
下载 相关 举报
[人脸识别Step]在opencv下作人脸检测.doc_第1页
第1页 / 共18页
[人脸识别Step]在opencv下作人脸检测.doc_第2页
第2页 / 共18页


点击查看更多>>
资源描述
[人脸识别Step]在opencv下作人脸检测 ———————————————————————————————— 作者: ———————————————————————————————— 日期: 18 个人收集整理 勿做商业用途 [人脸识别Step1]在opencv下作人脸检测 本帖被 admin 设置为精华(2007-08-03) 人脸识别的第一步,就是人脸检测。把人的脸部从一张照片中用计算机自动识别出来,作为下一步人脸识别的基础。 在opencv 中,库中自带了一个利用harr特征的人脸检测训练及检测函数:cvHaarDetectObjects.它利用训练好的检测器,在图片中间检测你想要的 物体,如人脸。opencv自带了很多检测器,在%opencv%data/haarcascades目录下,你可以随意取用。或者你也可以自己用图片训 练自己的检测器,之后拿来使用. 下面是检测人脸的源代码: // DetectFaces.c // // Example code showing how to detect faces using // OpenCV's CvHaarClassifierCascade // // See also, facedetect。c, in the samples directory。 // // Usage: DetectFaces 〈imagefilename〉 #include <stdio.h> #include "cv.h" #include "highgui.h" // *** Change this to your install location! *** // ********************************************* #define OPENCV_ROOT  ”C:/Program Files/OpenCV” // ********************************************* void displayDetections(IplImage * pInpImg, CvSeq * pFaceRectSeq, char* FileName); int main(int argc, char** argv) {     // variables     IplImage * pInpImg = 0;     CvHaarClassifierCascade * pCascade = 0;  // the face detector     CvMemStorage * pStorage = 0;        // memory for detector to use     CvSeq * pFaceRectSeq;              // memory-access interface     // usage check     if(argc < 2)     {         printf(”Missing name of image file!\n”                ”Usage: %s <imagefilename〉\n”, argv[0]);         exit(-1);     }     // initializations     pInpImg = (argc > 1) ? cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR) : 0;     pStorage = cvCreateMemStorage(0);     pCascade = (CvHaarClassifierCascade *)cvLoad        ((OPENCV_ROOT”/data/haarcascades/haarcascade_frontalface_default。xml”),        0, 0, 0 );     // validate that everything initialized properly     if( !pInpImg || !pStorage || !pCascade )     {         printf("Initialization failed: %s\n",             (!pInpImg)?  "can't load image file" :             (!pCascade)? "can’t load haar—cascade —- ”                          "make sure path is correct" :             "unable to allocate memory for data storage", argv[1]);         exit(-1);     }     // detect faces in image     pFaceRectSeq = cvHaarDetectObjects         (pInpImg, pCascade, pStorage,         1。1,                      // increase search scale by 10% each pass         3,                        // merge groups of three detections         CV_HAAR_DO_CANNY_PRUNING,  // skip regions unlikely to contain a face         cvSize(40,40));            // smallest size face to detect = 40x40     // display detected faces     displayDetections(pInpImg, pFaceRectSeq, argv[1]);     // clean up and release resources     cvReleaseImage(&pInpImg);     if(pCascade) cvReleaseHaarClassifierCascade(&pCascade);     if(pStorage) cvReleaseMemStorage(&pStorage);     return 0; } void displayDetections(IplImage * pInpImg, CvSeq * pFaceRectSeq, char* FileName) {     const char * DISPLAY_WINDOW = "Haar Window";     int i;         // create a window to display detected faces     cvNamedWindow(DISPLAY_WINDOW, CV_WINDOW_AUTOSIZE);     // draw a rectangular outline around each detection     for(i=0;i〈(pFaceRectSeq? pFaceRectSeq->total:0); i++ )     {         CvRect* r = (CvRect*)cvGetSeqElem(pFaceRectSeq, i);         CvPoint pt1 = { r->x, r->y };         CvPoint pt2 = { r—〉x + r-〉width, r—>y + r—>height };         cvRectangle(pInpImg, pt1, pt2, CV_RGB(0,255,0), 3, 4, 0);         cvSetImageROI(pInpImg, *r);         //char* FileName = argv[1];         IplImage* dst = cvCreateImage( cvSize(92,112), pInpImg—>depth, pInpImg—〉nChannels);         cvResize(pInpImg, dst, CV_INTER_LINEAR);         strcat(FileName,”.pgm");         cvSaveImage(FileName, dst);     }     // display face detections     cvShowImage(DISPLAY_WINDOW, pInpImg);     cvWaitKey(0);     cvDestroyWindow(DISPLAY_WINDOW); } 程序会将人脸检测出来,显示在屏幕上,并存回原来的文件将其覆盖。 在window xp, devcpp  4。9。9.2 下编译通过。本文为互联网收集,请勿用作商业用途文档为个人收集整理,来源于网络 人脸识别Step2]对人脸图片作PCA降维 本帖被 admin 设置为精华(2007—08—03) 在上一篇文章中,我们将人脸从图片中检测了出来,并存回了同名的文件。下一步,就要进行识别了。 对 一幅图片而言,我们可以将它看成是一个二位数组,每个元素表达一个像素的特征,如颜色等。那么,对一幅我们处理好的大小为92*112的黑白单通道图片, 就需要一个长度为10304的浮点箱量来表示.这么高维度的数据,处理起来计算代价很大,因为它将所有像素一视同仁,效果也不一定好. 所以,我们对于人脸数据,采用了主成分分析 ( Principal Component Analysis , PCA ) 方法来做数据降维。那么,什么是PCA呢? 主 成分分析 ( Principal Component Analysis , PCA ) 是一种掌握事物主要矛盾的统计分析方法,它可以从多元事物中解析出主要影响因素,揭示事物的本质,简化复杂的问题。计算主成分的目的是将高维数据投影到较 低维空间。给定 n 个变量的 m 个观察值,形成一个 n ′ m 的数据矩阵, n 通常比较大。对于一个由多个变量描述的复杂事物,人们难以认识,那么是否可以抓住事物主要方面进行重点分析呢?如果事物的主要方面刚好体现在几个主要变量 上,我们只需要将这几个变量分离出来,进行详细分析.但是,在一般情况下,并不能直接找出这样的关键变量。这时我们可以用原有变量的线性组合来表示事物的 主要方面, PCA 就是这样一种分析方法。 PCA 的目标是寻找 r ( r<n )个新变量,使它们反映事物的主要特征,压缩原有数据矩阵的规模.每个新变量是原有变量的线性组合,体现原有变量的综合效果,具有一定的实际含义。这 r 个新变量称为“主成分",它们可以在很大程度上反映原来 n 个变量的影响,并且这些新变量是互不相关的,也是正交的.通过主成分分析,压缩数据空间,将多元数据的特征在低维空间里直观地表示出来。 Opencv 中,事先预置了PCA方法的函数,我们去调用即可。在下面给出的程序中,我将训练集人脸图片的PCA结果输出到TrainFace。txt,测试人脸图片 的PCA结果输出到TestFace.txt中,以备以后调用别的学习算法(如SVM等)使用。在程序中,直接使用欧式距离或余弦距离(需要在源代码中手 动更改编译)度量测试人脸和各训练人脸的相似度并输出,以供作简单的观察。 以下是源程序: // eigenface.c, by Robin Hewitt, 2007 // edited by Xin QIao, AUG 2007 // // Example program showing how to implement eigenface with OpenCV // Usage: // // First, you need some face images。 I used the ORL face database. // You can download it for free at //    www.cl.cam。ac.uk/research/dtg/attarchive/facedatabase。html // // List the training and test face images you want to use in the // input files train.txt and test。txt。 (Example input files are provided // in the download.) To use these input files exactly as provided, unzip // the ORL face database, and place train。txt, test。txt, and eigenface.exe // at the root of the unzipped database. // // To run the learning phase of eigenface, enter //    eigenface train // at the command prompt. To run the recognition phase, enter //    eigenface test #include 〈stdio.h〉 #include 〈string.h〉 #include <math。h> #include ”cv.h” #include ”cvaux.h” #include ”highgui。h" //// Global variables IplImage ** faceImgArr        = 0; // array of face images CvMat    *  personNumTruthMat = 0; // array of person numbers int nTrainFaces              = 0; // the number of training images int nEigens                  = 0; // the number of eigenvalues IplImage * pAvgTrainImg      = 0; // the average image IplImage ** eigenVectArr      = 0; // eigenvectors CvMat * eigenValMat          = 0; // eigenvalues CvMat * projectedTrainFaceMat = 0; // projected training faces //// Function prototypes void learn(); void recognize(); void doPCA(); void storeTrainingData(); int  loadTrainingData(CvMat ** pTrainPersonNumMat); double * findNearestNeighbor(float * projectedTestFace); int  loadFaceImgArray(char * filename); void printUsage(); ////////////////////////////////// // main() // int main( int argc, char** argv ) {     // validate that an input was specified     if( argc != 2 )     {         printUsage();         return -1;     }     if( !strcmp(argv[1], "train”) ) learn();     else if( !strcmp(argv[1], ”test") ) recognize();     else     {         printf(”Unknown command: %s\n", argv[1]);         printUsage();     }     return 0; } ////////////////////////////////// // learn() // void learn() {     int i, j, k, offset;     // load training data     nTrainFaces = loadFaceImgArray(”train.txt");     if( nTrainFaces 〈 2 )     {         fprintf(stderr,                "Need 2 or more training faces\n"                ”Input file contains only %d\n", nTrainFaces);         return;     }     // do PCA on the training faces     doPCA();     // project the training images onto the PCA subspace     projectedTrainFaceMat = cvCreateMat( nTrainFaces, nEigens, CV_32FC1 );     offset = projectedTrainFaceMat-〉step / sizeof(float);     for(i=0; i〈nTrainFaces; i++)     {         //int offset = i * nEigens;         cvEigenDecomposite(             faceImgArr,             nEigens,             eigenVectArr,             0, 0,             pAvgTrainImg,             //projectedTrainFaceMat—〉data.fl + i*nEigens);             projectedTrainFaceMat->data.fl + i*offset);     }         // store the projectedTrainFaceMat as TrainFace。txt     FILE * TrainfaceFile = 0;     if( TrainfaceFile = fopen(”TrainFace。txt”, ”w”) )     {         for(j = 0 ; j 〈 nTrainFaces ; j++){               fprintf(TrainfaceFile,"%d    ", j);               for(k = 0; k 〈 nEigens ; k++){                   fprintf(TrainfaceFile, ” %d : %f ", k, (projectedTrainFaceMat->data.fl + j*offset)[k] );                }               fprintf(TrainfaceFile,” -1 : ? \n”);         }     }     // store the recognition data as an xml file     storeTrainingData(); } ////////////////////////////////// // recognize() // void recognize() {     int i, j, nTestFaces  = 0;        // the number of test images     CvMat * trainPersonNumMat = 0;  // the person numbers during training     float * projectedTestFace = 0;     // load test images and ground truth for person number     nTestFaces = loadFaceImgArray(”test.txt”);     printf(”%d test faces loaded\n", nTestFaces);     // load the saved training data     if( !loadTrainingData( &trainPersonNumMat ) ) return;     // project the test images onto the PCA subspace     projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) );         double * sim = (double *)cvAlloc( nEigens*sizeof(double) );;     for(i=0; i〈nTestFaces; i++)     {         int iNearest, k;         //int star[nTrainFaces];         // project the test image onto the PCA subspace                 cvEigenDecomposite(             faceImgArr,             nEigens,             eigenVectArr,             0, 0,             pAvgTrainImg,             projectedTestFace);                     // store the projectedTestFace as TestFace。txt         FILE * TestfaceFile = 0;         if( TestfaceFile = fopen(”TestFace。txt", ”w”) )        {                     fprintf(TestfaceFile,"%d    ", 0);               for(k = 0; k < nEigens ; k++){                   fprintf(TestfaceFile, ” %d : %f ”, k, projectedTestFace[k] );                }               fprintf(TestfaceFile,” —1 : ? \n");         }         sim = findNearestNeighbor(projectedTestFace);         //truth    = personNumTruthMat-〉data.i;         //nearest  = trainPersonNumMat->data。i[iNearest]; /*         //sort         int order[nTrainFaces];         order[0] = iNearest;         double temp[nTrainFaces];         for ( j = 0; j <  nTrainFaces; j++){             temp[j] = sim[j];         }                             for ( k = 0; k 〈  nTrainFaces; k++){             //记录temp中最小元素的index             int result=0;                         double leastSim = DBL_MAX;                         //找到temp中最小元素的index,记录入result             for ( j = 0; j <  nTrainFaces; j++){                                  if( temp[j] 〈 leastSim ){                     leastSim = temp[j];                    result = j;                 }                            }                                     order = result;                         //将temp中找到的最小元素置为无穷大             temp[result] = DBL_MAX;         //}              */                        printf("%d test image : \n”,i+1);                 for ( k = 0; k <  nTrainFaces; k++)             printf(”star : %d, simility : %f\n”, k, sim[ k ]);         //printf("nearest = %d, Truth = %d\n\n", nearest, truth);         } } ////////////////////////////////// // loadTrainingData() // int loadTrainingData(CvMat ** pTrainPersonNumMat) {     CvFileStorage * fileStorage;     int i;     // create a file-storage interface     fileStorage = cvOpenFileStorage( "。/data/facedata。xml", 0, CV_STORAGE_READ );     if( !fileStorage )     {         fprintf(stderr, ”Can’t open facedata.xml\n”);         return 0;     }     nEigens = cvReadIntByName(fileStorage, 0, "nEigens", 0);     nTrainFaces = cvReadIntByName(fileStorage, 0, ”nTrainFaces", 0);     *pTrainPersonNumMat = (CvMat *)cvReadByName(fileStorage, 0, ”trainPersonNumMat", 0);     eigenValMat  = (CvMat *)cvReadByName(fileStorage, 0, ”eigenValMat”, 0);     projectedTrainFaceMat = (CvMat *)cvReadByName(fileStorage, 0, "projectedTrainFaceMat", 0);     pAvgTrainImg = (IplImage *)cvReadByName(fileStorage, 0, ”avgTrainImg”, 0);     eigenVectArr = (IplImage **)cvAlloc(nTrainFaces*sizeof(IplImage *));     for(i=0; i〈nEigens; i++)     {         char varname[200];         sprintf( varname, "eigenVect_%d”, i );         eigenVectArr = (IplImage *)cvReadByName(fileStorage, 0, varname, 0);     }     // release the file-storage interface     cvReleaseFileStorage( &fileStorage );     return 1; } ////////////////////////////////// // storeTrainingData() // void storeTrainingData() {     CvFileStorage * fileStorage;     int i;     // create a file—storage interface     fileStorage = cvOpenFileStorage( ”./data/facedata.xml", 0, CV_STORAGE_WRITE );     // store all the data     cvWriteInt( fileStorage, "nEigens", nEigens );     cvWriteInt( fileStorage, ”nTrainFaces", nTrainFaces );     cvWrite(fileStorage, "trainPersonNumMat", personNumTruthMat, cvAttrList(0,0));     cvWrite(fileStorage, "eigenValMat”, eigenValMat, cvAttrList(0,0));     cvWrite(fileStorage, "projectedTrainFaceMat”, projectedTrainFaceMat, cvAttrList(0,0));     cvWrite(fileStorage, "avgTrainImg", pAvgTrainImg, cvAttrList(0,0));     for(i=0; i〈nEigens; i++)     {         char varname[200];         sprintf( varname, "eigenVect_%d", i );         cvWrite(fileStorage, varname, eigenVectArr, cvAttrList(0,0));     }     // release the file-storage interface     cvReleaseFileStorage( &fileStorage ); } ////////////////////////////////// // findNearestNeighbor() // double * findNearestNeighbor(float * projectedTestFace) {     //double leastDistSq = 1e12;     double leastDistSq = DBL_MAX;     int i, iTrain, iNearest = 0;     double * sim = (double *)malloc(nTrainFaces * sizeof(double));     for(iTrain=0; iTrain〈nTrainFaces; iTrain++)     {         double distSq=0;                 long double distSx=0;         long double distSy=0;         for(i=0; i〈nEigens; i++)         {                           float d_i =                 projectedTestFace -                 projectedTrainFaceMat—>data.fl[iTrain*nEigens + i];             //distSq += d_i*d_i / eigenValMat—>data.fl;  // Mahalanobis             distSq += d_i*d_i; // Euclidean                         /*             /////////////////////////////////////////////////////////////////              double d_i =                 projectedTestFace *                 projectedTr
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服