org.bytedeco.opencv.opencv_core

## Class PCA

• All Implemented Interfaces:
AutoCloseable

@Namespace(value="cv")
@NoOffset
@Properties(inherit=opencv_core.class)
public class PCA
extends Pointer
\brief 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. :

 {.cpp}
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;
}

calcCovarMatrix, mulTransposed, SVD, dft, dct

• ### Nested classes/interfaces inherited from class org.bytedeco.javacpp.Pointer

Pointer.CustomDeallocator, Pointer.Deallocator, Pointer.NativeDeallocator, Pointer.ReferenceCounter
• ### Field Summary

Fields
Modifier and Type Field and Description
static int DATA_AS_COL
enum cv::PCA::Flags
static int DATA_AS_ROW
enum cv::PCA::Flags
static int USE_AVG
enum cv::PCA::Flags
• ### Fields inherited from class org.bytedeco.javacpp.Pointer

address, capacity, limit, position
• ### Constructor Summary

Constructors
Constructor and Description
PCA()
\brief default constructor
PCA(GpuMat data, GpuMat mean, int flags)
PCA(GpuMat data, GpuMat mean, int flags, double retainedVariance)
PCA(GpuMat data, GpuMat mean, int flags, int maxComponents)
PCA(long size)
Native array allocator.
PCA(Mat data, Mat mean, int flags)
PCA(Mat data, Mat mean, int flags, double retainedVariance)
PCA(Mat data, Mat mean, int flags, int maxComponents)
PCA(Pointer p)
Pointer cast constructor.
PCA(UMat data, UMat mean, int flags)
PCA(UMat data, UMat mean, int flags, double retainedVariance)
PCA(UMat data, UMat mean, int flags, int maxComponents)
• ### Method Summary

All Methods
Modifier and Type Method and Description
PCA apply(GpuMat data, GpuMat mean, int flags)
PCA apply(GpuMat data, GpuMat mean, int flags, double retainedVariance)
PCA apply(GpuMat data, GpuMat mean, int flags, int maxComponents)
PCA apply(Mat data, Mat mean, int flags)
PCA apply(Mat data, Mat mean, int flags, double retainedVariance)
PCA apply(Mat data, Mat mean, int flags, int maxComponents)
\brief performs %PCA
PCA apply(UMat data, UMat mean, int flags)
PCA apply(UMat data, UMat mean, int flags, double retainedVariance)
PCA apply(UMat data, UMat mean, int flags, int maxComponents)
Mat backProject(GpuMat vec)
void backProject(GpuMat vec, GpuMat result)
Mat backProject(Mat vec)
\brief Reconstructs vectors from their PC projections.
void backProject(Mat vec, Mat result)
Mat backProject(UMat vec)
void backProject(UMat vec, UMat result)
Mat eigenvalues()
eigenvalues of the covariation matrix
PCA eigenvalues(Mat setter)
Mat eigenvectors()
eigenvectors of the covariation matrix
PCA eigenvectors(Mat setter)
PCA getPointer(long i)
Mat mean()
mean value subtracted before the projection and added after the back projection
PCA mean(Mat setter)
PCA position(long position)
Mat project(GpuMat vec)
void project(GpuMat vec, GpuMat result)
Mat project(Mat vec)
\brief Projects vector(s) to the principal component subspace.
void project(Mat vec, Mat result)
Mat project(UMat vec)
void project(UMat vec, UMat result)
void read(FileNode fn)
void write(FileStorage fs)
\brief write PCA objects
• ### Methods inherited from class org.bytedeco.javacpp.Pointer

address, asBuffer, asByteBuffer, availablePhysicalBytes, calloc, capacity, capacity, close, deallocate, deallocate, deallocateReferences, deallocator, deallocator, equals, fill, formatBytes, free, getDirectBufferAddress, getPointer, getPointer, getPointer, hashCode, interruptDeallocatorThread, isNull, isNull, limit, limit, malloc, maxBytes, maxPhysicalBytes, memchr, memcmp, memcpy, memmove, memset, offsetAddress, offsetof, offsetof, parseBytes, physicalBytes, physicalBytesInaccurate, position, put, realloc, referenceCount, releaseReference, retainReference, setNull, sizeof, sizeof, toString, totalBytes, totalCount, totalPhysicalBytes, withDeallocator, zero
• ### Methods inherited from class java.lang.Object

clone, finalize, getClass, notify, notifyAll, wait, wait, wait
• ### Field Detail

• #### DATA_AS_ROW

public static final int DATA_AS_ROW
enum cv::PCA::Flags
Constant Field Values
• #### DATA_AS_COL

public static final int DATA_AS_COL
enum cv::PCA::Flags
Constant Field Values
• #### USE_AVG

public static final int USE_AVG
enum cv::PCA::Flags
Constant Field Values
• ### Constructor Detail

• #### PCA

public PCA()
\brief default constructor

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

• #### PCA

public PCA(@ByVal
Mat data,
@ByVal
Mat mean,
int flags,
int maxComponents)
Parameters:
data - input samples stored as matrix rows or matrix columns.
mean - optional mean value; if the matrix is empty (\c noArray()), the mean is computed from the data.
flags - operation flags; currently the parameter is only used to specify the data layout (PCA::Flags)
maxComponents - maximum number of components that %PCA should retain; by default, all the components are retained.
• #### PCA

public PCA(@ByVal
Mat data,
@ByVal
Mat mean,
int flags)
• #### PCA

public PCA(@ByVal
UMat data,
@ByVal
UMat mean,
int flags,
int maxComponents)
• #### PCA

public PCA(@ByVal
UMat data,
@ByVal
UMat mean,
int flags)
• #### PCA

public PCA(@ByVal
GpuMat data,
@ByVal
GpuMat mean,
int flags,
int maxComponents)
• #### PCA

public PCA(@ByVal
GpuMat data,
@ByVal
GpuMat mean,
int flags)
• #### PCA

public PCA(@ByVal
Mat data,
@ByVal
Mat mean,
int flags,
double retainedVariance)
Parameters:
data - input samples stored as matrix rows or matrix columns.
mean - optional mean value; if the matrix is empty (noArray()), the mean is computed from the data.
flags - operation flags; currently the parameter is only used to specify the data layout (PCA::Flags)
retainedVariance - Percentage 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.
• #### PCA

public PCA(@ByVal
UMat data,
@ByVal
UMat mean,
int flags,
double retainedVariance)
• #### PCA

public PCA(@ByVal
GpuMat data,
@ByVal
GpuMat mean,
int flags,
double retainedVariance)
• ### Method Detail

• #### position

public PCA position(long position)
Overrides:
position in class Pointer
• #### getPointer

public PCA getPointer(long i)
Overrides:
getPointer in class Pointer
• #### apply

@ByRef
@Name(value="operator ()")
public PCA apply(@ByVal
Mat data,
@ByVal
Mat mean,
int flags,
int maxComponents)
\brief 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 \ref eigenvalues, \ref eigenvectors and \ref mean are allocated and computed.

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

Parameters:
data - input samples stored as the matrix rows or as the matrix columns.
mean - optional mean value; if the matrix is empty (noArray()), the mean is computed from the data.
flags - operation flags; currently the parameter is only used to specify the data layout. (Flags)
maxComponents - maximum number of components that PCA should retain; by default, all the components are retained.
• #### apply

@ByRef
@Name(value="operator ()")
public PCA apply(@ByVal
Mat data,
@ByVal
Mat mean,
int flags)
• #### apply

@ByRef
@Name(value="operator ()")
public PCA apply(@ByVal
UMat data,
@ByVal
UMat mean,
int flags,
int maxComponents)
• #### apply

@ByRef
@Name(value="operator ()")
public PCA apply(@ByVal
UMat data,
@ByVal
UMat mean,
int flags)
• #### apply

@ByRef
@Name(value="operator ()")
public PCA apply(@ByVal
GpuMat data,
@ByVal
GpuMat mean,
int flags,
int maxComponents)
• #### apply

@ByRef
@Name(value="operator ()")
public PCA apply(@ByVal
GpuMat data,
@ByVal
GpuMat mean,
int flags)
• #### apply

@ByRef
@Name(value="operator ()")
public PCA apply(@ByVal
Mat data,
@ByVal
Mat mean,
int flags,
double retainedVariance)
Parameters:
data - input samples stored as the matrix rows or as the matrix columns.
mean - optional mean value; if the matrix is empty (noArray()), the mean is computed from the data.
flags - operation flags; currently the parameter is only used to specify the data layout. (PCA::Flags)
retainedVariance - Percentage 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.
• #### apply

@ByRef
@Name(value="operator ()")
public PCA apply(@ByVal
UMat data,
@ByVal
UMat mean,
int flags,
double retainedVariance)
• #### apply

@ByRef
@Name(value="operator ()")
public PCA apply(@ByVal
GpuMat data,
@ByVal
GpuMat mean,
int flags,
double retainedVariance)
• #### project

@ByVal
public Mat project(@ByVal
Mat vec)
\brief 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:
vec - input 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

@ByVal
public Mat project(@ByVal
UMat vec)
• #### project

@ByVal
public Mat project(@ByVal
GpuMat vec)
• #### project

public void project(@ByVal
Mat vec,
@ByVal
Mat result)
Parameters:
vec - input 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.
result - output 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).
• #### project

public void project(@ByVal
UMat vec,
@ByVal
UMat result)
• #### project

public void project(@ByVal
GpuMat vec,
@ByVal
GpuMat result)
• #### backProject

@ByVal
public Mat backProject(@ByVal
Mat vec)
\brief 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:
vec - coordinates of the vectors in the principal component subspace, the layout and size are the same as of PCA::project output vectors.
• #### backProject

@ByVal
public Mat backProject(@ByVal
UMat vec)
• #### backProject

@ByVal
public Mat backProject(@ByVal
GpuMat vec)
• #### backProject

public void backProject(@ByVal
Mat vec,
@ByVal
Mat result)
Parameters:
vec - coordinates of the vectors in the principal component subspace, the layout and size are the same as of PCA::project output vectors.
result - reconstructed vectors; the layout and size are the same as of PCA::project input vectors.
• #### backProject

public void backProject(@ByVal
UMat vec,
@ByVal
UMat result)
• #### backProject

public void backProject(@ByVal
GpuMat vec,
@ByVal
GpuMat result)
• #### write

public void write(@ByRef
FileStorage fs)
\brief write PCA objects

Writes \ref eigenvalues \ref eigenvectors and \ref mean to specified FileStorage

public void read(@Const @ByRef
FileNode fn)

Loads \ref eigenvalues \ref eigenvectors and \ref mean from specified FileNode

• #### eigenvectors

@ByRef
public Mat eigenvectors()
eigenvectors of the covariation matrix
• #### eigenvectors

public PCA eigenvectors(Mat setter)
• #### eigenvalues

@ByRef
public Mat eigenvalues()
eigenvalues of the covariation matrix
• #### eigenvalues

public PCA eigenvalues(Mat setter)
• #### mean

@ByRef
public Mat mean()
mean value subtracted before the projection and added after the back projection
• #### mean

public PCA mean(Mat setter)