XML\YAML文件在OpenCV中的数据结构为FileStorage

string filename = "I.xml";
FileStorage fs(filename, FileStorage::WRITE);
\\...
fs.open(filename, FileStorage::READ);

fs.release();

 

写入文件使用  <<  运算符 ,读取文件,使用 >> 运算符

fs << "iterationNr" << 100;

int itNr;
fs["iterationNr"] >> itNr;
itNr = (int) fs["iterationNr"];

 

OpenCV 数据结构的输入和输出

Mat R = Mat_<uchar >::eye (3, 3),
T = Mat_<double>::zeros(3, 1);
fs << "R" << R; // Write cv::Mat
fs << "T" << T;
fs["R"] >> R; // Read cv::Mat
fs["T"] >> T;

 

vector要注意在第一个元素前加上“[”,在最后一个元素前加上"]"

fs << "strings" << "["; // text - string sequence
fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
fs << "]"; // close sequence

对于map结构的操作使用的符号是"{"和"}"

fs << "Mapping"; // text - mapping
fs << "{" << "One" << 1;
fs << "Two" << 2 << "}";

 

读取这些结构的时候,会用到FileNode和FileNodeIterator数据结构。对FileStorage类的[]操作符会返回FileNode数据类型,对于一连串的node,可以使用FileNodeIterator结构

FileNode n = fs["strings"]; // Read string sequence - Get node
if (n.type() != FileNode::SEQ)
{
cerr << "strings is not a sequence! FAIL" << endl;
return 1;
}
FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node
for (; it != it_end; ++it)
cout << (string)*it << endl;

 

<?xml version="1.0"?>
<opencv_storage>
<depthImg190 type_id="opencv-image">
<width>320</width>
<height>240</height>
<origin>top-left</origin>
<layout>interleaved</layout>
<dt>w</dt>
<data>
0 0 0 0 27120 27384 27120 27120 27384 27120 27120 27120 27120 27384
27384 27664 27664 27944 27944 27664 27664 27944 27944 27944 28224
27944 27944 28224 28224 28224 28224 28520 28816 29120 29120 29120
29120 29120 29120 29120 29432 29744 30072 30072 29744 29744 30072
30072 30072 30400 30400 30736 30736 31080 31080 31080 31440 31440
31440 31440 31800 31800 31800 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 27120 27120 27120 27120 27384 27384 27384 27384 27384 27384
</depthImg190>
</opencv_storage>

这种格式不能直接使用xml,而需要

IplImage* depth=(IplImage*)cvLoad("depthImg190.xml");

cvLoad 函数

 

const char* filename = "zhang.jpg";

    std::ifstream file(filename);
std::vector<char> data; file >> std::noskipws; // noskipws 不忽略空白
std::copy(std::istream_iterator<char>(file), std::istream_iterator<char>(), std::back_inserter(data)); cv::Mat matrixJprg = cv::imdecode(cv::Mat(data), 1);
IplImage qImg;
qImg = IplImage(matrixJprg); // cv::Mat -> IplImage 可以直接强制转换 cvSaveImage("./out.jpg", &qImg);

cvSaveImage 直接把 cv::Mat 数据保存为图片会出现问题,需要以上的代码强制转换

OPENCV(3) —— 对XML和YAML文件实现I/O 操作的更多相关文章

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

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

  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——输入输出XML和YAML文件

  5. OpenCV 输入输出XML和YAML文件

    #include <opencv2/core/core.hpp> #include <iostream> #include <string> using names ...

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

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

  7. <学习opencv>图像、视频和数据文件

    /*=========================================================================*/ // openCV中的函数 /*====== ...

  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. 洛谷 P1005 矩阵取数游戏 (区间dp+高精度)

    这道题大部分时间都在弄高精度-- 还是先讲讲dp吧 这道题是一个区间dp,不过我还是第一次遇到这种类型的区间dp f[i][j]表示取了数之后剩下i到j这个区间的最优值 注意这里是取了i之前和j之后的 ...

  2. ztree实现根节点右击事件,显示添加删除

    需求,右击树节点,出现编辑和删除的提示框 1:在setting 配置里面,给callback设置,右击事件onRightClick: 2:写一个函数onRightClick function onRi ...

  3. C实现头插法和尾插法来构建单链表(带头结点)

    我在之前一篇博客<C实现头插法和尾插法来构建单链表(不带头结点)>中具体实现了怎样使用头插法和尾插法来建立一个不带头结点的单链表,可是在实际使用中.我们用的最多的还是带头结点的单链表.今天 ...

  4. ASP.NET Web开发技术的深入总结

    [IT168技术]在国内.Net开发这个环境里, 中小型公司.或者大公司但主营业务不是软件开发里面的软件小团队.针对.Net开发者的要求都是十项全能型的全才, 能做的了从前台页面展现到最后数据存储的全 ...

  5. less03 混合

    less //基本混合 .font_hn{ color: red; font-family: microsoft yahei, "黑体", Arial, Simsun, " ...

  6. emmet教程

    https://www.zfanw.com/blog/zencoding-vim-tutorial-chinese.html https://www.zfanw.com/blog/zencoding- ...

  7. hdoj--1556--Color the ball(模拟&&树状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. 为Activity生成桌面快捷方式

    有时候如果想让我们的应用在桌面上创建多个快捷方式,我们可以在Manifest.xml文件中对相应的activity进行声明. <application android:icon="@d ...

  9. Spring MVC模式示例(采用解耦控制器+校验器)

    Product package com.mstf.bean; import java.io.Serializable; /** * Product类,封装了一些信息,包含三个属性 * @author ...

  10. 工作日志:dispatch_once、网络缓存、分享问题

    问题描述一: 每一个接口对应的model的数据在进入对应的模块(视图)时,执行一次本地缓存加载: 执行缓存加载的实现在基类实现. 解决方案: 将dispatch_once_t设置为成员变量: 问题扩展 ...