人脸识别源代码Open cv
#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的更多相关文章
- OpenCV图像处理以及人脸识别
OpenCV基础 OpenCV是一个开源的计算机视觉库.提供了很多图像处理常用的工具 批注:本文所有图片数据都在我的GitHub仓库 读取图片并显示 import numpy as np import ...
- 可学习的多人人脸识别程序(基于Emgu CV)
源代码下载(需要安装Emgu CV,安装方法请百度) 很多朋友使用Emgu CV遇到CvInvoke()的报错,我找到一种解决方法. 把EmguCV目录下bin里面的所有dll复制到C:\WINDOW ...
- 基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别
背景 目前AI 处于风口浪尖,作为 公司的CTO,也作为自己的技术专研,开始了AI之旅,在朋友圈中也咨询 一些大牛对于AI 机器学习框架的看法,目前自己的研究方向主要开源的 AI 库,如:Emgu C ...
- C# net Emgu.CV.World 人脸识别 根据照片将人脸抠图出来。
Emgu.CV.World 人脸识别 根据照片将人脸抠图出来.效果如下: 应用范围:配合摄像头,抓取的图像,抠出人脸照片,这样人脸照片的大小会很小,传输速度快.这样识别速度也就快. 目前我正在做百度人 ...
- Visual C++ 经典的人脸识别算法源代码
说明:VC++ 经典的人脸识别算法实例,提供人脸五官定位具体算法及两种实现流程. 点击下载
- 吴裕雄--天生自然python学习笔记:python 用 Open CV通过人脸识别进行登录
人脸识别登录功能的基本原理是通过对比两张图片的差异度来判断两张图片是 否是同 一人的面部 . 对比图片 差异度 的算法有很多种,本例中使用“颜色直方图” 算法来实现对人脸图像的识别. 下面为比较 im ...
- 吴裕雄--天生自然python学习笔记:python 用 Open CV 进行人脸识别
要对特定图像进行识别,最关键的是要有识别对象的特征文件, OpenCV 己内置 了人脸识别特征文件,我们只需使用 OpenCV 的 CascadeClassifier 类即可进行识别 . 创建 Cas ...
- OpenCV人脸识别LBPH算法源码分析
1 背景及理论基础 人脸识别是指将一个需要识别的人脸和人脸库中的某个人脸对应起来(类似于指纹识别),目的是完成识别功能,该术语需要和人脸检测进行区分,人脸检测是在一张图片中把人脸定位出来,完成的是搜寻 ...
- 关于opencv中人脸识别主函数的部分注释详解。
近段时间在搞opencv的视频人脸识别,无奈自带的分类器的准确度,实在是不怎么样,但又能怎样呢?自己又研究不清楚各大类检测算法. 正所谓,功能是由函数完成的,于是自己便看cvHaarDetectObj ...
随机推荐
- [Xcode 实际操作]九、实用进阶-(24)使用Segue(页面的跳转连接)进行页面跳转并传递参数
目录:[Swift]Xcode实际操作 本文将演示使用Segue(页面的跳转连接)进行页面跳转并传递参数. 参照结合:[Xcode10 实际操作]九.实用进阶-(23)多个Storyboard故事板中 ...
- PyInstaller 库
将.py 源代码转换成无需源代码的可执行文件 首先,PyInstaller是第三方库,需要下载额外安装(安装第三方库需要使用pip工具) 步骤: 1.用管理员运行cmd命令行 "pip in ...
- PostgreSQL-5-条件过滤
基本语法 SELECT column1, column2, columnN FROM table_name WHERE [search_condition] 操作符 =等于:<>不等于:! ...
- java数据结构----链表
1.链表:链表是继数组之后第二种使用的最广泛的通用存储结构,它克服了数组的许多弊端:无序数组的查找慢问题,有序数组的插入慢问题,数组定义时的定长问题.它也可取代数组,作为其他数据结构的基础. 2.引用 ...
- redis配置配置文件
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...
- Codeforces Round #390 (Div. 2) B
Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. ...
- [转][android][利用JNI技术在Android中调用、调试C++代码]
在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在Android中会生成Linux系统下的.so文件(好吧,其实我基本没用过Linux). 没写过 ...
- GIT GUI克隆github代码
新建一个文件夹,右击gitgui git clone 去掉不要
- tomcat启动时自动运行代码
原文链接:http://jingpin.jikexueyuan.com/article/49660.html 作者: 一直向北 发布时间:2015-07-13 11:12:13 方法1:tomcat ...
- 盒子模型--IE与标准
从上图可以看到标准 W3C 盒子模型的范围包括 margin.border.padding.content,并且 content 部分不包含其他部分. 从上图可以看到 IE 盒子模型的范围也包括 ma ...