OpenCV  2.4.13
Open Source Computer Vision
cv::PCA Class Reference

#include <core.hpp>

Public Member Functions

 PCA ()
 default constructor More...
 
 PCA (InputArray data, InputArray mean, int flags, int maxComponents=0)
 the constructor that performs PCA More...
 
 PCA (InputArray data, InputArray mean, int flags, double retainedVariance)
 
PCAoperator() (InputArray data, InputArray mean, int flags, int maxComponents=0)
 operator that performs PCA. The previously stored data, if any, is released More...
 
PCAcomputeVar (InputArray data, InputArray mean, int flags, double retainedVariance)
 
Mat project (InputArray vec) const
 projects vector from the original space to the principal components subspace More...
 
void project (InputArray vec, OutputArray result) const
 projects vector from the original space to the principal components subspace More...
 
Mat backProject (InputArray vec) const
 reconstructs the original vector from the projection More...
 
void backProject (InputArray vec, OutputArray result) const
 reconstructs the original vector from the projection More...
 

Public Attributes

Mat eigenvectors
 eigenvectors of the covariation matrix More...
 
Mat eigenvalues
 eigenvalues of the covariation matrix More...
 
Mat mean
 mean value subtracted before the projection and added after the back projection More...
 

Detailed Description

Principal Component Analysis

The class PCA is used to compute the special basis for a set of vectors. The basis will consist of eigenvectors of the covariance matrix computed from the input set of vectors. After PCA is performed, vectors can be transformed from the original high-dimensional space to the subspace formed by a few most prominent eigenvectors (called the principal components), corresponding to the largest eigenvalues of the covariation matrix. Thus the dimensionality of the vector and the correlation between the coordinates is reduced.

The following sample is the function that takes two matrices. The first one stores the set of vectors (a row per vector) that is used to compute PCA, the second one stores another "test" set of vectors (a row per vector) that are first compressed with PCA, then reconstructed back and then the reconstruction error norm is computed and printed for each vector.

using namespace cv;
PCA compressPCA(const Mat& pcaset, int maxComponents,
const Mat& testset, Mat& compressed)
{
PCA pca(pcaset, // pass the data
Mat(), // we do not have a pre-computed mean vector,
// so let the PCA engine to compute it
CV_PCA_DATA_AS_ROW, // indicate that the vectors
// are stored as matrix rows
// (use CV_PCA_DATA_AS_COL if the vectors are
// the matrix columns)
maxComponents // specify, how many principal components to retain
);
// if there is no test data, just return the computed basis, ready-to-use
if( !testset.data )
return pca;
CV_Assert( testset.cols == pcaset.cols );
compressed.create(testset.rows, maxComponents, testset.type());
Mat reconstructed;
for( int i = 0; i < testset.rows; i++ )
{
Mat vec = testset.row(i), coeffs = compressed.row(i), reconstructed;
// compress the vector, the result will be stored
// in the i-th row of the output matrix
pca.project(vec, coeffs);
// and then reconstruct it
pca.backProject(coeffs, reconstructed);
// and measure the error
printf("%d. diff = %g\n", i, norm(vec, reconstructed, NORM_L2));
}
return pca;
}

Constructor & Destructor Documentation

cv::PCA::PCA ( )

default constructor

cv::PCA::PCA ( InputArray  data,
InputArray  mean,
int  flags,
int  maxComponents = 0 
)

the constructor that performs PCA

cv::PCA::PCA ( InputArray  data,
InputArray  mean,
int  flags,
double  retainedVariance 
)

Member Function Documentation

Mat cv::PCA::backProject ( InputArray  vec) const

reconstructs the original vector from the projection

void cv::PCA::backProject ( InputArray  vec,
OutputArray  result 
) const

reconstructs the original vector from the projection

PCA& cv::PCA::computeVar ( InputArray  data,
InputArray  mean,
int  flags,
double  retainedVariance 
)
PCA& cv::PCA::operator() ( InputArray  data,
InputArray  mean,
int  flags,
int  maxComponents = 0 
)

operator that performs PCA. The previously stored data, if any, is released

Mat cv::PCA::project ( InputArray  vec) const

projects vector from the original space to the principal components subspace

void cv::PCA::project ( InputArray  vec,
OutputArray  result 
) const

projects vector from the original space to the principal components subspace

Member Data Documentation

Mat cv::PCA::eigenvalues

eigenvalues of the covariation matrix

Mat cv::PCA::eigenvectors

eigenvectors of the covariation matrix

Mat cv::PCA::mean

mean value subtracted before the projection and added after the back projection


The documentation for this class was generated from the following file: