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

官方教程地址:

转载自地址:

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
for (; 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;
转载自地址:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值