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. [luogu] P1772 [ZJOI2006]物流运输(动态规划,最短路)

    P1772 [ZJOI2006]物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线 ...

  2. Unity 3D本地公布WebPlayer版时&quot;Failed to download data file&quot;解决方式

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlzZW55YW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  3. 更新 hadoop eclipse 插件

    卸载hadoop 1.1.2插件.并安装新版hadoop 2.2.0插件. 假设直接删除eclipse plugin文件夹下的hadoop 1.1.2插件,会导致hadoop 1.1.2插件残留在ec ...

  4. 【HDU-4614】Vases and Flowers(线段树双查询)

    11946317 2014-10-23 09:08:28 Accepted 4614 437MS 2348K rid=11946317" target="_blank" ...

  5. Android5.0以上系统的移动网络开关

    笔者近期遇到一个非常有意思的bug,贴出来和大家分享下. 那是一个温暖的早晨,阳光晒得人非常舒服.一封bug邮件像一片叶子飘到我的邮箱. 一番交流.笔者确认负责的Widget开关在Android5.0 ...

  6. poj_2481,Cows,树状数组

    将e按从大到小排序,统计前i-1个中比 #include<iostream> #include<cstdio> #include<cstring> #include ...

  7. pyspark.mllib.feature module

    Feature Extraction Feature Extraction converts vague features in the raw data into concrete numbers ...

  8. 4. idea常用快捷键设置(改为eclipse相似)

    转自:https://blog.csdn.net/loveer0/article/details/82697877 idea常用快捷键设置(改为eclipse相似) 目录 idea常用快捷键设置改为e ...

  9. Dictionary subtraction

    Finding the words from the book that are not in the word list from words.txt is a problem you might ...

  10. 安卓开发--HttpClient

    package com.zx.httpclient01; import android.app.Activity; import android.os.Bundle; import android.v ...