天天看點

opencv中YAML或XML的輸入與輸出資料方法

用opencv對YAML或XML的操作分為兩種操作,操作與所對應的opencv類如下:

輸入資料 :FileStorag;

輸出資料 :FileStorag ,FileNode ,FileNodeIterator;

        因為對YAML與XML的操作都是一樣的,隻是讀取檔案時前者字尾是 .yaml 後者是 .xml,是以在這隻舉例對YAML操作,輸入的資料類型有一般常類型(如int),矩陣,序列(vector),maps和自定義的類。不管是輸入還是輸出資料都是通過鍵值的方式,輸入時先輸入鍵再輸入對應的值,輸出時是由鍵索引值,通過下面代碼會容易了解。

1.輸入資料 

資料有:

整型 int 100 ;

矩陣 Mat R = Mat_<uchar>::eye(3, 3),T = Mat_<double>::zeros(3, 1);

序列 ["image1.jpg" ,"Awesomeness" ,"baboon.jpg"];

maps  {One:1,Two:2 };

自定義類 MyData m(1);//類的具體定義在下面代碼中給出

代碼:

string filename = "E:\\test.yaml";
 FileStorage fs;
 fs.open(filename,FileStorage::WRITE);

            //輸入100 
            fs << "iterationNr" << 100;
            //輸入序列
            fs << "strings" << "[";                              
            fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
            fs << "]";                                           
           
            //輸入maps
            fs << "Mapping";                              
            fs << "{" << "One" << 1;
            fs <<        "Two" << 2 << "}";
           
            //輸入矩陣
            fs << "R" << R;                                      
            fs << "T" << T;
           
            //輸入自定義類
            fs << "MyData" << m;

fs.release();
           

2.輸出資料

代碼:

FileStorage fs;
 fs.open(filename, FileStorage::READ);

             int itNr;
            //fs["iterationNr"] >> itNr;
            itNr = (int) fs["iterationNr"];
            cout << itNr;
            if (!fs.isOpened())
            {
                cerr << "Failed to open " << filename << endl;

                return 1;
            }
            
            //擷取節點
            FileNode n = fs["strings"];                         
            if (n.type() != FileNode::SEQ)
            {
                cerr << "strings is not a sequence! FAIL" << endl;
                return 1;
            }
            //周遊序列輸出各元素
            FileNodeIterator it = n.begin(), it_end = n.end(); 
            for (; it != it_end; ++it)
                cout << (string)*it << endl;

            //周遊maps輸出各元素
            n = fs["Mapping"];                                
            cout << "Two  " << (int)(n["Two"]) << "; ";
            cout << "One  " << (int)(n["One"]) << endl << endl;


            MyData m;
            Mat R, T;
            
            //輸出矩陣
            fs["R"] >> R;                                      
            fs["T"] >> T;
            //輸出自定義類
            fs["MyData"] >> m;
           

      當打開檔案時,會使用FileStorag類的一個open()函數,這個函數中的第一個參數是檔案名路徑,第二個參數都以常量形式指定你要對檔案進行操作的類型,包括:WRITE, READ 或 APPEND。

3.完整的代碼如下:

using namespace cv;
using namespace std;


class MyData
{
public:
    MyData() : A(0), X(0), id() {}
    explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") {}  

    void write(FileStorage& fs) const                        //将類的資料寫入到fs
    {
        fs << "{" << "A" << A << "X" << X << "id" << id << "}";
    }
    void read(const FileNode& node)                          //将node的資料讀入類中
    {
        A = (int)node["A"];
        X = (double)node["X"];
        id = (string)node["id"];
    }
public:   // Data Members
    int A;
    double X;
    string id;
};

//These write and read functions must be defined for the serialization in FileStorage to work
void write(FileStorage& fs, const std::string&, const MyData& x)
{
    x.write(fs);//将x類中的資料寫入到fs中
}
void read(const FileNode& node, MyData& x, const MyData& default_value = MyData())
{
    if(node.empty())
        x = default_value;
    else
        x.read(node);//将node的資料讀入到x類中
}

// 對<<的重載,将類的屬性輸出
ostream& operator<<(ostream& out, const MyData& m)
{
    out << "{ id = " << m.id << ", ";
    out << "X = " << m.X << ", ";
    out << "A = " << m.A << "}";
    return out;
}

int main()
{

      string filename = "E:\\test.yaml";
        { //write
            Mat R = Mat_<uchar>::eye(3, 3),
                T = Mat_<double>::zeros(3, 1);
            MyData m(1);

            FileStorage fs(filename, FileStorage::WRITE);

            fs << "iterationNr" << 100;
            //輸入序列
            fs << "strings" << "[";                              
            fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
            fs << "]";                                           
           
            //輸入maps
            fs << "Mapping";                              
            fs << "{" << "One" << 1;
            fs <<        "Two" << 2 << "}";
           
            //輸入矩陣
            fs << "R" << R;                                      
            fs << "T" << T;
           
            //輸入自定義類
            fs << "MyData" << m;                                

            fs.release();                                       
            cout << "Write Done." << endl;
        }

        {//read
            cout << endl << "Reading: " << endl;
            FileStorage fs;
            fs.open(filename, FileStorage::READ);

            int itNr;
            //fs["iterationNr"] >> itNr;
            itNr = (int) fs["iterationNr"];
            cout << itNr;
            if (!fs.isOpened())
            {
                cerr << "Failed to open " << filename << endl;

                return 1;
            }
            
            //擷取節點
            FileNode n = fs["strings"];                        
            if (n.type() != FileNode::SEQ)
            {
                cerr << "strings is not a sequence! FAIL" << endl;
                return 1;
            }
            //周遊序列輸出各元素
            FileNodeIterator it = n.begin(), it_end = n.end(); 
            for (; it != it_end; ++it)
                cout << (string)*it << endl;

            //周遊maps輸出各元素
            n = fs["Mapping"];                                
            cout << "Two  " << (int)(n["Two"]) << "; ";
            cout << "One  " << (int)(n["One"]) << endl << endl;


            MyData m;
            Mat R, T;
            
            //輸出矩陣
            fs["R"] >> R;                                     
            fs["T"] >> T;
            //輸出自定義類
            fs["MyData"] >> m;                                  

            cout << endl
                << "R = " << R << endl;
            cout << "T = " << T << endl << endl;
            cout << "MyData = " << endl << m << endl << endl;

            //Show default behavior for non existing nodes
            cout << "Attempt to read NonExisting (should initialize the data structure with its default).";
            fs["NonExisting"] >> m;
            cout << endl << "NonExisting = " << endl << m << endl;
        }

        cout << endl
            << "Tip: Open up " << filename << " with a text editor to see the serialized data." << endl;
return 0;
}
           

PS:本人是用Qtcreator建立的程式。