2013-02-16 91 views
0

我需要將一組訓練圖像的OpenCV PCA對象(特徵值,特徵向量)存儲到持久性存儲中,以便稍後重新加載以供測試。我使用OpenCV 2.4功能XML/YAML file storages將我的特徵向量和特徵值矩陣寫入yaml文件。但是,當重新加載文件並將相同的輸入圖像投影到重新加載的PCA空間時,我沒有得到0投影之間的區別嗎?我相信我失去了精確性,但似乎無法弄清楚爲什麼?我根據我的代碼在他的解決方案「Saving pca object in opencv"OpenCV將PCA特徵向量寫入yaml文件,失去精度?

int numPrincipalComponents = db.size()-1; 
Mat output1, output2; 
PCA pca(matrix, global_mean_vec, CV_PCA_DATA_AS_ROW, numPrincipalComponents); 

pca.project(matrix.row(0), output1); //Project first image into orig. PCA 

Mat eigenvalues = pca.eigenvalues.clone(); 
Mat eigenvectors = pca.eigenvectors.clone(); 

//Write matrices to pca_happy.yml 
FileStorage fs("./Train/FileStore/pca_happy.yml", FileStorage::WRITE); 
fs << "Eigenvalues" << eigenvalues; 
fs << "Eigenvector" << eigenvectors; 
fs.release(); 

//Load matrices from pca_happy.yml 
FileStorage fs1("./Train/FileStore/pca_happy.yml", FileStorage::READ); 
Mat loadeigenvectors, loadeigenvalues; 
fs1["Eigenvalues"] >> eigenvalues; 
fs1["Eigenvector"] >> eigenvectors; 
fs1.release(); 

PCA pca2; 
pca2.mean = global_mean_vec; 
pca2.eigenvalues = loadeigenvalues; 
pca2.eigenvectors = loadeigenvectors; 

pca2.project(matrix.row(0), output2); 

Mat diff; 
absdiff(output1, output2, diff); 

cout<<sum(diff)[0]<<endl; 

通過@link給出一個答案然而不同的是88.4,應該是0,因爲我伸出完全相同的圖像。我需要存儲?特徵向量矩陣中的每一行的任何建議,非常感謝

回答

0

我設置的特徵值,特徵向量和手段或PCA2時做了一個非常愚蠢的錯誤

PCA pca2; 
pca2.mean = global_mean_vec; 
pca2.eigenvalues = loadeigenvalues; 
pca2.eigenvectors = loadeigenvectors; 

應該是:!

PCA pca2; 
pca2.mean = global_mean_vec.clone(); 
pca2.eigenvalues = loadeigenvalues.clone(); 
pca2.eigenvectors = loadeigenvectors.clone(); 

希望這可以幫助其他人!

0

您是否正確計算了均值向量? 我只是做了兩個小的修改:(用我自己的matrix

PCA pca(matrix, Mat(), CV_PCA_DATA_AS_ROW, numPrincipalComponents);//compute mean automatically 
pca2.mean = pca.mean; 

diff爲零。

0

我想,PCA2應該是:

Mat eigenvalues1,eigenvectors1; 
FileStorage fs1("fileName.yml", FileStorage::READ); 
//Mat loadeigenvectors, loadeigenvalues; 
fs1["Eigenvalues"] >> eigenvalues1; 
fs1["Eigenvector"] >> eigenvectors1; 
fs1.release(); 

PCA pca2; 
pca2.mean = pca.mean; 
pca2.eigenvalues = eigenvalues1; 
pca2.eigenvectors = eigenvectors1;