2

私は、PCA を使用して画像の特徴ベクトル サイズを縮小する顔認識プロジェクトに取り組んでいます。問題は、トレーニング中に、すべてのトレーニング画像を組み込んで PCA オブジェクトを作成することです。ここで、テスト中に、以前に取得した PCA オブジェクトが必要になります。

テスト中に使用できるように、PCA オブジェクトをファイルに書き込む方法がわかりません。1 つの代替方法は、その固有ベクトルをファイルに書き込むことです。しかし、オブジェクト自体を書く方がはるかに便利です。これを行う方法はありますか?

4

2 に答える 2

11

私の知る限り、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
于 2011-11-10T12:12:16.527 に答える