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

相关文章
|
24天前
|
计算机视觉 数据格式
使用opencv在Qt控件上播放mp4文件
使用opencv在Qt控件上播放mp4文件
28 2
|
2月前
|
XML JavaScript API
Ruby 教程 之 Ruby XML, XSLT 和 XPath 教程 3
Ruby XML, XSLT 和 XPath 教程
33 1
|
2月前
|
XML JavaScript API
Ruby 教程 之 Ruby XML, XSLT 和 XPath 教程 2
Ruby XML, XSLT 和 XPath 教程
26 0
|
3月前
|
存储 IDE 测试技术
玩转数据处理利器:学会使用 YAML 文件轻松处理数据
玩转数据处理利器:学会使用 YAML 文件轻松处理数据
56 0
|
2月前
|
机器学习/深度学习 数据可视化 计算机视觉
YOLOv5改进 | 2023Neck篇 | 轻量级跨尺度特征融合模块CCFM(附yaml文件+添加教程)
YOLOv5改进 | 2023Neck篇 | 轻量级跨尺度特征融合模块CCFM(附yaml文件+添加教程)
146 1
|
1月前
|
XML 安全 API
Python读写XML文件:深入解析与技术实现
Python读写XML文件:深入解析与技术实现
51 0
|
2月前
|
Shell Go
go 编辑yaml 文件
在Go语言中编辑YAML文件通常涉及以下步骤: 1. 读取YAML文件内容到字符串。 2. 使用YAML解析库(如`gopkg.in/yaml.v2`)将字符串解析为Go数据结构(如`map[string]interface{}`或自定义的结构体)。 3. 修改数据结构中的值以更新YAML内容。 4. 将修改后的数据结构编码回YAML格式的字符串。 5. 将字符串写回到YAML文件。 以下是一个简单的例子,展示了如何使用`gopkg.in/yaml.v2`库来编辑YAML文件: 首先,确保你已经安装了`gopkg.in/yaml.v2`包: ```bash go get gopkg.i
|
2月前
|
编解码 文件存储 计算机视觉
YOLOv5改进 | 2023Neck篇 | BiFPN双向特征金字塔网络(附yaml文件+代码)
YOLOv5改进 | 2023Neck篇 | BiFPN双向特征金字塔网络(附yaml文件+代码)
214 0
|
2月前
|
XML 数据格式 Ruby
Ruby 教程 之 Ruby XML, XSLT 和 XPath 教程 5
Ruby XML, XSLT 和 XPath 教程
93 0
|
2月前
|
XML 数据格式 Ruby
Ruby 教程 之 Ruby XML, XSLT 和 XPath 教程 4
Ruby XML, XSLT 和 XPath 教程
110 1

相关课程

更多