OpenCV教程(42) xml/yaml文件的读写
参考资料:
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文件的读写的更多相关文章
- XML结构文件的读写
附件:http://files.cnblogs.com/xe2011/XML_Writer_And_Read.rar 下面这段代码实现了以下功能 数据保存 textBox1的文本,textBox2的文 ...
- OPENCV(3) —— 对XML和YAML文件实现I/O 操作
XML\YAML文件在OpenCV中的数据结构为FileStorage string filename = "I.xml"; FileStorage fs(filename, Fi ...
- 对XML和YAML文件实现I/O操作
1.文件的打开关闭 XML\YAML文件在OpenCV中的数据结构为FileStorage,打开操作例如: string filename = "I.xml"; FileStora ...
- Python基础笔记1-Python读写yaml文件(使用PyYAML库)
最近在搭建自动化测试项目过程中经常遇到yaml文件的读写,为了方便后续使用,决定记下笔记. 一,YAML 简介 YAML,Yet Another Markup Language的简写,通常用来编写项目 ...
- Python基础笔记2-ruamel.yaml读写yaml文件
上一篇笔记记录了Python中的pyyaml库对yaml文件进行读写,但了解到ruamel.yaml也能对yaml文件进行读写,于是想尝试一下它的用法. 一,注意 这里首先要更正一下网上大部分博客的说 ...
- Opencv探索之路(十九):读写xml和yml文件
有时候我们处理完图像后需要保存一下数据到文件上,以供下一步的处理.一个比较广泛的需求场景就是:我们对一幅图像进行特征提取之后,需要把特征点信息保存到文件上,以供后面的机器学习分类操作.那么如果遇到这样 ...
- Opencv YAML和XML格式文件操作详解
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/47660943 本文参考Opencv 2 ...
- opencv 3 core组件进阶(3 离散傅里叶变换;输入输出XML和YAML文件)
离散傅里叶变换 #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" ...
- OpenCV FileStorage类读写XML/YML文件
本文转自:http://www.cnblogs.com/summerRQ/articles/2524560.html 在OpenCV程序中,需要保存中间结果的时候常常会使用.xml / .yml文件, ...
随机推荐
- 使用setsid替代nohup
使用setsid替代nohup 使用 nohup 执行sudo -e时会有问题的.
- Xiaoguang Tu's Home Page
Xiaoguang Tu (涂晓光): CV: Ph.D. Candidate of School of Communication and Information Engineering, Univ ...
- thinkphp3.2链接数据库常用的配置选项
thinkphp3.2常用配置选项,app/Common/Conf/config.php 或者 app/Home/Conf/config.php 1 2 3 4 5 6 7 8 'SHOW_PAGE_ ...
- Pycharm 激活码(转) 有效期到2019/10月
Pycharm 激活码(转) 有效期到2019/10月 2018年11月13日 17:15:32 may_ths 阅读数:64 [激活码激活] 修改hosts文件 添加下面一行到hosts文件,目 ...
- sublime插件FileHeader使用,自动的添加模板
sublime插件FileHeader能够自动的监测创建新文件动作,自动的添加模板 下载地址:https://github.com/shiyanhui/FileHeader FileHeader能够自 ...
- 数据准备<4>:变量筛选-理论篇
在上一篇文章<数据准备<3>:数据预处理>中,我们提到降维主要包括两种方式:基于特征选择的降维和基于维度转换的降维,其中基于特征选择的降维通俗的讲就是特征筛选或者变量筛选,是指 ...
- [BZOJ5293][BJOI2018]求和(倍增)
裸的树上倍增. #include<cstdio> #include<cstring> #include<algorithm> #define rep(i,l,r) ...
- Luogu 4492 [HAOI2018]苹果树 组合数
https://www.luogu.org/problemnew/show/P4492 找每个编号的点的父边的贡献,组合数和阶乘就能算了. 我考场上怎么就是没想到呢. 调了好久好久好久好久调不出来,样 ...
- centos7 docker安装和使用_入门教程
说明:本文也是参考互联网上的文章写的,感谢相关作者的贡献. 操作系统 64位CentOS Linux release 7.2.1511 (Core) 配置好IP:192.168.1.160 修改yum ...
- 分频器VHDL描述
在数字电路中,常需要对较高频率的时钟进行分频操作,得到较低频率的时钟信号.我们知道,在硬件电路设计中时钟信号时非常重要的. 下面我们介绍分频器的VHDL描述,在源代码中完成对时钟信号CLK的2分 ...