2012-12-12 62 views
3

我正在嘗試使用openCV從xml文件中讀取和顯示矩陣。 XML文件是這樣的:使用openCV從xml文件中讀取和顯示矩陣

<?xml version="1.0"?> 
    <opencv_storage> 
    <frame_00000> 
     <pose type_id="opencv-matrix"> 
     <rows>0</rows> 
     <cols>0</cols> 
     <dt>u</dt> 
     <data></data></pose> 
     <expertCode>3</expertCode> 
     <autoCode>-1</autoCode></frame_00000> 
    <frame_00001> 
     <pose type_id="opencv-matrix"> 
     <rows>0</rows> 
     <cols>0</cols> 
     <dt>u</dt> 
     <data></data></pose> 
     <expertCode>0</expertCode> 
     <autoCode>-1</autoCode></frame_00001> 
    <frame_00002> 
     <pose type_id="opencv-matrix"> 
     <rows>6</rows> 
     <cols>1</cols> 
     <dt>d</dt> 
     <data> 
      9.6603986167822176e-02 2.7534827334102827e-02 
      -7.9839974858475181e-03 2.9772357539313782e+02 
      2.6446663460538508e+02 1.5645098067258549e+00</data></pose> 
     <expertCode>0</expertCode> 
     <autoCode>0</autoCode></frame_00002> 
etc... 

我已成功地打開該文件,但我不能讓它打印幀數據時,它被編譯並運行。這是我的代碼:

#include "opencv2/opencv.hpp" 
#include <fstream> 

using namespace cv; 
using namespace std; 

int main() 
{ 
    std::cout<< endl << "Reading:" << endl; 
    FileStorage fs; 
    fs.open("output.xml", FileStorage::READ); 

    if (fs.isOpened()) 
    { 
     cout<<"File is opened\n"; 
    } 


    Mat pose2; 
    fs["pose"] >> pose2; 
    std::cout<< pose2; 

    fs.release(); 
    return (0); 
} 

問題是與fs.release前()代碼的最後塊。不管我嘗試什麼,它都不顯示數據。

我希望它顯示來自xml文件的所有幀數據。我一直在使用OpenCV教程和參考手冊作爲指導,但這沒有幫助。

任何指針將不勝感激,即使它只是我應該使用的命令的基本輪廓。

+0

你自己創建了xml文件嗎? – Sassa

+0

不,它是由別人編寫的一款面向軟件生成的。 – JM92

+0

也許我錯了,但這是格式化的方式,我不認爲你可以用OpenCV的內置函數讀取它。您可以使用簡單的XML解析器來完成這項工作。看看[這個問題](http://stackoverflow.com/questions/170686/best-open-xml-parser-for-c?lq=1)的建議。 – Sassa

回答

2
FileNode n = fs.root(); 
    for (FileNodeIterator current = n.begin(); current != n.end(); current++) { 
     FileNode item = *current; 
     Mat v; 
     item["pose"] >> v; 
     cout << v << endl; 
    } 

This Works! :)