参考资料:
http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html

#include "opencv2/opencv.hpp"
#include <time.h>

using namespace cv;
using namespace std;

int main(int, char** argv)
    {
    //打开yml文件,用来写入
    FileStorage fs("test.yml", FileStorage::WRITE);

    fs << "frameCount" << 5;
    time_t rawtime;
    time(&rawtime);
    fs << "calibrationDate" << asctime(localtime(&rawtime));
    Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
    Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
    fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
    fs << "features" << "[";
    for( int i = 0; i < 3; i++ )
        {
        int x = rand() % 640;
        int y = rand() % 480;
        uchar lbp = rand() % 256;

        fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
        for( int j = 0; j < 8; j++ )
            fs << ((lbp >> j) & 1);
        fs << "]" << "}";
        }
    fs << "]";
    fs.release();

通过上面的代码,我们可以把文件写入到yaml或xml文件中,写入ymal文件的格式为:

%YAML:1.0
frameCount: 5
calibrationDate: "Sat Nov 30 16:49:41 2013\n"
cameraMatrix: !!opencv-matrix
   rows: 3
   cols: 3
   dt: d
   data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
distCoeffs: !!opencv-matrix
   rows: 5
   cols: 1
   dt: d
   data: [ 1.0000000000000001e-001, 1.0000000000000000e-002,
       -1.0000000000000000e-003, 0., 0. ]
features:
   - { x:41, y:227, lbp:[ 0, 1, 1, 1, 1, 1, 0, 1 ] }
   - { x:260, y:449, lbp:[ 0, 0, 1, 1, 0, 1, 1, 0 ] }
   - { x:598, y:78, lbp:[ 0, 1, 0, 0, 1, 0, 1, 0 ]

写入xml文件后的格式为:

<?xml version="1.0"?>
<opencv_storage>
<frameCount>5</frameCount>
<calibrationDate>"Sat Nov 30 16:50:54 2013 "</calibrationDate>
<cameraMatrix type_id="opencv-matrix">
  <rows>3</rows>
  <cols>3</cols>
  <dt>d</dt>
  <data>
    1000. 0. 320. 0. 1000. 240. 0. 0. 1.</data></cameraMatrix>
<distCoeffs type_id="opencv-matrix">
  <rows>5</rows>
  <cols>1</cols>
  <dt>d</dt>
  <data>
    1.0000000000000001e-001 1.0000000000000000e-002
    -1.0000000000000000e-003 0. 0.</data></distCoeffs>
<features>
  <_><x>41</x>
    <y>227</y>
    <lbp>
      0 1 1 1 1 1 0 1</lbp></_>
  <_><x>260</x>
    <y>449</y>
    <lbp>
      0 0 1 1 0 1 1 0</lbp></_>
  <_><x>598</x>
    <y>78</y>
    <lbp>
      0 1 0 0 1 0 1 0</lbp></_></features>
</opencv_storage>

 

下面的代码用来读取xml或者yaml文件:

    FileStorage fs2("test.yml", FileStorage::READ);

   // 得到xml/yml文件中的信息,第一种方法:用type数组
    int frameCount = (int)fs2["frameCount"];

    std::string date;
   // 第二种方法:用文件操作符 >>
    fs2["calibrationDate"] >> date;

    Mat cameraMatrix2, distCoeffs2;
    fs2["cameraMatrix"] >> cameraMatrix2;
    fs2["distCoeffs"] >> distCoeffs2;

    cout << "frameCount: " << frameCount << endl
        << "calibration date: " << date << endl
        << "camera matrix: " << cameraMatrix2 << endl
        << "distortion coeffs: " << distCoeffs2 << endl;

    FileNode features = fs2["features"];
    FileNodeIterator it = features.begin(), it_end = features.end();
    int idx = 0;
    std::vector<uchar> lbpval;

    //用文件节点迭代器
    for( ; it != it_end; ++it, idx++ )
        {
        cout << "feature #" << idx << ": ";
        cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";

        (*it)["lbp"] >> lbpval;
        for( int i = 0; i < (int)lbpval.size(); i++ )
            cout << " " << (int)lbpval[i];
        cout << ")" << endl;
        }
    fs2.release();
    return 0;
    }

读取的结果显示为:

程序代码:工程FirstOpenCV38

最新文章

  1. Cucumber测试驱动开发
  2. C/C++链表操作(面试)
  3. ripple
  4. Android Studio 调试过程中快捷查看断点处变量值(Ctrl+Shift+I无效)?
  5. C#版-百度网盘API的实现(二)
  6. is,as,sizeof,typeof,GetType
  7. sql server日期字段值的比较
  8. 统计知识选讲(二)——主成分分析(PCA)的推导和应用
  9. Java基础知识思维导图
  10. 使用python实现深度神经网络 4(转)
  11. oracle11g客户端配置及使用(Windows系统)
  12. 6月16 ThinkPHP连接数据库及Model数据模型层--------查询及数据添加
  13. html &lt;iframe&gt;介绍
  14. express返回html文件
  15. nodeclub
  16. ubuntu 下 python模块 mysql-python安装(转)
  17. maven仓库介绍 牛人博客
  18. 字符设备驱动笔记——中断方式按键驱动之linux中断处理结构(五)
  19. unity对象物体闪烁
  20. 使用android studio检测app内存泄漏【转载】

热门文章

  1. 安卓代码混淆(Android Studio)
  2. 文件系统层级结构标准(FHS)
  3. ARM 寄存器
  4. php最简单最基础入门笔记
  5. 最大子段和问题Java实现
  6. BZOJ 1449: [JSOI2009]球队收益 最小费用最大流 网络流
  7. [POI2000]Repetitions
  8. Git 简易使用指南及补充
  9. [Visual Studio] 安装清单
  10. GIT(1)----更新代码和上传代码操作的步骤