# cv::PCA Class Reference

`#include <core.hpp>`

List of all members.

## Public Member Functions

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

## Public Attributes

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

## 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 | ( | const Mat & | data,
|  | const Mat & | mean,
|  | int | flags,
|  | int | maxComponents = `0`
| ) |  |

the constructor that performs PCA

## Member Function Documentation

Mat cv::PCA::backProject | ( | const Mat & | vec ) | const

reconstructs the original vector from the projection

void cv::PCA::backProject | ( | const Mat & | vec,
|  | CV_OUT Mat & | result
| ) |  | const

reconstructs the original vector from the projection

PCA& cv::PCA::operator() | ( | const Mat & | data,
|  | const Mat & | mean,
|  | int | flags,
|  | int | maxComponents = `0`
| ) |  |

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

void cv::PCA::project | ( | const Mat & | vec,
|  | CV_OUT Mat & | result
| ) |  | const

projects vector from the original space to the principal components subspace

Mat cv::PCA::project | ( | const Mat & | vec ) | 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:

- /usr/obj/OpenCV-2.2.0/OpenCV-2.2.0/modules/core/include/opencv2/core/core.hpp

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines