参考资料:
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

OpenCV教程(42) xml/yaml文件的读写的更多相关文章

  1. XML结构文件的读写

    附件:http://files.cnblogs.com/xe2011/XML_Writer_And_Read.rar 下面这段代码实现了以下功能 数据保存 textBox1的文本,textBox2的文 ...

  2. OPENCV(3) —— 对XML和YAML文件实现I/O 操作

    XML\YAML文件在OpenCV中的数据结构为FileStorage string filename = "I.xml"; FileStorage fs(filename, Fi ...

  3. 对XML和YAML文件实现I/O操作

    1.文件的打开关闭 XML\YAML文件在OpenCV中的数据结构为FileStorage,打开操作例如: string filename = "I.xml"; FileStora ...

  4. Python基础笔记1-Python读写yaml文件(使用PyYAML库)

    最近在搭建自动化测试项目过程中经常遇到yaml文件的读写,为了方便后续使用,决定记下笔记. 一,YAML 简介 YAML,Yet Another Markup Language的简写,通常用来编写项目 ...

  5. Python基础笔记2-ruamel.yaml读写yaml文件

    上一篇笔记记录了Python中的pyyaml库对yaml文件进行读写,但了解到ruamel.yaml也能对yaml文件进行读写,于是想尝试一下它的用法. 一,注意 这里首先要更正一下网上大部分博客的说 ...

  6. Opencv探索之路(十九):读写xml和yml文件

    有时候我们处理完图像后需要保存一下数据到文件上,以供下一步的处理.一个比较广泛的需求场景就是:我们对一幅图像进行特征提取之后,需要把特征点信息保存到文件上,以供后面的机器学习分类操作.那么如果遇到这样 ...

  7. Opencv YAML和XML格式文件操作详解

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/47660943 本文参考Opencv 2 ...

  8. opencv 3 core组件进阶(3 离散傅里叶变换;输入输出XML和YAML文件)

    离散傅里叶变换 #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" ...

  9. OpenCV FileStorage类读写XML/YML文件

    本文转自:http://www.cnblogs.com/summerRQ/articles/2524560.html 在OpenCV程序中,需要保存中间结果的时候常常会使用.xml / .yml文件, ...

随机推荐

  1. ionic启动App时不使用淡入淡出效果

    找到我们项目下面 res下面的config.xml,然后加入下面两句就可以解决. <preference name="FadeSplashScreen" value=&quo ...

  2. Hadoop整理三(Hadoop分布式计算框架MapReduce)

    一.概念 MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想.它极大 ...

  3. JFinal 3.3 学习 -- JFinalConfig (配置web项目)

    开篇  概述 基于JFinal的web项目需要创建一个继承自JFinalConfig类的子类,该类用于对整个web项目进行配置. JFinalConfig子类需要实现六个抽象方法,如下所示: publ ...

  4. CentOS重装grub修复损坏的系统

    grub损坏一般有两种情况:第一.安装双系统时,后安装的系统把先安装的系统的MBR删除了.第二.误操作将grub文件删除了. 不管怎样都需要进入到救援模式,详细请看CentOS通过光盘启动救援数据 ( ...

  5. Sting.format字符串格式化

    控制格式scanf printf 也不知道为什么=-= 越研究深层的java就越感觉它是从别的语言那抄袭来的

  6. [leetcode tree]104. Maximum Depth of Binary Tree

    求树的最大深度 class Solution(object): def maxDepth(self, root): if not root: return 0 left = self.maxDepth ...

  7. vdp配置

    转:http://jiangjianlong.blog.51cto.com/3735273/1902879 本文将介绍VDP 6.1.2的部署与配置,主要内容包括部署VDP的OVA模板.初始化配置VD ...

  8. Codeforces Round #279 (Div. 2) A. Team Olympiad 水题

    #include<stdio.h> #include<iostream> #include<memory.h> #include<math.h> usi ...

  9. SMACH(五)----用户数据UserData类和重映射Remapper类的原理和例子

    用户数据UserData类和重映射Remapper类包含在smach中的user_data.py文件中实现,该博文主要介绍其原理和例子 UserData主要用于状态之间的数据传递,包括数据的输入inp ...

  10. 电子数据识别软件ABBYY FineReader

    ABBYY 是一家俄罗斯软件公司,在文档识别,数据捕获和语言技术的开发中居世界领先地位.其获奖产品 FineReader OCR 软件可以把静态纸文件和 PDF 文件转换成可管理的电子数据,可以大大节 ...