在opencv中保存pca对象

发布于 2024-12-14 12:01:50 字数 190 浏览 1 评论 0原文

我正在开发一个人脸识别项目,其中我们使用 PCA 来减少图像的特征向量大小。问题是,在训练期间,我通过合并所有训练图像来创建 PCA 对象。现在,在测试过程中,我需要之前获得的 PCA 对象。

我似乎无法弄清楚如何将 PCA 对象写入文件,以便我可以在测试期间使用它。一种替代方法是将其特征向量写入文件。但编写对象本身会方便得多。有办法做到这一点吗?

I'm working on a face recognition project in which we are using PCA to reduce feature vector size of an image. The trouble is, during training, I create the PCA object by incorporating all the training images. Now, during testing, I need the PCA object obtained earlier.

I cannot seem to figure out how to write the PCA object to a file, so that I can use it during testing. One alternative is that I write it's eigenvectors to the file. But it would be so much more convenient to write the object itself. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雾里花 2024-12-21 12:01:50

据我所知,没有通用的方法将 PCA 对象保存到文件中。您需要将特征向量、特征值和均值保存到文件中,然后在加载后将它们放入新的 PCA 中。您必须记住使用不会丢失精度的格式,尤其是对于平均值。

这是一些示例代码:

#include "opencv2/core/core.hpp"
#include <iostream>

...

cv::PCA pca1;
cv::PCA pca2;

cv::Mat eigenval,eigenvec,mean;
cv::Mat inputData;
cv::Mat outputData1,outputData2;

//input data has been populated with data to be used
pca1(inputData,Mat()/*dont have previously computed mean*/,
CV_PCA_DATA_AS_ROW /*depends of your data layout*/);//pca is computed
pca1.project(inputData,outputData1);

//here is how to extract matrices from pca
mean=pca1.mean.clone();
eigenval=pca1.eigenvalues.clone();
eigenvec=pca1.eigenvectors.clone();

//here You can save mean,eigenval and eigenvec matrices

//and here is how to use them to make another pca
pca2.eigenvalues=eigenval;
pca2.eigenvectors=eigenvec;
pca2.mean=mean;

pca2.project(inputData,outputData2);

cv::Mat diff;//here some proof that it works
cv::absdiff(outputData1,outputData2,diff);

std::cerr<<sum(diff)[0]<<std::endl; //assuming Youre using one channel data, there
                                    //is data only in first cell of the returned scalar

// if zero was printed, both output data matrices are identical

As far as I know, there is no generic way of saving PCA objects to a file. You will need to save eigenvectors, eigenvalues and mean to a file, and then put them into a new PCA after loading. You have to remember to use a format that doesn't lose precision, especially for mean.

Here is some example code:

#include "opencv2/core/core.hpp"
#include <iostream>

...

cv::PCA pca1;
cv::PCA pca2;

cv::Mat eigenval,eigenvec,mean;
cv::Mat inputData;
cv::Mat outputData1,outputData2;

//input data has been populated with data to be used
pca1(inputData,Mat()/*dont have previously computed mean*/,
CV_PCA_DATA_AS_ROW /*depends of your data layout*/);//pca is computed
pca1.project(inputData,outputData1);

//here is how to extract matrices from pca
mean=pca1.mean.clone();
eigenval=pca1.eigenvalues.clone();
eigenvec=pca1.eigenvectors.clone();

//here You can save mean,eigenval and eigenvec matrices

//and here is how to use them to make another pca
pca2.eigenvalues=eigenval;
pca2.eigenvectors=eigenvec;
pca2.mean=mean;

pca2.project(inputData,outputData2);

cv::Mat diff;//here some proof that it works
cv::absdiff(outputData1,outputData2,diff);

std::cerr<<sum(diff)[0]<<std::endl; //assuming Youre using one channel data, there
                                    //is data only in first cell of the returned scalar

// if zero was printed, both output data matrices are identical
夜声 2024-12-21 12:01:50

你可以试试这个。

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;
    fs.release();
}

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 ;
    fs.release();

}

这里是来源。

You may try this.

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;
    fs.release();
}

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 ;
    fs.release();

}

Here is the source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文