收藏 分销(赏)

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

上传人:精*** 文档编号:2631809 上传时间:2024-06-03 格式:DOC 页数:18 大小:764.04KB
下载 相关 举报
[人脸识别Step]在opencv下作人脸检测.doc_第1页
第1页 / 共18页
[人脸识别Step]在opencv下作人脸检测.doc_第2页
第2页 / 共18页
[人脸识别Step]在opencv下作人脸检测.doc_第3页
第3页 / 共18页
[人脸识别Step]在opencv下作人脸检测.doc_第4页
第4页 / 共18页
[人脸识别Step]在opencv下作人脸检测.doc_第5页
第5页 / 共18页
点击查看更多>>
资源描述

1、人脸识别Step在opencv下作人脸检测 作者: 日期:18 个人收集整理 勿做商业用途人脸识别Step1在opencv下作人脸检测本帖被 admin 设置为精华(2007-08-03) 人脸识别的第一步,就是人脸检测。把人的脸部从一张照片中用计算机自动识别出来,作为下一步人脸识别的基础。在opencv 中,库中自带了一个利用harr特征的人脸检测训练及检测函数:cvHaarDetectObjects.它利用训练好的检测器,在图片中间检测你想要的 物体,如人脸。opencv自带了很多检测器,在%opencv%data/haarcascades目录下,你可以随意取用。或者你也可以自己用图片训

2、练自己的检测器,之后拿来使用.下面是检测人脸的源代码:/ DetectFaces.c/ Example code showing how to detect faces using/ OpenCVs CvHaarClassifierCascade/ See also, facedetect。c, in the samples directory。/ Usage: DetectFaces imagefilename#include include cv.h#include highgui.h/ * Change this to your install location! */ *define

3、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

4、CvSeq * pFaceRectSeq; / memory-access interface / usage check if(argc 2) printf(”Missing name of image file!n” ”Usage: %s 1) ? cvLoadImage(argv1, CV_LOAD_IMAGE_COLOR) : 0; pStorage = cvCreateMemStorage(0); pCascade = (CvHaarClassifierCascade )cvLoad (OPENCV_ROOT”/data/haarcascades/haarcascade_fronta

5、lface_default。xml”), 0, 0, 0 ); / validate that everything initialized properly if( !pInpImg | !pStorage | !pCascade ) printf(Initialization failed: %sn, (!pInpImg)? cant load image file : (!pCascade)? cant load haarcascade - ” make sure path is correct : unable to allocate memory for data storage,

6、argv1); 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 de

7、tect = 40x40 / display detected faces displayDetections(pInpImg, pFaceRectSeq, argv1); / clean up and release resources cvReleaseImage(&pInpImg); if(pCascade) cvReleaseHaarClassifierCascade(pCascade); if(pStorage) cvReleaseMemStorage(&pStorage); return 0;void displayDetections(IplImage pInpImg, CvSe

8、q * 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)c

9、vGetSeqElem(pFaceRectSeq, i); CvPoint pt1 = r-x, r-y ; CvPoint pt2 = rx + r-width, ry + rheight ; cvRectangle(pInpImg, pt1, pt2, CV_RGB(0,255,0), 3, 4, 0); cvSetImageROI(pInpImg, *r); /char FileName = argv1; IplImage dst = cvCreateImage( cvSize(92,112), pInpImgdepth, pInpImgnChannels); cvResize(pInp

10、Img, 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对人脸图片

11、作PCA降维本帖被 admin 设置为精华(20070803) 在上一篇文章中,我们将人脸从图片中检测了出来,并存回了同名的文件。下一步,就要进行识别了。对 一幅图片而言,我们可以将它看成是一个二位数组,每个元素表达一个像素的特征,如颜色等。那么,对一幅我们处理好的大小为92112的黑白单通道图片, 就需要一个长度为10304的浮点箱量来表示.这么高维度的数据,处理起来计算代价很大,因为它将所有像素一视同仁,效果也不一定好.所以,我们对于人脸数据,采用了主成分分析 ( Principal Component Analysis , PCA ) 方法来做数据降维。那么,什么是PCA呢?主 成分分析

12、 ( Principal Component Analysis , PCA ) 是一种掌握事物主要矛盾的统计分析方法,它可以从多元事物中解析出主要影响因素,揭示事物的本质,简化复杂的问题。计算主成分的目的是将高维数据投影到较 低维空间。给定 n 个变量的 m 个观察值,形成一个 n m 的数据矩阵, n 通常比较大。对于一个由多个变量描述的复杂事物,人们难以认识,那么是否可以抓住事物主要方面进行重点分析呢?如果事物的主要方面刚好体现在几个主要变量 上,我们只需要将这几个变量分离出来,进行详细分析.但是,在一般情况下,并不能直接找出这样的关键变量。这时我们可以用原有变量的线性组合来表示事物的 主

13、要方面, PCA 就是这样一种分析方法。 PCA 的目标是寻找 r ( rn )个新变量,使它们反映事物的主要特征,压缩原有数据矩阵的规模.每个新变量是原有变量的线性组合,体现原有变量的综合效果,具有一定的实际含义。这 r 个新变量称为“主成分,它们可以在很大程度上反映原来 n 个变量的影响,并且这些新变量是互不相关的,也是正交的.通过主成分分析,压缩数据空间,将多元数据的特征在低维空间里直观地表示出来。Opencv 中,事先预置了PCA方法的函数,我们去调用即可。在下面给出的程序中,我将训练集人脸图片的PCA结果输出到TrainFace。txt,测试人脸图片 的PCA结果输出到TestFac

14、e.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 databas

15、e./ 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

16、/ 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.hinclude string.hinclude include

17、”cv.h”#include ”cvaux.h”include ”highgui。h/ Global variablesIplImage faceImgArr = 0; / array of face imagesCvMat * personNumTruthMat = 0; / array of person numbersint nTrainFaces = 0; / the number of training imagesint nEigens = 0; / the number of eigenvaluesIplImage * pAvgTrainImg = 0; / the averag

18、e imageIplImage * eigenVectArr = 0; / eigenvectorsCvMat * eigenValMat = 0; / eigenvaluesCvMat * projectedTrainFaceMat = 0; / projected training faces/ Function prototypesvoid learn();void recognize();void doPCA();void storeTrainingData();int loadTrainingData(CvMat pTrainPersonNumMat);double * findNe

19、arestNeighbor(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(argv1, train”) ) learn(); else if( !strcmp(argv1, ”test) ) recognize()

20、; else printf(”Unknown command: sn, argv1); 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 facesn ”Input file contains only dn, nTrainFaces); return; / do

21、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; inTrainFaces; i+) /int offset = i nEigens; cvEigenDecomposite( faceImgArr, nEige

22、ns, eigenVectArr, 0, 0, pAvgTrainImg, /projectedTrainFaceMatdata.fl + i*nEigens); projectedTrainFaceMat-data.fl + ioffset); / store the projectedTrainFaceMat as TrainFace。txt FILE TrainfaceFile = 0; if( TrainfaceFile = fopen(”TrainFace。txt”, ”w”) ) for(j = 0 ; j nTrainFaces ; j+) fprintf(TrainfaceFi

23、le,%d , j); for(k = 0; k nEigens ; k+) fprintf(TrainfaceFile, ” d : %f , k, (projectedTrainFaceMat-data.fl + joffset)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

24、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 loadedn, nTestFaces); / load the saved training data if( !loadTrainingData(

25、&trainPersonNumMat ) ) return; / project the test images onto the PCA subspace projectedTestFace = (float )cvAlloc( nEigenssizeof(float) ); double sim = (double )cvAlloc( nEigenssizeof(double) );; for(i=0; inTestFaces; i+) int iNearest, k; /int starnTrainFaces; / project the test image onto the PCA

26、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 data。iiNearest;/* /sort int ordernTr

27、ainFaces; order0 = iNearest; double tempnTrainFaces; for ( j = 0; j nTrainFaces; j+) tempj = simj; 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( tempj leastSim ) leastSim = tempj; result = j; order = result; /将temp中找到的最小元素置为无穷大 tempresult = DBL_MAX; / / printf(d test image : n”,i+1); for ( k = 0; k data.fliTrainnEigens + i; /distSq += d_i*d_i / eigenValMatdata.fl; / Mahalanobis distSq += d_id_i; / Euclidean /* / double d_i = projectedTestFace * projectedTr

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

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

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

关于我们      便捷服务       自信AI       AI导航        获赠5币

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

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

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

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服