#include <stdio.h>
#include <string.h>
#include "cv.h"
#include "cvaux.h"
#include "highgui.h" using namespace cv; //globle variables
int nTrainFaces = ; // number of trainning images
int nEigens = ; // number of eigenvalues
IplImage** faceImgArr = ; // array of face images
CvMat* personNumTruthMat = ; // array of person numbers
IplImage* pAvgTrainImg = ; // the average image
IplImage** eigenVectArr = ; // eigenvectors
CvMat* eigenValMat = ; // eigenvalues
CvMat* projectedTrainFaceMat = ; // projected training faces //// Function prototypes
void learn();
void recognize();
void doPCA();
void storeTrainingData();
int loadTrainingData(CvMat** pTrainPersonNumMat);
int findNearestNeighbor(float* projectedTestFace);
int loadFaceImgArray(char* filename);
void printUsage(); int main( int argc, char** argv )
{
if((argc != ) && (argc != )){
printUsage();
return -;
} if( !strcmp(argv[], "train" )){
learn();
} else if( !strcmp(argv[], "test") ){
recognize();
} else {
printf("Unknown command: %s\n", argv[]);
}
return ;
} void printUsage(){
printf("Usage: eigenface <command>\n",
" Valid commands are\n"
" train\n"
" test\n"
);
} void learn(){
int i; // load training data
nTrainFaces = loadFaceImgArray("train.txt");
if( nTrainFaces < ){
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);
for(i = ; i < nTrainFaces; i ++){
cvEigenDecomposite(
faceImgArr[i],
nEigens,
eigenVectArr,
, ,
pAvgTrainImg,
projectedTrainFaceMat->data.fl + i*nEigens
);
} // store the recognition data as an xml file
storeTrainingData();
} int loadFaceImgArray(char* filename){
FILE* imgListFile = ;
char imgFilename[];
int iFace, nFaces = ; // open the input file
imgListFile = fopen(filename, "r"); // count the number of faces
while( fgets(imgFilename, , imgListFile) ) ++ nFaces;
rewind(imgListFile); // allocate the face-image array and person number matrix
faceImgArr = (IplImage **)cvAlloc( nFaces*sizeof(IplImage *) );
personNumTruthMat = cvCreateMat( , nFaces, CV_32SC1 ); // store the face images in an array
for(iFace=; iFace<nFaces; iFace++){
//read person number and name of image file
fscanf(imgListFile, "%d %s", personNumTruthMat->data.i+iFace, imgFilename); // load the face image
faceImgArr[iFace] = cvLoadImage(imgFilename, CV_LOAD_IMAGE_GRAYSCALE);
} fclose(imgListFile); return nFaces;
} void doPCA(){
int i;
CvTermCriteria calcLimit;
CvSize faceImgSize; // set the number of eigenvalues to use
nEigens = nTrainFaces - ; // allocate the eigenvector images
faceImgSize.width = faceImgArr[]->width;
faceImgSize.height = faceImgArr[]->height;
eigenVectArr = (IplImage**)cvAlloc(sizeof(IplImage*) * nEigens);
for(i=; i<nEigens; i++){
eigenVectArr[i] = cvCreateImage(faceImgSize, IPL_DEPTH_32F, );
} // allocate the eigenvalue array
eigenValMat = cvCreateMat( , nEigens, CV_32FC1 ); // allocate the averaged image
pAvgTrainImg = cvCreateImage(faceImgSize, IPL_DEPTH_32F, ); // set the PCA termination criterion
calcLimit = cvTermCriteria( CV_TERMCRIT_ITER, nEigens, ); // compute average image, eigenvalues, and eigenvectors
cvCalcEigenObjects(
nTrainFaces,
(void*)faceImgArr,
(void*)eigenVectArr,
CV_EIGOBJ_NO_CALLBACK,
,
,
&calcLimit,
pAvgTrainImg,
eigenValMat->data.fl
);
} void storeTrainingData(){
CvFileStorage* fileStorage;
int i; // create a file-storage interface
fileStorage = cvOpenFileStorage( "facedata.xml", , CV_STORAGE_WRITE); // store all the data
cvWriteInt( fileStorage, "nEigens", nEigens);
cvWriteInt( fileStorage, "nTrainFaces", nTrainFaces );
cvWrite(fileStorage, "trainPersonNumMat", personNumTruthMat, cvAttrList(, ));
cvWrite(fileStorage, "eigenValMat", eigenValMat, cvAttrList(,));
cvWrite(fileStorage, "projectedTrainFaceMat", projectedTrainFaceMat, cvAttrList(,));
cvWrite(fileStorage, "avgTrainImg", pAvgTrainImg, cvAttrList(,)); for(i=; i<nEigens; i++){
char varname[];
sprintf( varname, "eigenVect_%d", i);
cvWrite(fileStorage, varname, eigenVectArr[i], cvAttrList(,));
} //release the file-storage interface
cvReleaseFileStorage( &fileStorage );
} void recognize(){
int i, nTestFaces = ; // the number of test images
CvMat* trainPersonNumMat = ; // the person numbers during training
float* projectedTestFace = ; // 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) );
for(i=; i<nTestFaces; i++){
int iNearest, nearest, truth; // project the test image onto PCA subspace
cvEigenDecomposite(
faceImgArr[i],
nEigens,
eigenVectArr,
, ,
pAvgTrainImg,
projectedTestFace
); iNearest = findNearestNeighbor(projectedTestFace);
truth = personNumTruthMat->data.i[i];
nearest = trainPersonNumMat->data.i[iNearest]; printf("nearest = %d, Truth = %d\n", nearest, truth);
}
} int loadTrainingData(CvMat** pTrainPersonNumMat){
CvFileStorage* fileStorage;
int i; // create a file-storage interface
fileStorage = cvOpenFileStorage( "facedata.xml", , CV_STORAGE_READ );
if( !fileStorage ){
fprintf(stderr, "Can't open facedata.xml\n");
return ;
} nEigens = cvReadIntByName(fileStorage, , "nEigens", );
nTrainFaces = cvReadIntByName(fileStorage, , "nTrainFaces", );
*pTrainPersonNumMat = (CvMat*)cvReadByName(fileStorage, , "trainPersonNumMat", );
eigenValMat = (CvMat*)cvReadByName(fileStorage, , "eigenValMat", );
projectedTrainFaceMat = (CvMat*)cvReadByName(fileStorage, , "projectedTrainFaceMat", );
pAvgTrainImg = (IplImage*)cvReadByName(fileStorage, , "avgTrainImg", );
eigenVectArr = (IplImage**)cvAlloc(nTrainFaces*sizeof(IplImage*));
for(i=; i<nEigens; i++){
char varname[];
sprintf( varname, "eigenVect_%d", i );
eigenVectArr[i] = (IplImage*)cvReadByName(fileStorage, , varname, );
} // release the file-storage interface
cvReleaseFileStorage( &fileStorage ); return ;
} int findNearestNeighbor(float* projectedTestFace){
double leastDistSq = DBL_MAX;
int i, iTrain, iNearest = ; for(iTrain=; iTrain<nTrainFaces; iTrain++){
double distSq = ; for(i=; i<nEigens; i++){
float d_i = projectedTestFace[i] -
projectedTrainFaceMat->data.fl[iTrain*nEigens + i];
distSq += d_i*d_i;
} if(distSq < leastDistSq){
leastDistSq = distSq;
iNearest = iTrain;
}
} return iNearest;
}

人脸识别源代码Open cv的更多相关文章

  1. OpenCV图像处理以及人脸识别

    OpenCV基础 OpenCV是一个开源的计算机视觉库.提供了很多图像处理常用的工具 批注:本文所有图片数据都在我的GitHub仓库 读取图片并显示 import numpy as np import ...

  2. 可学习的多人人脸识别程序(基于Emgu CV)

    源代码下载(需要安装Emgu CV,安装方法请百度) 很多朋友使用Emgu CV遇到CvInvoke()的报错,我找到一种解决方法. 把EmguCV目录下bin里面的所有dll复制到C:\WINDOW ...

  3. 基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别

    背景 目前AI 处于风口浪尖,作为 公司的CTO,也作为自己的技术专研,开始了AI之旅,在朋友圈中也咨询 一些大牛对于AI 机器学习框架的看法,目前自己的研究方向主要开源的 AI 库,如:Emgu C ...

  4. C# net Emgu.CV.World 人脸识别 根据照片将人脸抠图出来。

    Emgu.CV.World 人脸识别 根据照片将人脸抠图出来.效果如下: 应用范围:配合摄像头,抓取的图像,抠出人脸照片,这样人脸照片的大小会很小,传输速度快.这样识别速度也就快. 目前我正在做百度人 ...

  5. Visual C++ 经典的人脸识别算法源代码

    说明:VC++ 经典的人脸识别算法实例,提供人脸五官定位具体算法及两种实现流程. 点击下载

  6. 吴裕雄--天生自然python学习笔记:python 用 Open CV通过人脸识别进行登录

    人脸识别登录功能的基本原理是通过对比两张图片的差异度来判断两张图片是 否是同 一人的面部 . 对比图片 差异度 的算法有很多种,本例中使用“颜色直方图” 算法来实现对人脸图像的识别. 下面为比较 im ...

  7. 吴裕雄--天生自然python学习笔记:python 用 Open CV 进行人脸识别

    要对特定图像进行识别,最关键的是要有识别对象的特征文件, OpenCV 己内置 了人脸识别特征文件,我们只需使用 OpenCV 的 CascadeClassifier 类即可进行识别 . 创建 Cas ...

  8. OpenCV人脸识别LBPH算法源码分析

    1 背景及理论基础 人脸识别是指将一个需要识别的人脸和人脸库中的某个人脸对应起来(类似于指纹识别),目的是完成识别功能,该术语需要和人脸检测进行区分,人脸检测是在一张图片中把人脸定位出来,完成的是搜寻 ...

  9. 关于opencv中人脸识别主函数的部分注释详解。

    近段时间在搞opencv的视频人脸识别,无奈自带的分类器的准确度,实在是不怎么样,但又能怎样呢?自己又研究不清楚各大类检测算法. 正所谓,功能是由函数完成的,于是自己便看cvHaarDetectObj ...

随机推荐

  1. Unity3d与3dmax模型比例问题

    1 把3dmax中1米的物体,在unity中为1厘米,所以unity中需要放大100倍才能跟3dmax中效果相同 2 unity中调整模型->inspector-scale factor可以调整 ...

  2. iOS中UIWebView使用JS交互

    iOS中偶尔也会用到webview来显示一些内容,比如新闻,或者一段介绍.但是用的不多,现在来教大家怎么使用js跟webview进行交互. 这里就拿点击图片获取图片路径为例: 1.测试页面html & ...

  3. assembly x86(nasm)选择排序

    有一个首地址为NUM的N字无序无符号整数数组,编制程序采用选择排序法使该数组中的数按照从小到大的次序排序输出. 选择排序: data segment message db 'This is a pro ...

  4. iOS开发 - 多线程实现方案之NSThread篇

    NSThread API //类方法:创建一个线程 + (void)detachNewThreadWithBlock:(void (^)(void))block API_AVAILABLE(macos ...

  5. java操作mongodb数据库实现新建数据库,新建集合,新建文档

    *首先明确一点,要通过java代码创建mongodb数据库实例,需要同时创建集合和文档. 代码实现: /* 建立与mongodb数据库的连接,可指定参数,如:MongoClient client = ...

  6. jmeter常用的beanshell脚本

    时间戳下载文件保存响应内容断言连接数据库解析jsonlist递归创建多级目录 常用内置变量调用cmd文件GUI小命令 时间戳import java.text.SimpleDateFormat;impo ...

  7. Redis安装(centOS7)

    1.安装gcc环境 sudo yum install gcc-c++ 2.安装wget sudo yum install wget 3.下载Redis源码包 wget http://download. ...

  8. 最耗资源的10条sql

    ----当前最耗资源的10个cpu select * from (select address,hash_value, round(cpu_time/1000000) cpu_time_s, roun ...

  9. 本机运行x程序出现:Can't open display 原因及其解决方法(貌似非永久)

    http://blog.sina.com.cn/s/blog_53db572501016ma7.html 这是因为Xserver默认情况下不允许别的用户的图形程序的图形显示在当前屏幕上. 如果需要别的 ...

  10. 单线程异步回调机制的缺陷与node的解决方案

    一.node单线程异步的缺陷: 单线程异步的优点自然不必多说,node之所以能够如此快的兴起,其单线程异步回调机制相比于传统同步执行编程语言的优势便是原因之一.然而,开发一个node程序,其缺陷也是不 ...