Skip to content

Commit

Permalink
Add overloaded cv::PCACompute() that returns also the eigenvalues. Us…
Browse files Browse the repository at this point in the history
…eful for Java and Python OpenCV where PCA is not available.
  • Loading branch information
catree committed Jul 13, 2018
1 parent 9c3ec05 commit 4dc7e61
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/core/include/opencv2/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1978,10 +1978,20 @@ CV_EXPORTS_W void calcCovarMatrix( InputArray samples, OutputArray covar,
CV_EXPORTS_W void PCACompute(InputArray data, InputOutputArray mean,
OutputArray eigenvectors, int maxComponents = 0);

/** wrap PCA::operator() and add eigenvalues output parameter */
CV_EXPORTS_AS(PCACompute2) void PCACompute(InputArray data, InputOutputArray mean,
OutputArray eigenvectors, OutputArray eigenvalues,
int maxComponents = 0);

/** wrap PCA::operator() */
CV_EXPORTS_W void PCACompute(InputArray data, InputOutputArray mean,
OutputArray eigenvectors, double retainedVariance);

/** wrap PCA::operator() and add eigenvalues output parameter */
CV_EXPORTS_AS(PCACompute2) void PCACompute(InputArray data, InputOutputArray mean,
OutputArray eigenvectors, OutputArray eigenvalues,
double retainedVariance);

/** wrap PCA::project */
CV_EXPORTS_W void PCAProject(InputArray data, InputArray mean,
InputArray eigenvectors, OutputArray result);
Expand Down
26 changes: 26 additions & 0 deletions modules/core/src/pca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,19 @@ void cv::PCACompute(InputArray data, InputOutputArray mean,
pca.eigenvectors.copyTo(eigenvectors);
}

void cv::PCACompute(InputArray data, InputOutputArray mean,
OutputArray eigenvectors, OutputArray eigenvalues,
int maxComponents)
{
CV_INSTRUMENT_REGION()

PCA pca;
pca(data, mean, 0, maxComponents);
pca.mean.copyTo(mean);
pca.eigenvectors.copyTo(eigenvectors);
pca.eigenvalues.copyTo(eigenvalues);
}

void cv::PCACompute(InputArray data, InputOutputArray mean,
OutputArray eigenvectors, double retainedVariance)
{
Expand All @@ -371,6 +384,19 @@ void cv::PCACompute(InputArray data, InputOutputArray mean,
pca.eigenvectors.copyTo(eigenvectors);
}

void cv::PCACompute(InputArray data, InputOutputArray mean,
OutputArray eigenvectors, OutputArray eigenvalues,
double retainedVariance)
{
CV_INSTRUMENT_REGION()

PCA pca;
pca(data, mean, 0, retainedVariance);
pca.mean.copyTo(mean);
pca.eigenvectors.copyTo(eigenvectors);
pca.eigenvalues.copyTo(eigenvalues);
}

void cv::PCAProject(InputArray data, InputArray mean,
InputArray eigenvectors, OutputArray result)
{
Expand Down

0 comments on commit 4dc7e61

Please sign in to comment.