OpenCV 输入输出XML和YAML文件
#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文件的更多相关文章
- OpenCV——输入输出XML和YAML文件
- opencv 3 core组件进阶(3 离散傅里叶变换;输入输出XML和YAML文件)
离散傅里叶变换 #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" ...
- OpenCV之XML和YAML文件读写
FileStorage类 该类有两个构造函数 FileStorage::FileStorage() FileStorage::FileStorage(const string& source, ...
- 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 ...
- OpenCV教程(42) xml/yaml文件的读写
参考资料: http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html #include "opencv2/openc ...
- OpenCV 读取.xml文件
OpenCV 只提供了读取和存储.xml和.yml 文件格式的函数. 读取.xml文件的C++例程如下: cv::FileStorage fs; //OpenCV 读XML文件流 cv::Mat De ...
- Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)
目录: shutil logging模块 shelve configparser subprocess xml处理 yaml处理 自定义模块 一,系统标准模块: 1.shutil:是一种高层次的文件操 ...
- 持续集成时 travis 和 codecov 等 yaml 文件的配置
最近在项目中在配置CodeCov 以及Travis 和 AppVeyor做持续集成时,遇到了一些问题,也解决了一些问题.顺便拿来分享一下. 首先时Travis,这个主要是来跑基于 Linux 环境下的 ...
随机推荐
- 4. react 基础 - 编写 todoList 功能
编写 TodoList 功能 react 入口 js #src/index.js import React from 'react'; import ReactDOM from 'react-dom' ...
- 函数的配置对象Configuration Objects
配置对象通常用在API库的实现中,当程序中需要编写要多次的模块,也可以采用这种模式.这种模式的好处是接口明确,扩展方便.比如,一个 addPerson在设计的最初需要两个参数作为初始化时人的姓名: f ...
- order by rand()优化
优化前: SELECT id, loan_id, NAME, company FROM tablename WHERE time BETWEEN 1522512000 AND 1525103999 A ...
- 将元素平分成差值最小的两个集合(DP)
现有若干物品,要分成较为平均的两部分,分的规则是这样的: 1)两部分物品的个数最多只能差一个. 2)每部分物品的权值总和必须要尽可能接近. 现在请你编写一个程序,给定现在有的物品的个数以及每个物品的权 ...
- LeetCode——324. 摆动排序 II
给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums = [1, 5 ...
- PAT Basic 完美数列(25) [two pointers]
题目 给定⼀个正整数数列,和正整数p,设这个数列中的最⼤值是M,最⼩值是m,如果M <= m * p,则称这个数列是完美数列.现在给定参数p和⼀些正整数,请你从中选择尽可能多的数构成⼀个完美数列 ...
- 用Matplotlib画三维图片的一个实例
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np from matp ...
- 别了JetBrains,换Visual Studio
Visual Studio一直是我排斥的,这么多年一致不用. 2019年JetBrains的注册码越来越频繁的被封杀,我承认使用盗版不对. 试过Eclipse+pydev搞python,但是todo用 ...
- 线性齐次递推式快速求第n项 学习笔记
定义 若数列 \(\{a_i\}\) 满足 \(a_n=\sum_{i=1}^kf_i \times a_{n-i}\) ,则该数列为 k 阶齐次线性递推数列 可以利用多项式的知识做到 \(O(k\l ...
- 6.windows-oracle实战第六课 --数据管理
数据库管理员: 每个oracle数据库应该至少有一个数据库管理员(dba),对于一个小的数据库,一个dba就够了,但是对于一个大的数据库可能需要多个dba分担不同的管理职责. 对于dba来说,会权限管 ...