opencv下XML 文件的读写 很全滴

34 篇文章 3 订阅
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">Opencv 存储和读取XML文件使用 FileStorage 类,对于不同的数据结构,存储和读取的方式不同,下面结合例子具体分析下。</span>  

头文件

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "opencv2/core/core.hpp"  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <vector>  

存储XML的代码结构

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  FileStorage fs(“name.xml”, FileStorage::WRITE); //创建XML文件  
  2.   
  3.   if (!fs.isOpened())  
  4.   {  
  5.     cerr << "failed to open " << filename << endl;  
  6.   }  
  7.  /******************************/  
  8.  /*  存储数据                 */  
  9.  /******************************/  
  10. fs.release();  

一、一般数据的存储

1. int型数据(float, string, float 等数据类型相同)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int num;  
  2. fs["Num"]<<num;  

2. vector 数据

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. vector<int> data;  
  2. for(int i=0; i<5; i++)  
  3. data.push_back(i);  
  4.   
  5. fs<<"VECTOR"<<"["// 注意要有中括号  
  6. for(vector<int>::iterator it =data.begin(); it!= data.begin(); it++)  
  7. {  
  8.   fs<<(*it);  
  9. }  
  10. fs<<"]";  

3. Map 数据读取

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. map<string, int> data;  
  2. data.insert(map<string,int>::value_type("one", 1));  
  3. data.insert(map<string,int>::value_type("two", 2));  
  4.   
  5. fs<<"MAP"<<"{";//注意要用到大括号  
  6. for(map<string,int>::iterator it = data.begin(); it!= data.end(); it++)  
  7. {  
  8.    fs<<it->first<<it->second;  
  9. }  
  10. fs<<"}";  

4. Opencv 中矩阵数据的存储

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Mat R = Mat_::eye(3,3);  
  2. fs<<"Mat"<<R;  

二、一般数据的读取


首先需要打开XML文件

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. FileStorage fs("name.xml", FileStorage::READ);  
  2.   
  3.    if (!fs.isOpened())  
  4.    {  
  5.      cerr << "failed to open " << filename << endl;  
  6.    }  

1. int型数据(float, string, float 等数据类型相同)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int Num = fs["Num"];  
  2. //or  
  3. fs["Num"]>>Num;  

2. vector 数据

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. FileNode n = fs["VECOTR"];  
  2. if(n.type()!= FileNode::SEQ)  
  3. {  
  4.   cerr<<"VECTOR is not a sequence! FAIL"<<endl;  
  5. }  
  6. for(FileNodeIterator it = n.begin(); it!=n.end(); it++)  
  7. {  
  8.   cout<< *it<<endl;  
  9. }  

3. Map数据

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. FileNode n = fs["MAP"];  
  2. cout<<"one"<< (int)n["one"]<<endl;  
  3. cout<<"two"<< (int)n["two"]<<endl;  

 4. Opencv 中矩阵数据的存储

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Mat R= fs["MAT"];  


三、自定义数据的存储与读取

1、首先需要先定义一个数据结构

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class MyData{  
  2.  public:  
  3.    MyaData()A(0){ B.clear();}  
  4.    MyData(int k, Mat& m)  
  5.    {  
  6.       A =k;  
  7.       B = m;  
  8.       for(int i=0; i<A;++)  
  9.       {  
  10.          B.push_back(i);  
  11.       }  
  12.    }  
  13.   
  14.   int  A;  
  15.   vector<float> B;  
  16.   Mat R;  
  17.     
  18.   // 定义存储函数,注意末尾处的const,缺少会出现错误  
  19.   void write(FileStorage &fs) const  
  20.   {  
  21.     fs<<"{";//必须要有大括号  
  22.     fs<<"A"<<A; //存储A  
  23.     fs<<"B"<<"["// 存储B  
  24.     for(vector<float>::iterator it = B.begin(); it!=B.end(); it++)  
  25.      {   
  26.        fs<<(*it);  
  27.      }  
  28.     fs<<"]";  
  29.     fs<<"R"<<R; //存储R  
  30.     fs<<"}";  
  31.   }   
  32.  // 定义读取函数的数据  
  33.  void read( FileStorage & node)  
  34.  {  
  35.      // 读取A  
  36.      int A = node["A"];  
  37.       
  38.      // 读取B  
  39.      FileNode n = node["B"];  
  40.        
  41.     if(n.type()!=cv::FileNode::SEQ)  
  42.     {  
  43.         cerr<<"B is not a sequence! FAIL"<<endl;  
  44.     }  
  45.       
  46.   for(cv::FileNodeIterator it = n.begin(); it!= n.end(); it++)  
  47.         cout<<(int)(*it)<<endl;  
  48.      
  49.     // 读取R  
  50.     Mat R = node["R"];  
  51.   
  52.  }  
  53.   
  54. };  

2、定义完数据结构后需要重载两个函数

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static void write(FileStorage& fs, const std::string&, const MyData& x){  
  2.   x.write(fs);  
  3. }  
  4. static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){  
  5.   if(node.empty())  
  6.     x = default_value;  
  7.   else  
  8.     x.read(node);  
  9. }  

3. 对自定义的数据惊醒存储和读取

存储

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int num = 2;  
  2. Mat R = Mat_::eye(3,3);  
  3. MyData  mydata(2, R);  
  4. fs<<"MYDATA"<<mydata;  

读取

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. MyData *mydata = new MyData();  
  2. fs["MYDATA"]>> (*mydata);  



  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值