2012-04-25 62 views
3

我試圖從.yaml文件,但OpenCV的加載矩陣使我有以下錯誤:OpenCV的Storagefile錯誤:

OpenCV Error: Parsing Error (myFile.yaml(1): valid xml should start with ') OpenCV Error: Parsing Error (myFile.yaml(1): Tag should start with '<'> in unknown function)

這是我寫入庫文件,whcih正常工作:

cv::FileStorage fs("myFile.yaml", cv::FileStorage::APPEND); 
while(counter<_imgPtrVector.size()){  
    unsigned char* _pointer=(unsigned char*)_imgPtrVector.at(counter); 
    cv::Mat _matrixImage(cv::Size(width,height), CV_8UC1,_pointer , cv::Mat::AUTO_STEP);  
    fs <<"Matrix"<<_matrixImage; 
    counter++; 
} 

但是當我想從同一個文件加載數據時,我得到了這些錯誤;這是從存儲文件中讀取的代碼:

cv::FileStorage f("myFile.yaml", cv::FileStorage::READ); 
cv::Mat mat(cv::Size(width,height), CV_8UC1); 
if(f.isOpened()){ 
    cv::FileNode n = f["Matrix"]; 
    if (n.type() != cv::FileNode::SEQ){ 
    std::cout << "error!"; 
    } 
    f["Matrix"] >> mat; 
} 
+0

如果您有進一步的信息需要添加,請編輯您自己的問題。請不要編輯別人的答案,因爲這樣看起來就像他們寫了你實際寫的內容。 – 2012-04-25 21:22:24

+0

hi Luke對不起,我完全沒有打算。 – user261002 2012-04-25 22:42:18

+0

編輯自己的問題的另一件事是編輯立即出現。如果您編輯他人的答案,這些編輯必須在其他用戶出現之前得到其他用戶的批准,並且如果編輯被拒絕,他們可能永遠不會顯示。看來你對卡爾答案的編輯確實被拒絕了。 – 2012-04-26 06:28:40

回答

2

問題可能是您始終追加到現有文件。因此,您可能必須將代碼更改爲:

FileStorage fs("test.yml", FileStorage::WRITE); 

這會在您每次運行程序時重新創建該文件。

在OpenCV文檔與XML/YAML持久性上how to write一個例子,這是非常明確的:

#include "opencv2/opencv.hpp" 
#include <time.h> 

using namespace cv; 

int main(int, char** argv) 
{ 
    FileStorage fs("test.yml", FileStorage::WRITE); 

    fs << "frameCount" << 5; 
    time_t rawtime; time(&rawtime); 
    fs << "calibrationDate" << asctime(localtime(&rawtime)); 
    Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1); 
    Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0); 
    fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs; 
    fs << "features" << "["; 
    for(int i = 0; i < 3; i++) 
    { 
     int x = rand() % 640; 
     int y = rand() % 480; 
     uchar lbp = rand() % 256; 

     fs << "{:" << "x" << x << "y" << y << "lbp" << "[:"; 
     for(int j = 0; j < 8; j++) 
      fs << ((lbp >> j) & 1); 
     fs << "]" << "}"; 
    } 
    fs << "]"; 
    fs.release(); 
    return 0; 
} 

而且有顯示how to read另一個例子。

+0

感謝鏈接非常有用。還有一個問題,我需要將這個文件傳遞給我們團隊中的另一個人來處理matlab中的矩陣,我知道有一個yamlmatlab庫來讀取文件,但是你知道opencv是否給我們任何其他格式文件來保存matlab的矩陣?請讓我知道謝謝 – user261002 2012-04-25 19:21:18

+1

似乎有一些與MEX相關的東西,但我不知道它是什麼。 – karlphillip 2012-04-25 19:25:45

+0

謝謝,當我運行代碼時,先寫入文件並關閉流,這很有趣,從代碼中讀取,代碼工作正常,但是當我只想從存儲文件讀取數據時,它給了我相同的例外。 – user261002 2012-04-25 19:31:32