#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. AtCoder Regular Contest 061 E - すぬけ君の地下鉄旅行【最短路】

    具体题解又要搬大哥的了,嘿嘿~ 请点击:G点我 这道题目的难点就是同一家公司的路直接走不需要再花费,然后多了一个公司这个东西,这个不像是边的副权值(瞎说的)之类的东西,这是对于路来说的,路的属性... ...

  2. C#异步调用的应用实践浅谈

    C#异步调用的应用实践最经公司工作需要调用一个外部的webservice,同时要将传出的数据进行保存,以自己以前的习惯,就打算逐步操作,失败啊,完全没考虑过用户体验效果,在同事指点下,意识到使用C#异 ...

  3. 异步编程(AsyncCallback委托,IAsyncResult接口,BeginInvoke方法,EndInvoke方法的使用小总结)

    http://www.cnblogs.com/panjun-Donet/archive/2009/03/03/1284700.html 让我们来看看同步异步的区别: 同步方法调用在程序继续执行之前需要 ...

  4. 常用的高级sql查询

    1.根据主键id数组批量修改 void updateByIdArr(Integer[] idArr); <update id="updateByIdArr" paramete ...

  5. Sublime Text 报“Pylinter could not automatically determined the path to lint.py

    Pylinter could not automatically determined the path to lint.py. please provide one in the settings ...

  6. 分布式通信-tcp/ip 广播

    服务端 /** * 广播 */ public class MulticastServer { public static void main(String[] args) { try { //地址是2 ...

  7. Zynq7000开发系列-6(QT开发环境搭建:Ubuntu、Zynq)

    操作系统:Ubuntu14.04.5 LTS 64bit Qt:Qt 5.4.2 (qt-opensource-linux-x64-5.4.2.run.qt-everywhere-opensource ...

  8. Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode.rpc-address is not configured.

    在搭建Hadoop集群的时候,遇到了这样的一个报错. 就在启动HDFS调用命令: start-dfs.sh 的时候,报错: 然后输密码就一直很奇怪,反正一直运行不成功. 百度了半天,确定是core-s ...

  9. 简单的鼠标经过特效-mouse事件

    <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  10. AJPFX关于子类父类中的构造

    1.子父类中的构造函数不存在重写,因为子父类的构造函数名字不一样(重写要求子父类的方法名字一模一样,包括参数列表)2.子类创建对象时会先运行父类的构造函数再运行子类的构造函数.因为每个子类的构造函数的 ...