- <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">Opencv 存储和读取XML文件使用 FileStorage 类,对于不同的数据结构,存储和读取的方式不同,下面结合例子具体分析下。</span>
头文件
- #include "opencv2/core/core.hpp"
- #include <iostream>
- #include <string>
- #include <vector>
存储XML的代码结构
- FileStorage fs(“name.xml”, FileStorage::WRITE); //创建XML文件
- if (!fs.isOpened())
- {
- cerr << "failed to open " << filename << endl;
- }
- /******************************/
- /* 存储数据 */
- /******************************/
- fs.release();
一、一般数据的存储
1. int型数据(float, string, float 等数据类型相同)
- int num;
- fs["Num"]<<num;
2. vector 数据
- vector<int> data;
- for(int i=0; i<5; i++)
- data.push_back(i);
- fs<<"VECTOR"<<"["; // 注意要有中括号
- for(vector<int>::iterator it =data.begin(); it!= data.begin(); it++)
- {
- fs<<(*it);
- }
- fs<<"]";
3. Map 数据读取
- map<string, int> data;
- data.insert(map<string,int>::value_type("one", 1));
- data.insert(map<string,int>::value_type("two", 2));
- fs<<"MAP"<<"{";//注意要用到大括号
- for(map<string,int>::iterator it = data.begin(); it!= data.end(); it++)
- {
- fs<<it->first<<it->second;
- }
- fs<<"}";
4. Opencv 中矩阵数据的存储
- Mat R = Mat_::eye(3,3);
- fs<<"Mat"<<R;
二、一般数据的读取
首先需要打开XML文件
- FileStorage fs("name.xml", FileStorage::READ);
- if (!fs.isOpened())
- {
- cerr << "failed to open " << filename << endl;
- }
1. int型数据(float, string, float 等数据类型相同)
- int Num = fs["Num"];
- //or
- fs["Num"]>>Num;
2. vector 数据
- FileNode n = fs["VECOTR"];
- if(n.type()!= FileNode::SEQ)
- {
- cerr<<"VECTOR is not a sequence! FAIL"<<endl;
- }
- for(FileNodeIterator it = n.begin(); it!=n.end(); it++)
- {
- cout<< *it<<endl;
- }
3. Map数据
- FileNode n = fs["MAP"];
- cout<<"one"<< (int)n["one"]<<endl;
- cout<<"two"<< (int)n["two"]<<endl;
4. Opencv 中矩阵数据的存储
- Mat R= fs["MAT"];
三、自定义数据的存储与读取
1、首先需要先定义一个数据结构
- class MyData{
- public:
- MyaData()A(0){ B.clear();}
- MyData(int k, Mat& m)
- {
- A =k;
- B = m;
- for(int i=0; i<A;++)
- {
- B.push_back(i);
- }
- }
- int A;
- vector<float> B;
- Mat R;
- // 定义存储函数,注意末尾处的const,缺少会出现错误
- void write(FileStorage &fs) const
- {
- fs<<"{";//必须要有大括号
- fs<<"A"<<A; //存储A
- fs<<"B"<<"["; // 存储B
- for(vector<float>::iterator it = B.begin(); it!=B.end(); it++)
- {
- fs<<(*it);
- }
- fs<<"]";
- fs<<"R"<<R; //存储R
- fs<<"}";
- }
- // 定义读取函数的数据
- void read( FileStorage & node)
- {
- // 读取A
- int A = node["A"];
- // 读取B
- FileNode n = node["B"];
- if(n.type()!=cv::FileNode::SEQ)
- {
- cerr<<"B is not a sequence! FAIL"<<endl;
- }
- for(cv::FileNodeIterator it = n.begin(); it!= n.end(); it++)
- cout<<(int)(*it)<<endl;
- // 读取R
- Mat R = node["R"];
- }
- };
2、定义完数据结构后需要重载两个函数
- static void write(FileStorage& fs, const std::string&, const MyData& x){
- x.write(fs);
- }
- static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){
- if(node.empty())
- x = default_value;
- else
- x.read(node);
- }
3. 对自定义的数据惊醒存储和读取
存储
- int num = 2;
- Mat R = Mat_::eye(3,3);
- MyData mydata(2, R);
- fs<<"MYDATA"<<mydata;
读取
- MyData *mydata = new MyData();
- fs["MYDATA"]>> (*mydata);