opencv(二):FileStorage类

opencv FileStorage类

使用这个工具类,我们可以将 opencv 中的数据结构(或者int,float,string)保存到 XML/YAML 文件中去。或者从XML/YAML文件中加载这些数据。

这个类的声明在 “opencv2\core.hpp” 头文件中

保存

代码一:

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

fs <<"name" << "keithyin";
fs<< "age"<<18;
//等价于
fs <<"name" << "keithyim"<<"age"<<18;

fs.release();
<?xml version="1.0"?>
<opencv_storage>
<name>keithyim</name>
<age>18</age>
</opencv_storage>

代码二:

FileStorage fs("test.xml", FileStorage::WRITE);
fs <<"name" << "[" <<"first name"<<"keith" <<"last name"<<"yin"<<"]";
fs.release();
<?xml version="1.0"?>
<opencv_storage>
<name>
  "first name" keith "last name" yin
</name>
</opencv_storage>

看出有”[]” 和 没 “[]”的不同之处了吗。”[]”表示里面的东西是个序列 string

代码三:

FileStorage fs("test.xml", FileStorage::WRITE);
    fs <<"name" << "{" <<"first_name"<<"keith" <<"last_name"<<"yin"<<"}";
    fs.release();
<?xml version="1.0"?>
<opencv_storage>
<name>
  <first_name>keith</first_name>
  <last_name>yin</last_name></name>
</opencv_storage>

{}表示里面的东西还是键值对。

读取

FileStorage fs("test.xml", FileStorage::READ);
string name;
fs["name"]["first_name"] >> name;
cout << name << endl;
fs.release();
keith

这样就读出来了。

存储Mat类型数据

void test(){
    FileStorage fs("test.xml", FileStorage::WRITE);
    Mat mat(2, 2, CV_8UC3);
    fs << "mat_val" << mat;
    fs.release();
}

test.xml中数据的内容是:

<?xml version="1.0"?>
<opencv_storage>
<mat_val type_id="opencv-matrix">
  <rows>2</rows>
  <cols>2</cols>
  <dt>"3u"</dt>
  <data>
    205 205 205 205 205 205 205 205 205 205 205 205</data></mat_val>
</opencv_storage>

读取保存的Mat类型数据

void test(){
    FileStorage fs("test.xml", FileStorage::READ);
    Mat mat;
    fs["mat_val"] >> mat;
    cout << mat << endl;
    fs.release();
}
[205, 205, 205, 205, 205, 205;
  205, 205, 205, 205, 205, 205]

参考资料

http://www.cnblogs.com/fourseas/p/5519681.html

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值