PCA--主成分分析,主要用在降维上。
具体原理讲解的很多,在这里就不多说了。
具体可见:http://blog.csdn.net/xiaojidan2011/article/details/11595869
PCA主要计算步骤如下:
1、事先把每个样本归一化,把原始数据中每个样本用一个向量表示,然后把所有样本组合起来构成一个矩阵。
2、求该矩阵的协防差矩阵
3、求步骤2中得到的协方差矩阵的特征值和特征向量。
4、将求出的特征向量按照特征值的大小进行组合形成一个映射矩阵,并根据指定的PCA保留的特征个数取出映射矩阵的前n行或者前n列作为最终的映射矩阵。
5、用步骤4的映射矩阵对原始数据进行映射,达到数据降维的目的。
PCA的应用:
1、人脸识别
2、其他运算的预处理也叫数据的白化
opencv中PCA类的主要函数有:
C++: PCA::PCA(InputArray data, InputArray mean, int flags, int maxComponents=0)
//参数依次为:原始数据;原始数据均值,输入空会自己计算;每行/列代表一个样本;保留多少特征值,默认全保留
C++: PCA::PCA(InputArray data, InputArray mean, int flags, double retainedVariance)
//参数依次为:原始数据;原始数据均值,输入空会自己计算;每行/列代表一个样本;保留多少特征值,百分比,默认全保留
C++: PCA& PCA::computeVar(InputArray data, InputArray mean, int flags, double retainedVariance)
//参数依次为:原始数据;原始数据均值,输入空会自己计算;每行/列代表一个样本;保留多少特征值,百分比,默认全保留
C++: Mat PCA::project(InputArray vec) const
//原图像,投影到新的空间
C++: Mat PCA::backProject(InputArray vec) const
//先进行project之后的数据,反映摄到原始图像
变量值有:
mean--------原始数据的均值
eigenvalues--------协方差矩阵的特征值
eigenvectors--------特征向量
函数使用测试:
#pragma once#pragma execution_character_set("utf-8")
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
#define DIMENTIONS 7
#define SAMPLE_NUM 31
float Coordinates[DIMENTIONS*SAMPLE_NUM] = {
101.5, 100.4, 97.0, 98.7, 100.8, 114.2, 104.2
, 100.8, 93.5, 95.9, 100.7, 106.7, 104.3, 106.4
, 100.8, 97.4, 98.2, 98.2, 99.5, 103.6, 102.4
, 99.4, 96.0, 98.2, 97.8, 99.1, 98.3, 104.3
, 101.8, 97.7, 99.0, 98.1, 98.4, 102.0, 103.7
, 101.8, 96.8, 96.4, 92.7, 99.6, 101.3, 103.4
, 101.3, 98.2, 99.4, 103.7, 98.7, 101.4, 105.3
, 101.9, 100.0, 98.4, 96.9, 102.7, 100.3, 102.3
, 100.3, 98.9, 97.2, 97.4, 98.1, 102.1, 102.3
, 99.3, 97.7, 97.6, 101.1, 96.8, 110.1, 100.4
, 98.7, 98.4, 97.0, 99.6, 95.6, 107.2, 99.8
, 99.7, 97.7, 98.0, 99.3, 97.3, 104.1, 102.7
, 97.6, 96.5, 97.6, 102.5, 97.2, 100.6, 99.9
, 98.0, 98.4, 97.1, 100.5, 101.4, 103.0, 99.9
, 101.1, 98.6, 98.7, 102.4, 96.9, 108.2, 101.7
, 100.4, 98.6, 98.0, 100.7, 99.4, 102.4, 103.3
, 99.3, 96.9, 94.0, 98.1, 99.7, 109.7, 99.2
, 98.6, 97.4, 96.4, 99.8, 97.4, 102.1, 100.0
, 98.2, 98.2, 99.4, 99.3, 99.7, 101.5, 99.9
, 98.5, 96.3, 97.0, 97.7, 98.7, 112.6, 100.4
, 98.4, 99.2, 98.1, 100.2, 98.0, 98.2, 97.8
, 99.2, 97.4, 95.7, 98.9, 102.4, 114.8, 102.6
, 101.3, 97.9, 99.2, 98.8, 105.4, 111.9, 99.9
, 98.5, 97.8, 94.6, 102.4, 107.0, 115.0, 99.5
, 98.3, 96.3, 98.5, 106.2, 92.5, 98.6, 101.6
, 99.3, 101.1, 99.4, 100.1, 103.6, 98.7, 101.3
, 99.2, 97.3, 96.2, 99.7, 98.2, 112.6, 100.5
, 100.0, 99.9, 98.2, 98.3, 103.6, 123.2, 102.8
, 102.2, 99.4, 96.2, 98.6, 102.4, 115.3, 101.2
, 100.1, 98.7, 97.4, 99.8, 100.6, 112.4, 102.5
, 104.3, 98.7, 100.2, 116.1, 105.2, 101.6, 102.6
};
float Coordinates_test[DIMENTIONS] = {
104.3, 98.7, 100.2, 116.1, 105.2, 101.6, 102.6
};
int main()
{
Mat pcaSet(SAMPLE_NUM, DIMENTIONS, CV_32FC1); //原始数据
for (int i = 0; i < SAMPLE_NUM;i++)
{
for (int j = 0; j < DIMENTIONS;j++)
{
pcaSet.at<float>(i, j) = Coordinates[i*j + j];
}
}
PCA pca(pcaSet,Mat(),CV_PCA_DATA_AS_ROW);
cout << pca.mean;//均值
cout << endl;
cout << pca.eigenvalues << endl;//特征值
cout << endl;
cout << pca.eigenvectors << endl;//特征向量
Mat dst = pca.project(pcaSet);//映射新空间
cout << endl;
cout << dst;
Mat src = pca.backProject(dst);//反映射回来
cout << endl;
cout << src;
}
结果:
[101.5, 100.43227, 99.58065, 100.06452, 99.838699, 100.24837, 99.783859]
[24.541122;
14.777925;
8.6030035;
7.7478752;
4.9640956;
3.4982314;
0]
[0, 0.51984203, 0.23296018, -0.37617847, 0.40756124, -0.40061155, -0.4553985;
0, -0.70445395, 0.49155188, -0.16719522, 0.26280969, -0.37678754, 0.15208434;
0, -0.066723101, -0.038854279, -0.425872, 0.5511927, 0.70094371, 0.13242386;
0, 0.39764708, 0.065581232, -0.22947183, -0.03224976, -0.23389639, 0.85391527;
0, 0.22684877, 0.83399707, 0.15428388, -0.27935144, 0.38740331, -0.032665201;
0, 0.1395478, 0.05239287, 0.7567746, 0.6180082, -0.037830293, 0.14733768;
1, 0, 0, 0, 0, 0, 0]
[-7.3131528, -5.3501873, 11.577508, 0.60781765, 2.6220136, -0.45538509, 0;
-7.8768339, 1.1766117, -4.1587815, 1.7213521, 2.6745353, -0.5545125, 0;
4.830832, 6.3897724, 2.1345577, 0.84970546, 0.47657442, -0.3282631, 0;
-3.4450772, -5.4992504, -1.2498263, -3.3689275, -2.9782302, 1.5516136, 0;
10.281049, -7.7164068, 0.67565495, 5.676187, 2.5108516, 1.4743179, 0;
4.2984672, -0.75921839, -2.5882311, -0.25295436, 4.8868942, -1.3065162, 0;
0.21250246, 0.61282039, 2.5805261, 1.2471696, 1.0015231, 0.98909825, 0;
-6.5931559, 4.6479883, 2.1551886, 2.1832461, -2.4442921, -2.8696241, 0;
-6.8244157, 0.42434323, -0.6853919, -3.048038, 1.7272427, 0.63614368, 0;
2.6583216, 1.596774, 0.73432744, -0.89127231, 1.7983944, 0.043869421, 0;
0.095922641, -7.1871662, 0.26721755, 2.4715328, -0.39144242, 1.9969523, 0;
7.1780763, -1.2327303, 2.3370812, 0.11952236, -3.3782988, 1.0221726, 0;
3.7909391, -4.8472018, -3.902699, 1.3304355, 0.077061251, -1.6088341, 0;
2.0462558, 0.99781752, -1.7571733, -1.5092815, 1.4675957, 1.1244757, 0;
-6.6255755, -1.8157345, 2.6063576, -3.5488029, 2.5637681, 0.97726601, 0;
-5.361299, 0.40121809, -3.4525211, 0.87062407, -0.55830055, 2.7767134, 0;
7.4224248, 7.5462208, 3.3444295, -2.152308, -1.55211, 4.7031174, 0;
-2.6555865, -1.2249128, -1.6175778, 0.3482253, -1.8660933, -1.7037547, 0;
2.6387012, -6.742743, 0.57189912, -4.1314187, -4.5536275, -1.7193881, 0;
3.6488121, -0.1332843, -1.0593877, -0.31005722, 1.2057302, -1.7476019, 0;
-0.10034398, 0.81379104, -1.0097508, -1.4380376, 1.8176793, -2.2551713, 0;
-4.7259798, 1.3738803, -0.06910602, -2.330924, 0.57052439, -1.5393797, 0;
-7.0539317, 3.4640625, -0.56143302, 10.470398, -2.0381155, 0.97668087, 0;
4.358706, 6.8107924, 0.76822168, -0.92740154, 1.7716813, -0.58819091, 0;
-3.2411737, -0.38846514, -3.8849792, -2.7995861, 1.3991531, 4.4114451, 0;
0.40072519, 1.1867304, 1.8176504, -0.91143012, -3.7974873, -1.1423945, 0;
5.7739553, -0.76024073, -1.1735778, 1.2575091, -0.38342121, -1.1518155, 0;
3.0023592, 0.13134341, -0.85866189, 0.3392027, 0.012864275, -2.1181574, 0;
-2.9541163, 1.3588973, -2.0341418, 0.23292983, -3.0436382, 1.5964537, 0;
-0.98775464, 0.01095093, -2.2096908, -1.160388, -1.390853, -2.0177698, 0;
3.1199689, 4.7135777, 0.70306331, -0.94494265, -0.2082427, -1.1734859, 0]
[101.5, 100.4, 97, 98.699997, 100.8, 114.2, 104.2;
101.5, 97, 100.8, 104.2, 93.5, 100.7, 104.3;
101.5, 98.699997, 104.2, 95.900002, 104.3, 97.400002, 99.5;
101.5, 100.8, 93.5, 104.3, 98.199997, 102.4, 97.800003;
101.5, 114.2, 100.7, 97.400002, 102.4, 99.099998, 99;
101.5, 104.2, 104.3, 99.5, 97.800003, 99, 96.800003;
101.5, 100.8, 100.8, 99.400002, 101.8, 101.8, 101.3;
101.5, 93.5, 98.199997, 97.800003, 98.400002, 101.3, 105.3;
101.5, 95.900002, 99.5, 104.3, 96.800003, 103.7, 100.3;
101.5, 100.7, 102.4, 99, 101.3, 100, 98.099998;
101.5, 106.7, 96, 102, 99.400002, 102.3, 101.1;
101.5, 104.3, 97.800003, 96.800003, 105.3, 98.099998, 97;
101.5, 106.4, 98.300003, 99.599998, 96.900002, 97.599998, 97.699997;
101.5, 100.8, 101.8, 101.3, 100.3, 98.699997, 97.599998;
101.5, 97.400002, 99, 103.7, 98.099998, 107.2, 99.900002;
101.5, 98.199997, 98.400002, 105.3, 97.699997, 99.300003, 103;
101.5, 98.199997, 103.7, 98.400002, 110.1, 96.5, 96.900002;
101.5, 99.5, 96.800003, 100.3, 97, 99.900002, 100.7;
101.5, 103.6, 92.699997, 98.900002, 99.800003, 101.4, 94;
101.5, 102.4, 101.3, 98.099998, 99.300003, 98.699997, 97.400002;
101.5, 99.400002, 101.3, 99.300003, 97.599998, 100.4, 98.199997;
101.5, 96, 99.400002, 101.1, 97.199997, 102.4, 99.900002;
101.5, 98.199997, 98.699997, 100.4, 98.400002, 98.099998, 112.6;
101.5, 97.800003, 105.3, 97, 103, 97.400002, 98;
101.5, 99.099998, 100, 107.2, 98.699997, 100, 98.900002;
101.5, 98.300003, 96.900002, 97.699997, 101.7, 99.699997, 99.199997;
101.5, 104.3, 100.3, 97.300003, 100.7, 97, 97.800003;
101.5, 101.8, 100.3, 97.599998, 99.300003, 98.400002, 98.300003;
101.5, 97.699997, 97.199997, 102.5, 99.699997, 98.199997, 101.6;
101.5, 99, 98.099998, 99.900002, 97.400002, 98.900002, 98.699997;
101.5, 98.099998, 102.3, 97.099998, 102.1, 97.900002, 98.199997]请按任意键继续
转载于:https://blog.51cto.com/luoqiu/1618532