#include <opencv2/core/core.hpp>
#include <iostream>
#include <string> using namespace cv;
using namespace std; class MyData
{
public:
MyData() : A(), X(), id()
{}
explicit MyData(int) : A(), X(CV_PI), id("mydata1234") // explicit to avoid implicit conversion
{}
void write(FileStorage& fs) const //Write serialization for this class
{
fs << "{" << "A" << A << "X" << X << "id" << id << "}";
}
void read(const FileNode& node) //Read serialization for this class
{
A = (int)node["A"];
X = (double)node["X"];
id = (string)node["id"];
}
public: // Data Members
int A;
double X;
string id;
}; //These write and read functions must be defined for the serialization in FileStorage to work
void write(FileStorage& fs, const std::string&, const MyData& x)
{
x.write(fs);
}
void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){
if(node.empty())
x = default_value;
else
x.read(node);
} // This function will print our custom class to the console
ostream& operator<<(ostream& out, const MyData& m)
{
out << "{ id = " << m.id << ", ";
out << "X = " << m.X << ", ";
out << "A = " << m.A << "}";
return out;
} int main(int ac, char** av)
{
if (ac != )
{
help(av);
return ;
} string filename = av[];
{ //write
Mat R = Mat_<uchar>::eye(, ),
T = Mat_<double>::zeros(, );
MyData m(); FileStorage fs(filename, FileStorage::WRITE); fs << "iterationNr" << ;
fs << "strings" << "["; // text - string sequence
fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
fs << "]"; // close sequence fs << "Mapping"; // text - mapping
fs << "{" << "One" << ;
fs << "Two" << << "}"; fs << "R" << R; // cv::Mat
fs << "T" << T; fs << "MyData" << m; // your own data structures fs.release(); // explicit close
cout << "Write Done." << endl;
} {//read
cout << endl << "Reading: " << endl;
FileStorage fs;
fs.open(filename, FileStorage::READ); int itNr;
//fs["iterationNr"] >> itNr;
itNr = (int) fs["iterationNr"];
cout << itNr;
if (!fs.isOpened())
{
cerr << "Failed to open " << filename << endl;
help(av);
return ;
} FileNode n = fs["strings"]; // Read string sequence - Get node
if (n.type() != FileNode::SEQ)
{
cerr << "strings is not a sequence! FAIL" << endl;
return ;
} FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node
for (; it != it_end; ++it)
cout << (string)*it << endl; n = fs["Mapping"]; // Read mappings from a sequence
cout << "Two " << (int)(n["Two"]) << "; ";
cout << "One " << (int)(n["One"]) << endl << endl; MyData m;
Mat R, T; fs["R"] >> R; // Read cv::Mat
fs["T"] >> T;
fs["MyData"] >> m; // Read your own structure_ cout << endl
<< "R = " << R << endl;
cout << "T = " << T << endl << endl;
cout << "MyData = " << endl << m << endl << endl; //Show default behavior for non existing nodes
cout << "Attempt to read NonExisting (should initialize the data structure with its default).";
fs["NonExisting"] >> m;
cout << endl << "NonExisting = " << endl << m << endl;
} cout << endl
<< "Tip: Open up " << filename << " with a text editor to see the serialized data." << endl; return ;
}

OpenCV 输入输出XML和YAML文件的更多相关文章

  1. OpenCV——输入输出XML和YAML文件

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

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

  3. OpenCV之XML和YAML文件读写

    FileStorage类 该类有两个构造函数 FileStorage::FileStorage() FileStorage::FileStorage(const string& source, ...

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

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

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

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

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

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

  7. OpenCV 读取.xml文件

    OpenCV 只提供了读取和存储.xml和.yml 文件格式的函数. 读取.xml文件的C++例程如下: cv::FileStorage fs; //OpenCV 读XML文件流 cv::Mat De ...

  8. Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)

    目录: shutil logging模块 shelve configparser subprocess xml处理 yaml处理 自定义模块 一,系统标准模块: 1.shutil:是一种高层次的文件操 ...

  9. 持续集成时 travis 和 codecov 等 yaml 文件的配置

    最近在项目中在配置CodeCov 以及Travis 和 AppVeyor做持续集成时,遇到了一些问题,也解决了一些问题.顺便拿来分享一下. 首先时Travis,这个主要是来跑基于 Linux 环境下的 ...

随机推荐

  1. Linux--Shell传递参数

    参考:http://www.runoob.com/linux/linux-shell-passing-arguments.html

  2. Flink(四) —— 数据流编程模型

    分层抽象 The lowest level abstraction simply offers stateful streaming. It is embedded into the DataStre ...

  3. android studio compile api implementation 区别

    compile与api 二者等同,无区别 implementation与compile或implementation与api implementation编译的依赖只作用于当前的module.即APP ...

  4. 埃拉托色尼(Eratosthenes)筛法

     用筛选法求1—100之内的素数(此法难度的话,方法可以不界定:能完成求1—100之内的素数即可).   在一张纸上写上1到100全部整数,然后逐个判断它们是否是素数,找出一个非素数,就把它挖掉,最后 ...

  5. js运算符相关要点

    取模运算的结果符号只与左边值的符号有关: var x = 7 % 3; // 结果为 1 var y = 7 % (-3); // 结果为 1 var z = (-7) % 3; // 结果为 -1

  6. GCC与gcc,g++区别

    看的Linux公社的一篇文章,觉得不错,内容复制过来了. 其实在这之前,我一直以为gcc和g++是一个东西,只是有两个不同的名字而已,今天在linux下编译一个c代码时出现了错误才找了一下gcc和g+ ...

  7. 4. 监控利器nagios手把手企业级实战第三部

    1.nagios图形监控显示和管理服务器 虽然能显示,能报警.但是我们企业工作中需要一个历史趋势图. nagios只开放核心,插件是单独的形式,图像也一样,是插件或者整合的方式.所以可能看起来很多,这 ...

  8. kaggle——分销商产品未来销售情况预测

    分销商产品未来销售情况预测 介绍 前面的几个实验中,都是根据提供的数据特征来构建模型,也就是说,数据集中会含有许多的特征列.本次将会介绍如何去处理另一种常见的数据,即时间序列数据.具体来说就是如何根据 ...

  9. python基础——认识(if __name__ == ‘__main__’:)

    我们在写代码时,经常会用到这一句:if __name__ == '__main__',那么加这一句有什么用呢?实际上,它起到到了一个代码保护功能,它能够让别人在导入你写的模块情况下,无法看到和运行if ...

  10. ubuntu14 中文显示问号

    1 终端执行 sudo locale-gen zh_CN.UTF-8 2 终端执行 export LC_ALL=zh_CN.UTF-8