Ask Your Question

How to read/write a PCA object from/to a cv::FileStorage ?

asked 2012-12-04 15:50:43 -0600

Emmanuel gravatar image

Dear all,

I'm trying to save (and subsequently reuse) to a cv::FileStorage a cv::PCAobject after training it.

However, the cv::FileStorageclass does not implement suitable R/W methods for this type, and the cv::PCA class description does provide little insight on how to initialize an object without computing a principal component analysis.

Is what I want to do possible ? And in this case, could someone please point me to the methods that would be useful in my case,?

Thanks in advance.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-08-02 10:54:53 -0600

You have to save mean , eigen vectors & eigen values Matrix.

void save(const string &file_name,cv::PCA pca_)
    FileStorage fs(file_name,FileStorage::WRITE);
    fs << "mean" << pca_.mean;
    fs << "e_vectors" << pca_.eigenvectors;
    fs << "e_values" << pca_.eigenvalues;

int load(const string &file_name,cv::PCA pca_)
    FileStorage fs(file_name,FileStorage::READ);
    fs["mean"] >> pca_.mean ;
    fs["e_vectors"] >> pca_.eigenvectors ;
    fs["e_values"] >> pca_.eigenvalues ;

edit flag offensive delete link more

answered 2012-12-26 03:51:47 -0600

Emmanuel gravatar image

OK, I figured out myself a small workaround.

According to the code in the cv::PCA, the mean and covariance matrices are public attributes of the class. So I used cv::Mat's ability to read/write to a file, and read/wrote directly to the attributes of my PCA object.

It should do the trick (at least until the attributes get private :-/ ).

edit flag offensive delete link more


You should post a pull request to add the writing/loading ability to the PCA class.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-08-02 19:58:12 -0600 )edit

Agreed, otherwise the above posted solution by Mostafa is what I do

pickle27 gravatar imagepickle27 ( 2013-08-03 19:40:32 -0600 )edit

I'll write the small code and make the PR later this year when I get more time, it does not seem to be urgent yet.

Emmanuel gravatar imageEmmanuel ( 2013-08-23 03:58:48 -0600 )edit

I've made a PR, it's in review right now, I hope it will be available soon.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-08-24 01:15:03 -0600 )edit

Merci Mathieu :-)

Emmanuel gravatar imageEmmanuel ( 2013-08-26 01:37:36 -0600 )edit
sturkmen gravatar imagesturkmen ( 2016-02-20 17:15:32 -0600 )edit

Question Tools

1 follower


Asked: 2012-12-04 15:50:43 -0600

Seen: 3,326 times

Last updated: Aug 02 '13