EigenTest.cpp:Defines the entry point for the console application.
#include "stdafx.h" void mypca (const Mat &_data, int Dim, Mat &eigenvalues, Mat &eigenvectors);
void Printmat (Mat _data) {Mat data = cv::mat_<double> (_data); for (int i=0, i<data.rows; i++) {for (int j=0; j< Data.cols; j + +) {cout <&L T
Data.at<double> (i,j) << "";
} cout << Endl; }} int _tmain (int argc, _tchar* argv[]) {float a[60]= {1.5, 2.3, 1.5, 2.3, 1.5, 2.3, 3.0, 1.7 , 3.0, 1.7, 3.0, 1.7, 1.2, 2.9, 1.2, 2.9, 1.2, 2.9, 2.1, 2.2, 2.1, 2.2, 2.1, 2.2, 3.1, 3.1 , 3.1, 3.1, 3.1, 3.1, 1.3, 2.7, 1.3, 2.7, 1.3, 2.7, 2.0, 1.7, 2.0, 1.7, 2.0, 1.7, 1.0, 2.0
, 1.0, 2.0, 1.0, 2.0, 0.5, 0.6, 0.5, 0.6, 0.5, 0.6, 1.0, 0.9, 1.0, 0.9, 1.0, 0.9};
Mat m = Mat::zeros (Ten, 6, cv_32f);
for (int i=0, i<10; i++) {for (int j=0; j<6; J + +) {M.at<float>
;(i,j) = A[i*6+j];
}} cout << "Origin data:" << Endl;
Printmat (m);
OPENCV PCA PCA PCA (M, Noarray (), cv_pca_data_as_row);
cout << "pca.eigenvalues:" << Endl;
Printmat (pca.eigenvalues);
cout << "pca.eigenvectors:" << Endl;
Printmat (pca.eigenvectors);
My PCA Mat eigenvalues;
Mat eigenvectors;
MYPCA (M, 3, eigenvalues, eigenvectors);
cout << "eigenvalues:" << Endl;
Printmat (eigenvalues);
cout << "eigenvectors:" << Endl;
Printmat (eigenvectors);
return 0;
} void Mypca (const Mat &_data, int Dim, Mat &eigenvalues, Mat &eigenvectors) {//#define Showpcamsg
ASSERT (DIM>0);
Mat data = cv::mat_<double> (_data);
#ifdef showpcamsg Printmat (data);#endif int N = data.rows;
int D = Data.cols;
if (dim>d) {Dim = D;
}///mean Mat m = Mat::zeros (1, D, Data.type ()); for (int j=0; j<d; J + +) {for (int i=0; i<n; i++) {m.at<double> (0,j) + = D
Ata.at<double> (I,J);
}} m = m/n;
The dispersion matrix calculates the Mat S = Mat::zeros (N, D, Data.type ()); for (int i=0, i<n; i++) {for (int j=0; j<d; J + +) {s.at<double> (i,j) = da Ta.at<double> (I,J)-m.at<double> (0,J); < Data-mean}} Mat C = S.T () * S/(N);
< divided by n, or N-1 #ifdef showpcamsg printmat (C);
#endif////Ask eigenvalue eigenvector eigen (C, eigenvalues, eigenvectors);
}