Merge pull request #1224 from mbarnach:pca_io
This commit is contained in:
commit
79457f8f49
@ -670,6 +670,10 @@ public:
|
|||||||
//! reconstructs the original vector from the projection
|
//! reconstructs the original vector from the projection
|
||||||
void backProject(InputArray vec, OutputArray result) const;
|
void backProject(InputArray vec, OutputArray result) const;
|
||||||
|
|
||||||
|
//! write and load PCA matrix
|
||||||
|
void write(FileStorage& fs ) const;
|
||||||
|
void read(const FileNode& fs);
|
||||||
|
|
||||||
Mat eigenvectors; //!< eigenvectors of the covariation matrix
|
Mat eigenvectors; //!< eigenvectors of the covariation matrix
|
||||||
Mat eigenvalues; //!< eigenvalues of the covariation matrix
|
Mat eigenvalues; //!< eigenvalues of the covariation matrix
|
||||||
Mat mean; //!< mean value subtracted before the projection and added after the back projection
|
Mat mean; //!< mean value subtracted before the projection and added after the back projection
|
||||||
|
@ -2911,6 +2911,27 @@ PCA& PCA::operator()(InputArray _data, InputArray __mean, int flags, int maxComp
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PCA::write(FileStorage& fs ) const
|
||||||
|
{
|
||||||
|
CV_Assert( fs.isOpened() );
|
||||||
|
|
||||||
|
fs << "name" << "PCA";
|
||||||
|
fs << "vectors" << eigenvectors;
|
||||||
|
fs << "values" << eigenvalues;
|
||||||
|
fs << "mean" << mean;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PCA::read(const FileNode& fs)
|
||||||
|
{
|
||||||
|
CV_Assert( !fs.empty() );
|
||||||
|
String name = (String)fs["name"];
|
||||||
|
CV_Assert( name == "PCA" );
|
||||||
|
|
||||||
|
cv::read(fs["vectors"], eigenvectors);
|
||||||
|
cv::read(fs["values"], eigenvalues);
|
||||||
|
cv::read(fs["mean"], mean);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
int computeCumulativeEnergy(const Mat& eigenvalues, double retainedVariance)
|
int computeCumulativeEnergy(const Mat& eigenvalues, double retainedVariance)
|
||||||
{
|
{
|
||||||
|
@ -510,6 +510,32 @@ protected:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
// Test read and write
|
||||||
|
FileStorage fs( "PCA_store.yml", FileStorage::WRITE );
|
||||||
|
rPCA.write( fs );
|
||||||
|
fs.release();
|
||||||
|
|
||||||
|
PCA lPCA;
|
||||||
|
fs.open( "PCA_store.yml", FileStorage::READ );
|
||||||
|
lPCA.read( fs.root() );
|
||||||
|
err = norm( rPCA.eigenvectors, lPCA.eigenvectors, CV_RELATIVE_L2 );
|
||||||
|
if( err > 0 )
|
||||||
|
{
|
||||||
|
ts->printf( cvtest::TS::LOG, "bad accuracy of write/load functions (YML); err = %f\n", err );
|
||||||
|
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
|
||||||
|
}
|
||||||
|
err = norm( rPCA.eigenvalues, lPCA.eigenvalues, CV_RELATIVE_L2 );
|
||||||
|
if( err > 0 )
|
||||||
|
{
|
||||||
|
ts->printf( cvtest::TS::LOG, "bad accuracy of write/load functions (YML); err = %f\n", err );
|
||||||
|
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
|
||||||
|
}
|
||||||
|
err = norm( rPCA.mean, lPCA.mean, CV_RELATIVE_L2 );
|
||||||
|
if( err > 0 )
|
||||||
|
{
|
||||||
|
ts->printf( cvtest::TS::LOG, "bad accuracy of write/load functions (YML); err = %f\n", err );
|
||||||
|
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user