OpenCV: cv::PCA Class Reference (2024)

Principal Component Analysis. More...

#include <opencv2/core.hpp>

Collaboration diagram for cv::PCA:

Public Types

enum Flags {
DATA_AS_ROW = 0,
DATA_AS_COL = 1,
USE_AVG = 2
}

Public Member Functions

PCA ()
default constructor
PCA (InputArray data, InputArray mean, int flags, double retainedVariance)
PCA (InputArray data, InputArray mean, int flags, int maxComponents=0)
MatbackProject (InputArray vec) const
Reconstructs vectors from their PC projections.
voidbackProject (InputArray vec, OutputArray result) const
PCA &operator() (InputArray data, InputArray mean, int flags, double retainedVariance)
PCA &operator() (InputArray data, InputArray mean, int flags, int maxComponents=0)
performs PCA
Matproject (InputArray vec) const
Projects vector(s) to the principal component subspace.
voidproject (InputArray vec, OutputArray result) const
voidread (const FileNode &fn)
load PCA objects
voidwrite (FileStorage &fs) const
write PCA objects

Public Attributes

Mateigenvalues
eigenvalues of the covariation matrix
Mateigenvectors
eigenvectors of the covariation matrix
Matmean
mean value subtracted before the projection and added after the back projection

Detailed Description

Principal Component Analysis.

The class is used to calculate a special basis for a set of vectors. The basis will consist of eigenvectors of the covariance matrix calculated from the input set of vectors. The class PCA can also transform vectors to/from the new coordinate space defined by the basis. Usually, in this new coordinate system, each vector from the original set (and any linear combination of such vectors) can be quite accurately approximated by taking its first few components, corresponding to the eigenvectors of the largest eigenvalues of the covariance matrix. Geometrically it means that you calculate a projection of the vector to a subspace formed by a few eigenvectors corresponding to the dominant eigenvalues of the covariance matrix. And usually such a projection is very close to the original vector. So, you can represent the original vector from a high-dimensional space with a much shorter vector consisting of the projected vector's coordinates in the subspace. Such a transformation is also known as Karhunen-Loeve Transform, or KLT. See http://en.wikipedia.org/wiki/Principal_component_analysis

The sample below is the function that takes two matrices. The first function stores a set of vectors (a row per vector) that is used to calculate PCA. The second function stores another "test" set of vectors (a row per vector). First, these vectors are 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

PCA::DATA_AS_ROW, // indicate that the vectors

// are stored as matrix rows

// (use 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;

}

cv::Mat::row

Mat row(int y) const

Creates a matrix header for the specified matrix row.

cv::Mat::data

uchar * data

pointer to the data

Definition mat.hpp:2140

cv::Mat::create

void create(int rows, int cols, int type)

Allocates new array data if needed.

cv::Mat::cols

int cols

Definition mat.hpp:2138

cv::Mat::rows

int rows

the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions

Definition mat.hpp:2138

cv::Mat::type

int type() const

Returns the type of a matrix element.

cv::PCA

Principal Component Analysis.

Definition core.hpp:2513

cv::PCA::DATA_AS_ROW

@ DATA_AS_ROW

indicates that the input samples are stored as matrix rows

Definition core.hpp:2515

cv::norm

double norm(InputArray src1, int normType=NORM_L2, InputArray mask=noArray())

Calculates the absolute norm of an array.

cv::NORM_L2

@ NORM_L2

Definition base.hpp:185

CV_Assert

#define CV_Assert(expr)

Checks a condition at runtime and throws exception if it fails.

Definition base.hpp:359

cv

"black box" representation of the file storage associated with a file on disk.

Definition core.hpp:102

See also
calcCovarMatrix, mulTransposed, SVD, dft, dct
Examples
samples/cpp/pca.cpp, and samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp.

Member Enumeration Documentation

Flags

enum cv::PCA::Flags

Enumerator
DATA_AS_ROW

indicates that the input samples are stored as matrix rows

DATA_AS_COL

indicates that the input samples are stored as matrix columns

USE_AVG

Constructor & Destructor Documentation

PCA() [1/3]

cv::PCA::PCA ( )

default constructor

The default constructor initializes an empty PCA structure. The other constructors initialize the structure and call PCA::operator()().

PCA() [2/3]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
datainput samples stored as matrix rows or matrix columns.
meanoptional mean value; if the matrix is empty (noArray()), the mean is computed from the data.
flagsoperation flags; currently the parameter is only used to specify the data layout (PCA::Flags)
maxComponentsmaximum number of components that PCA should retain; by default, all the components are retained.

PCA() [3/3]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
datainput samples stored as matrix rows or matrix columns.
meanoptional mean value; if the matrix is empty (noArray()), the mean is computed from the data.
flagsoperation flags; currently the parameter is only used to specify the data layout (PCA::Flags)
retainedVariancePercentage of variance that PCA should retain. Using this parameter will let the PCA decided how many components to retain but it will always keep at least 2.

Member Function Documentation

backProject() [1/2]

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

Reconstructs vectors from their PC projections.

The methods are inverse operations to PCA::project. They take PC coordinates of projected vectors and reconstruct the original vectors. Unless all the principal components have been retained, the reconstructed vectors are different from the originals. But typically, the difference is small if the number of components is large enough (but still much smaller than the original vector dimensionality). As a result, PCA is used.

Parameters
veccoordinates of the vectors in the principal component subspace, the layout and size are the same as of PCA::project output vectors.

backProject() [2/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
veccoordinates of the vectors in the principal component subspace, the layout and size are the same as of PCA::project output vectors.
resultreconstructed vectors; the layout and size are the same as of PCA::project input vectors.

operator()() [1/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
datainput samples stored as the matrix rows or as the matrix columns.
meanoptional mean value; if the matrix is empty (noArray()), the mean is computed from the data.
flagsoperation flags; currently the parameter is only used to specify the data layout. (PCA::Flags)
retainedVariancePercentage of variance that PCA should retain. Using this parameter will let the PCA decided how many components to retain but it will always keep at least 2.

operator()() [2/2]

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

performs PCA

The operator performs PCA of the supplied dataset. It is safe to reuse the same PCA structure for multiple datasets. That is, if the structure has been previously used with another dataset, the existing internal data is reclaimed and the new eigenvalues, eigenvectors and mean are allocated and computed.

The computed eigenvalues are sorted from the largest to the smallest and the corresponding eigenvectors are stored as eigenvectors rows.

Parameters
datainput samples stored as the matrix rows or as the matrix columns.
meanoptional mean value; if the matrix is empty (noArray()), the mean is computed from the data.
flagsoperation flags; currently the parameter is only used to specify the data layout. (Flags)
maxComponentsmaximum number of components that PCA should retain; by default, all the components are retained.

project() [1/2]

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

Projects vector(s) to the principal component subspace.

The methods project one or more vectors to the principal component subspace, where each vector projection is represented by coefficients in the principal component basis. The first form of the method returns the matrix that the second form writes to the result. So the first form can be used as a part of expression while the second form can be more efficient in a processing loop.

Parameters
vecinput vector(s); must have the same dimensionality and the same layout as the input data used at PCA phase, that is, if DATA_AS_ROW are specified, then vec.cols==data.cols (vector dimensionality) and vec.rows is the number of vectors to project, and the same is true for the PCA::DATA_AS_COL case.

project() [2/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
vecinput vector(s); must have the same dimensionality and the same layout as the input data used at PCA phase, that is, if DATA_AS_ROW are specified, then vec.cols==data.cols (vector dimensionality) and vec.rows is the number of vectors to project, and the same is true for the PCA::DATA_AS_COL case.
resultoutput vectors; in case of PCA::DATA_AS_COL, the output matrix has as many columns as the number of input vectors, this means that result.cols==vec.cols and the number of rows match the number of principal components (for example, maxComponents parameter passed to the constructor).

read()

void cv::PCA::read ( const FileNode & fn)

load PCA objects

Loads eigenvalues eigenvectors and mean from specified FileNode

write()

void cv::PCA::write ( FileStorage & fs) const

write PCA objects

Writes eigenvalues eigenvectors and mean to specified FileStorage

Member Data Documentation

eigenvalues

Mat cv::PCA::eigenvalues

eigenvalues of the covariation matrix

eigenvectors

Mat cv::PCA::eigenvectors

eigenvectors of the covariation matrix

mean

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:

  • opencv2/core.hpp
OpenCV: cv::PCA Class Reference (2024)
Top Articles
In the Loop: Can Oklahoma State be the next face of the Big 12?
PGGM Investments Sells 3,163 Shares of O'Reilly Automotive, Inc. (NASDAQ:ORLY)
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Things to do in Wichita Falls on weekends 12-15 September
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
What's the Difference Between Halal and Haram Meat & Food?
R/Skinwalker
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Cubilabras
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Juego Friv Poki
Dirt Devil Ud70181 Parts Diagram
Truist Bank Open Saturday
Water Leaks in Your Car When It Rains? Common Causes & Fixes
What’s Closing at Disney World? A Complete Guide
New from Simply So Good - Cherry Apricot Slab Pie
Drys Pharmacy
Ohio State Football Wiki
Find Words Containing Specific Letters | WordFinder®
FirstLight Power to Acquire Leading Canadian Renewable Operator and Developer Hydromega Services Inc. - FirstLight
Webmail.unt.edu
2024-25 ITH Season Preview: USC Trojans
Metro By T Mobile Sign In
Restored Republic December 1 2022
12 30 Pacific Time
Jami Lafay Gofundme
Greenbrier Bunker Tour Coupon
No Compromise in Maneuverability and Effectiveness
Crystal Westbrooks Nipple
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 5487

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.