OpenCV笔记(一)-XML文件读写

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

OpenCV笔记(一)-XML文件读写

平台:ubuntu10.04 + OpenCV2.3 + cmake

1. XML、YAML文件的打开和关闭

XML\YAML文件在OpenCV中的数据结构为FileStorage,打开操作例如:

string filename = "test.xml";

FileStorage fs(filename, FileStorage::WRITE);

\\或者

fs.open(filename, FileStorage::WRITE);

// 文件读写模式

// file storage mode

enum

{

READ=0, //! read mode

WRITE=1, //! write mode

APPEND=2 //! append mode

};

文件关闭操作会在FileStorage结构销毁时自动进行,但也可调用如下函数实现fs.release();

2.文本和数字的输入和输出

写入文件使用<< 运算符,例如:

fs << "iterationNr" << 100;

读取文件,使用>> 运算符,例如

int itNr;

fs["iterationNr"] >> itNr;

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

3. OpenCV数据结构的输入和输出,和基本的C++形式相同

Mat R = Mat_::eye (3, 3),

T = Mat_::zeros(3, 1);

fs << "R" << R; // Write cv::Mat

fs << "T" << T;

fs["R"] >> R; // Read cv::Mat

fs["T"] >> T;

4. vector(arrays)和maps的输入和输出

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;

//!FileNode的类型

//! type of the file storage node

enum

{

NONE=0, //!< empty node

INT=1, //!< an integer

REAL=2, //!< floating-point number

FLOAT=REAL, //!< synonym or REAL

STR=3, //!< text string in UTF-8 encoding

STRING=STR, //!< synonym for STR

REF=4, //!< integer of size size_t. Typically used for storing complex dynamic structures where some elements reference the others

SEQ=5, //!< sequence

MAP=6, //!< mapping

TYPE_MASK=7,

FLOW=8, //!< compact representation of a sequence or mapping. Used only by YAML writer

USER=16, //!< a registered object (e.g. a matrix)

EMPTY=32, //!< empty structure (sequence or mapping)

NAMED=64 //!< the node has a name (i.e. it is element of a mapping) };

5. 文件读写源码

### xml2.cpp

############################################################################# // XML/YAML File Storage Class.

//

// The class describes an object associated with XML or Y AML file.

// It can be used to store data to such a file or read and decode the data.

//

// The storage is organized as a tree of nested sequences (or lists) and mappings.

// Sequence is a heterogenious array, which elements are accessed by indices or sequentially using an iterator.

// Mapping is analogue of std::map or C structure, which elements are accessed by names.

// The most top level structure is a mapping.

// Leaves of the file storage tree are integers, floating-point numbers and text strings.

//

// For example, the following code:

#include"opencv2/core/core.hpp"

#include

#include

using namespace std;

using namespace cv;

int main(int agc, char** agv)

{

// open file storage for writing. Type of the file is determined from the extension

{

FileStorage fs("test.xml", FileStorage::WRITE);

fs << "test_int" << 5 << "test_real" << 3.1 << "test_string" << "ABCDEFGH";

fs << "test_mat" << Mat::eye(3,3,CV_32F);

fs << "test_list" << "[" << 0.0000000000001 << 2 << CV_PI << -3435345 << "2-502 2-029 3egegeg" <<

"{:" << "month" << 12 << "day" << 31 << "year" << 1969 << "}" << "]";

相关文档
最新文档