本文主要是介绍人体头像面部的二维主成分分析(2D PCA),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
刚开始写博客,如果有什么不对的地方,请大家帮忙指出,谢谢!
二维PCA介绍
在前一篇文章《PCA算法:从一组照片中获取特征脸(特征向量)》中,介绍了对人像进行一维PCA处理的过程及结果,并提取显示了特征脸。在后续应用中可以使用特征脸空间来表示人像,是数据从m*n(图片尺寸为m*n)的大小缩减到了p(p为选取的前p个特征脸)。再进行人脸识别、检测的时候只需要处理明显的特征,并且具有数据量大大减小,便于处理等好处。
PCA方法作为一种图像统计处理方法,平等地对待所有点,角度、光照、尺寸及表情的变化会导致识别率急剧下降。其次人脸在人脸空间的分布近似高斯分布,普通人脸靠近均值附近,难以识别。PCA具有好的表达能力,但是区分能力不足。其次,PCA将样本转化为一行,生成一个q行m*n列的矩阵(q为样本数),计算变得复杂。
近年来发展了很多对PCA的改进方法,2DPCA(2-dimensional principal component analysis)就是其中一种。
二维PCA基本思想
本节直接截取武汉理工大学齐兴敏硕士的论文《基于PCA的人脸识别技术的研究》(链接)的内容。
二维PCA实现过程
// PCA_2D.cpp : 定义控制台应用程序的入口点。
// by dhj555 572694157@qq.com
// ZJU University#include "stdafx.h"
#include <string>
#include <strstream>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;vector<Mat> loadImages();
double* matrix_mul(int* mat1, int m, int n, int* mat2, int k);
int* matrix_trans(int* mat, int m, int n);
double myDot(Mat mat1, Mat mat2);int _tmain(int argc, _TCHAR* argv[])
{//Mat mat = Mat(5, 5, CV_64FC1,0.0);//Mat lie = Mat(5, 1, CV_8UC1);//lie.at<uchar>(0) = 0;//lie.at<uchar>(1) = 1;//lie.at<uchar>(2) = 2;//lie.at<uchar>(3) = 3;//lie.at<uchar>(4) = 4;//mat.col(3) = lie;//Mat lie2 = mat.col(3);//cout << lie2.dot(lie);//cout << lie;//cout << mat;//1、定义变量int num_sample = 38; //样本数量int num_eigen = 15; //投影和重构使用的前num_eigen个特征向量int norm_row = 64, norm_col = 56; //样本图像的尺寸vector<Mat> imgs = loadImages(); //所有样本图像Mat mean_face = Mat(norm_row, norm_col, CV_8UC1); //平均脸vector<int> mean_face_total;mean_face_total.resize(norm_row * norm_col);//2、计算平均脸for (int n = 0; n < num_sample; n++){for (int i = 0; i < norm_row; i++){for (int j = 0; j < norm_col; j++){int index = i*norm_col + j;mean_face_total.at(index) += ((imgs.at(n))).at<uchar>(index);}}}for (int j = 0; j < norm_row * norm_col; j++){mean_face.at<uchar>(j) = (uchar)(mean_face_total.at(j) / num_sample);}//3、计算协方差矩阵Mat covar_matrix = Mat(norm_col, norm_col, CV_64FC1, 0.0);for (int n = 0; n < num_sample; n++){Mat img = Mat(norm_row, norm_col, CV_64FC1);for (int i = 0; i < norm_row*norm_col; i++)img.at<double>(i) = ((double)imgs.at(n).at<uchar>(i)) - ((double)mean_face.at<uchar>(i));covar_matrix = covar_matrix + (img.t()*img) / num_sample;}//4、计算特征值和特征向量Mat eValuesMat; //特征值,从大大小排列Mat eVectorsMat; //特征向量,按行排列,按照对应特征值的大小eigen(covar_matrix, eValuesMat, eVectorsMat);//5、投影到特征向量空间,并重构for (int n = 0; n < num_sample; n++){Mat origin_img_uchar = imgs.at(n); //原图像Mat origin_img = Mat(norm_row, norm_col, CV_64FC1, 0.0);for (int index = 0; index < norm_row*norm_col; index++)origin_img.at<double>(index) = (double)origin_img_uchar.at<uchar>(index);Mat preject_mat = Mat(norm_row, num_eigen, CV_64FC1, 0.0); //投影矩阵for (int i = 0; i < num_eigen; i++){for (int p = 0; p < norm_row; p++){Mat row1 = origin_img.row(p);Mat row2 = eVectorsMat.row(i);double res = row1.dot(row2);preject_mat.at<double>(p*num_eigen + i) = res;}//preject_mat.col(i) = origin_img*(eVectorsMat.row(i).t());}Mat recons_mat = Mat(norm_row, norm_col, CV_64FC1, 0.0); //重构for (int j = 0; j < num_eigen; j++){recons_mat = recons_mat + (preject_mat.col(j))*(eVectorsMat.row(j));}float min = LLONG_MAX, max = LLONG_MIN, span = 0.0;for (int index = 0; index < norm_col*norm_row; index++){float d = recons_mat.at<double>(index);if (d>max)max = d;if (d < min)min = d;}span = max - min;Mat recon_face = Mat(norm_row, norm_col, CV_8UC1);for (int index = 0; index < norm_row*norm_col; index++){float d = recons_mat.at<double>(index);recon_face.at<uchar>(index) = (d - min) / span * 255.0;}Mat diff_face = Mat(norm_row, norm_col, CV_8UC1);//vector<float> diffs;//diffs.resize(norm_row*norm_col);for (int index = 0; index < norm_row*norm_col; index++){double origin_d = origin_img.at<double>(index);double recon_d = recons_mat.at<double>(index);//diffs.at(index) = origin_d - recon_d;diff_face.at<uchar>(index) = origin_d + 127 - recon_d;}char buffer[128];sprintf_s(buffer, "C:/Users/dhj555/Desktop/YelaFaces/PCA2D/1/1-000%dorgin.jpg", n);string orgin_ImgPath(buffer);sprintf_s(buffer, "C:/Users/dhj555/Desktop/YelaFaces/PCA2D/1/1-000%drecon.jpg", n);string recon_ImgPath(buffer);sprintf_s(buffer, "C:/Users/dhj555/Desktop/YelaFaces/PCA2D/1/1-000%ddiff.jpg", n);string diff_ImgPath(buffer);printf("%d st:\t%f\n", n, eValuesMat.at<double>(n));imwrite(orgin_ImgPath, origin_img);imwrite(recon_ImgPath, recons_mat);imwrite(diff_ImgPath, diff_face);}cout << "\n" << eVectorsMat;waitKey(0);return 0;
}vector<Mat> loadImages()
{vector<Mat> all_imgs;for (int i = 0; i < 38; i++){char buffer[128];sprintf_s(buffer, "C:/Users/dhj555/Desktop/YelaFaces/%d/%d-0001.jpg", i + 1, i + 1);string imgPath(buffer);Mat origin_img = imread(imgPath, CV_LOAD_IMAGE_GRAYSCALE);Mat img = Mat(64, 56, CV_8UC1);resize(origin_img, img, Size(56, 64));all_imgs.push_back(img);}return all_imgs;
}double myDot(Mat mat1, Mat mat2)
{double res = 0.0;if (mat1.cols == 1 && mat2.cols == 1 && mat1.rows == mat2.rows){for (int i = 0; i < mat1.rows; i++)res += mat1.at<double>(i)*mat2.at<double>(i);return res;}if (mat1.rows == 1 && mat2.rows == 1 && mat1.cols == mat2.cols){for (int i = 0; i < mat1.cols; i++)res += mat1.at<double>(i)*mat2.at<double>(i);return res;}return res;
}
二维PCA图片重构实验结果
此处列举5组实验结果。
注:
<span style="white-space:pre"> </span>double origin_d = origin_img.at<double>(index);double recon_d = recons_mat.at<double>(index);diff_face.at<uchar>(index) = origin_d + 127 - recon_d;
<span style="white-space:pre"> </span>由于使用了uchar表示像素灰度,直接相减可能出现负值,但是uchar不能表示复数,所以加上了127。
原始图像 | 重构图像 | 差异图像 |
---|---|---|
0 st: 2160549.032902
1 st: 452443.355672
2 st: 269018.469038
3 st: 165124.140552
4 st: 106610.785769
5 st: 89444.567562
6 st: 73015.552536
7 st: 63570.818226
8 st: 39627.906668
9 st: 38556.676027
10 st: 36000.282860
11 st: 33237.237388
12 st: 25777.377389
13 st: 25500.496538
14 st: 22803.806736
15 st: 21003.406909
16 st: 19098.320455
17 st: 17029.164552
18 st: 15863.203747
19 st: 13805.013111
20 st: 13136.374819
21 st: 12094.863309
22 st: 10604.307490
23 st: 9949.116257
24 st: 8720.810884
25 st: 8338.006774
26 st: 7937.054498
27 st: 7164.202648
28 st: 6632.291813
29 st: 6019.611097
30 st: 5137.939391
31 st: 4889.753865
32 st: 4727.662225
33 st: 4287.633124
34 st: 3985.294864
35 st: 3955.256511
36 st: 3638.502077
37 st: 3460.752888
这篇关于人体头像面部的二维主成分分析(2D PCA)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!