OpenCV教程(42) xml/yaml文件的读写

简介: 参考资料: http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html #include "opencv2/opencv.

参考资料:
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&#x0a;"</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;
    }

读取的结果显示为:

image

程序代码:工程FirstOpenCV38

相关文章
|
15天前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
18天前
|
XML 监控 数据格式
ROS 2 - Python、XML 和 YAML 编写 Launch 文件
ROS 2 - Python、XML 和 YAML 编写 Launch 文件
69 0
|
3月前
|
XML Java 数据库
配置applicationContext.xml文件
配置applicationContext.xml文件
|
2月前
|
XML Java 数据库
配置applicationContext.xml文件
配置applicationContext.xml文件
|
3月前
|
XML JavaScript Java
解析XML文件的几种方法
解析XML文件的几种方法
|
3月前
|
XML 数据格式
XML配置Servlet文件,不使用注解配置路径的方法
XML配置Servlet文件,不使用注解配置路径的方法
|
2月前
|
XML Java 数据格式
支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
|
2月前
|
XML JavaScript Java
解析XML文件的几种方法
解析XML文件的几种方法
|
3月前
|
XML Java 数据处理
JSP 教程 之 JSP XML 数据处理 3
在JSP中处理XML涉及使用XSLT转换。`main.jsp`演示了如何结合JSTL `&lt;c:import&gt;` 和 `&lt;x:transform&gt;` 标签将内联XML数据转换为HTML。`style.xsl`定义了一个模板,将`&lt;books&gt;`元素转换为表格,显示书名、作者和价格。当`main.jsp`运行时,它导入XSL样式表并应用到XML数据上,生成一个格式化的书籍列表。
18 0
|
3月前
|
XML Java 数据格式
java删除xml文件内容
java删除xml文件内容
22 0

相关课程

更多
下一篇
云函数