您的位置:首页 > 运维架构

OpenCV XML YAML 文件输入输出(部分翻译)

2013-03-27 10:22 489 查看
官方教程地址:

http://docs.opencv.org/doc/tutorials/core/file_input_output_with_xml_yml/file_input_output_with_xml_yml.html#fileinputoutputxmlyaml

转载自地址:

http://blog.njuaplusplus.com/file-input-output-with-xml-yml/

XML/YAML 文件打开关闭

FileStorage类描述XML/YAML数据结构。

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


其中第二个淡出用来描述操作类型:WRITE,READ 或者 APPEND

文件扩展名将决定输出文件的格式。当扩展名为 .xml.gz 的时候输出可能是压缩的。

当FileStorage对象销毁时,文件会自动关闭。也可以显示调用如下方法:

fs.release();                                       // explicit close


文本、数字的输入输出

输出使用<<操作符。首先需要制定数据名字。

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, arrays, maps的输入输出

首先制定名字,对于序列sequence,我们将其元素用[ ]限定。

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


对于maps,使用{ }限定。

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
f
4000
or (; it != it_end; ++it)
cout << (string)*it << endl;


对于maps,可以使用[]操作符或者使用>>操作符:

n = fs["Mapping"];                                // Read mappings from a sequence
cout << "Two  " << (int)(n["Two"]) << "; ";
cout << "One  " << (int)(n["One"]) << endl << endl;


自定义数据结构的输入输出

假设自定义如下数据结构

class MyData
{
public:
MyData() : A(0), X(0), id() {}
public:   // Data Members
int A;
double X;
string id;
};


只需要在自定义类内外添加read和write函数,即可通过OpenCV的接口来序列化。在类内部添加:

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"];
}


在类外部添加:

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);
}


在外部的read函数中,当空节点读入时,将使用默认值。也可以使用其他负值表示错误。

然后可以使用正常的<<和>>操作符来读写。

MyData m(1);
fs << "MyData" << m;                                // your own data structures
fs["MyData"] >> m;                                 // Read your own structure_


fs["NonExisting"] >> m;   // Do not add a fs << "NonExisting" << m command for this to work
cout << endl << "NonExisting = " << endl << m << endl;


转载自地址:

http://blog.njuaplusplus.com/file-input-output-with-xml-yml/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ OpenCV