matlab中fisherface算法,opencv学习之路(40)、人脸识别算法——EigenFace、FisherFace、LBPH(示例代码)...

一、人脸识别算法之特征脸方法(Eigenface)

1、原理介绍及数据收集

特征脸方法主要是基于PCA降维实现。

详细介绍和主要思想可以参考

http://blog.csdn.net/u010006643/article/details/46417127

上述博客的人脸数据库打不开了,大家可以去下面这个博客下载ORL人脸数据库

http://blog.csdn.net/xdzzju/article/details/50445160

下载后,ORL人脸数据库有40个人,每人10张照片。

20171226222802441649.png

2、流程

20171226222802733652.png

20171226222802814710.png

3、相关图示

20171226222803055930.png

20171226222803127222.png

20171226222803233671.png

20171226222803475868.png

4、代码

1 #include

2 #include

3

4 using namespacecv;5 using namespacecv::face;6 using namespacestd;7

8 //对原图归一化

9 Mat normal(Mat src, Mat dst) {10 if (src.channels() == 1)//若原图单通道

11 normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC1);12 else //否则,原图三通道

13 normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC3);14 returndst;15 }16

17 voidmain() {18 //读取文件,转换为数据流

19 string filename = string("at.txt");20 ifstream file(filename.c_str(), ifstream::in);21 if (!file)22 cout << "error" <

24 stringline, path, classlabel;25 vectorimage;26 vectorlabels;27 char separator = ‘;‘;28 while(getline(file,line))29 {30 stringstream liness(line);31 getline(liness, path, separator);32 getline(liness, classlabel);33 if (!path.empty()&&!classlabel.empty())34 {35 //cout << "path:" << path<< endl;

36 image.push_back(imread(path, 0));37 labels.push_back(atoi(classlabel.c_str()));38 }39 }40

41 if (image.size() < 1 || labels.size() < 1)42 cout << "invalid image path..." <

44 int height = image[0].rows;45 int width = image[0].cols;46 //cout << "height:" << height << ",width:" << width<

48 //最后一个人为测试样本

49 Mat testSample = image[image.size() - 1];50 int testLabel = labels[labels.size() - 1];51 image.pop_back();52 labels.pop_back();53

54 //训练

55 Ptrmodel =createEigenFaceRecognizer();56 model->train(image, labels);57

58 //识别

59 int predictLabel = model->predict(testSample);60 cout << "actual label:" << testLabel << ",predict label:" << predictLabel <

62 //获得特征值,特征向量,均值 平均脸

63 Mat eigenvalues = model->getEigenValues();64 Mat eigenvectors = model->getEigenVectors();65 Mat mean = model->getMean();66 Mat meanFace = mean.reshape(1,height);67 Mat dst;68 dst=normal(meanFace,dst);69 imshow("Mean Face", dst);70

71 //特征脸

72 for (int i = 0; i < min(10,eigenvectors.cols); i++)73 {74 Mat ev =eigenvectors.col(i).clone();75 Mat eigenFace = ev.reshape(1, height);76 Mat grayscale;77 grayscale =normal(eigenFace, grayscale);78 Mat colorface;79 applyColorMap(grayscale, colorface, COLORMAP_BONE);80 char* winTitle = new char[128];81 sprintf(winTitle, "eigenface_%d", i);82 imshow(winTitle, colorface);83 }84

85 //重建人脸

86 for (int num = min(10, eigenvectors.cols); num < min(300, eigenvectors.cols); num+=15)87 {88 Mat evs = Mat(eigenvectors, Range::all(), Range(0, num));89 Mat projection = LDA::subspaceProject(evs, mean, image[0].reshape(1, 1));90 Mat reconstruction=LDA::subspaceReconstruct(evs, mean, projection);91

92 Mat result = reconstruction.reshape(1, height);93 reconstruction =normal(result, reconstruction);94 char* winTitle = new char[128];95 sprintf(winTitle, "recon_face_%d", num);96 imshow(winTitle, reconstruction);97 }98

99 waitKey(0);100 }

20171226222803731737.png

二、FisherFace(LDA线性判别分析)

1、理论介绍

http://blog.csdn.net/feirose/article/details/39552997

20171226222803893853.png

2、流程

20171226222803971981.png

3、PCA和LDA的对比

20171226222804081360.png

4、代码(与特征脸代码几乎一致)

此处只列出修改部分

55行模型训练 Ptrmodel = createFisherFaceRecognizer();

72行显示特征脸 for (int i = 0; i < min(16,eigenvectors.cols); i++)

Mat ev = eigenvectors.col(i).clone();

86行重建人脸 for (int num = 0; num < min(16, eigenvectors.cols); num++)

三、LBPH

1、原理介绍

大家可以参考http://blog.csdn.net/xiaomaishiwoa/article/details/46640377

20171226222804164371.png

二、流程

20171226222804422193.png

3、代码

#include #include

using namespacecv;using namespacecv::face;using namespacestd;//对原图归一化

Mat normal(Mat src, Mat dst) {if (src.channels() == 1)//若原图单通道

normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC1);else //否则,原图三通道

normalize(src, dst, 0, 255, NORM_MINMAX, CV_8UC3);returndst;

}voidmain() {//读取文件,转换为数据流

string filename = string("at.txt");

ifstream file(filename.c_str(), ifstream::in);if (!file)

cout<< "error" <

vectorimage;

vectorlabels;char separator = ‘;‘;while(getline(file,line))

{

stringstream liness(line);

getline(liness, path, separator);

getline(liness, classlabel);if (!path.empty()&&!classlabel.empty())

{//cout << "path:" << path<< endl;

image.push_back(imread(path, 0));

labels.push_back(atoi(classlabel.c_str()));

}

}if (image.size() < 1 || labels.size() < 1)

cout<< "invalid image path..." <

Mat testSample = image[image.size() - 1];int testLabel = labels[labels.size() - 1];

image.pop_back();

labels.pop_back();//训练

Ptrmodel =createLBPHFaceRecognizer();

model->train(image, labels);//识别

int predictLabel = model->predict(testSample);

cout<< "actual label:" << testLabel << ",predict label:" << predictLabel <

int radius = model->getRadius(); //中心像素点到周围像素点的距离

int neibs = model->getNeighbors(); //周围像素点的个数

int grad_x = model->getGridX(); //将一张图片在x方向分成几块

int grad_y = model->getGridY(); //将一张图片在y方向分成几块

double t = model->getThreshold(); //相似度阈值

cout << "radius:" << radius <

cout<< "neibs:" << neibs <

cout<< "grad_x:" << grad_x <

cout<< "grad_y:" << grad_y <

cout<< "threshold:" << t<

waitKey(0);

}

20171226222804589192.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值