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

前端之家收集整理的这篇文章主要介绍了Opencv对XML和YAML文件实现I/O操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. XML、YAML文件的打开和关闭
XML\YAML文件在OpenCV中的数据结构为FileStorage,打开操作例如:
  1. string filename = "I.xml";
  2. FileStorage fs(filename,FileStorage::WRITE);
  3. \\...
  4. fs.open(filename,FileStorage::READ);


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

  1. fs.release();

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

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

  1. fs << "iterationNr" << 100;

读取文件,使用 >> 运算符,例如
  1. int itNr;
  2. fs["iterationNr"] >> itNr;
  3. itNr = (int) fs["iterationNr"];
3. OpenCV数据结构的输入和输出,和基本的C++形式相同

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

4. vector(arrays) 和 maps的输入和输出
vector要注意在第一个元素前加上“[”,在最后一个元素前加上"]"。例如:

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

对于map结构的操作使用的符号是"{"和"}",例如:
  1. fs << "Mapping"; // text - mapping
  2. fs << "{" << "One" << 1;
  3. fs << "Two" << 2 << "}";

读取这些结构的时候,会用到FileNode和FileNodeIterator数据结构。对FileStorage类的[]操作符会返回FileNode数据类型,对于一连串的node,可以使用FileNodeIterator结构,例如:
  1. FileNode n = fs["strings"]; // Read string sequence - Get node
  2. if (n.type() != FileNode::SEQ)
  3. {
  4. cerr << "strings is not a sequence! FAIL" << endl;
  5. return 1;
  6. }
  7. FileNodeIterator it = n.begin(),it_end = n.end(); // Go through the node
  8. for (; it != it_end; ++it)
  9. cout << (string)*it << endl;


5. 读写自己的数据结构
这部分比较复杂,参考最后的实例中的MyData结构自己领悟吧

最后,我这里上一个实例,供大家参考。

文件里填入如下代码

  1. #include <opencv2/core/core.hpp>
  2. #include <iostream>
  3. #include <string>
  4. using namespace cv;
  5. using namespace std;
  6. void help(char** av)
  7. {
  8. cout << endl
  9. << av[0] << " shows the usage of the OpenCV serialization functionality." << endl
  10. << "usage: " << endl
  11. << av[0] << " outputfile.yml.gz" << endl
  12. << "The output file may be either XML (xml) or YAML (yml/yaml). You can even compress it by "
  13. << "specifying this in its extension like xml.gz yaml.gz etc... " << endl
  14. << "With FileStorage you can serialize objects in OpenCV by using the << and >> operators" << endl
  15. << "For example: - create a class and have it serialized" << endl
  16. << " - use it to read and write matrices." << endl;
  17. }
  18. class MyData
  19. {
  20. public:
  21. MyData() : A(0),X(0),id()
  22. {}
  23. explicit MyData(int) : A(97),X(CV_PI),id("mydata1234") // explicit to avoid implicit conversion
  24. {}
  25. void write(FileStorage& fs) const //Write serialization for this class
  26. {
  27. fs << "{" << "A" << A << "X" << X << "id" << id << "}";
  28. }
  29. void read(const FileNode& node) //Read serialization for this class
  30. {
  31. A = (int)node["A"];
  32. X = (double)node["X"];
  33. id = (string)node["id"];
  34. }
  35. public: // Data Members
  36. int A;
  37. double X;
  38. string id;
  39. };
  40. //These write and read functions must be defined for the serialization in FileStorage to work
  41. void write(FileStorage& fs,const std::string&,const MyData& x)
  42. {
  43. x.write(fs);
  44. }
  45. void read(const FileNode& node,MyData& x,const MyData& default_value = MyData()){
  46. if(node.empty())
  47. x = default_value;
  48. else
  49. x.read(node);
  50. }
  51. // This function will print our custom class to the console
  52. ostream& operator<<(ostream& out,const MyData& m)
  53. {
  54. out << "{ id = " << m.id << ",";
  55. out << "X = " << m.X << ",";
  56. out << "A = " << m.A << "}";
  57. return out;
  58. }
  59. int main(int ac,char** av)
  60. {
  61. if (ac != 2)
  62. {
  63. help(av);
  64. return 1;
  65. }
  66. string filename = av[1];
  67. { //write
  68. Mat R = Mat_<uchar>::eye(3,1);
  69. MyData m(1);
  70. FileStorage fs(filename,FileStorage::WRITE);
  71. fs << "iterationNr" << 100;
  72. fs << "strings" << "["; // text - string sequence
  73. fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
  74. fs << "]"; // close sequence
  75. fs << "Mapping"; // text - mapping
  76. fs << "{" << "One" << 1;
  77. fs << "Two" << 2 << "}";
  78. fs << "R" << R; // cv::Mat
  79. fs << "T" << T;
  80. fs << "MyData" << m; // your own data structures
  81. fs.release(); // explicit close
  82. cout << "Write Done." << endl;
  83. }
  84. {//read
  85. cout << endl << "Reading: " << endl;
  86. FileStorage fs;
  87. fs.open(filename,FileStorage::READ);
  88. int itNr;
  89. //fs["iterationNr"] >> itNr;
  90. itNr = (int) fs["iterationNr"];
  91. cout << itNr;
  92. if (!fs.isOpened())
  93. {
  94. cerr << "Failed to open " << filename << endl;
  95. help(av);
  96. return 1;
  97. }
  98. FileNode n = fs["strings"]; // Read string sequence - Get node
  99. if (n.type() != FileNode::SEQ)
  100. {
  101. cerr << "strings is not a sequence! FAIL" << endl;
  102. return 1;
  103. }
  104. FileNodeIterator it = n.begin(),it_end = n.end(); // Go through the node
  105. for (; it != it_end; ++it)
  106. cout << (string)*it << endl;
  107. n = fs["Mapping"]; // Read mappings from a sequence
  108. cout << "Two " << (int)(n["Two"]) << "; ";
  109. cout << "One " << (int)(n["One"]) << endl << endl;
  110. MyData m;
  111. Mat R,T;
  112. fs["R"] >> R; // Read cv::Mat
  113. fs["T"] >> T;
  114. fs["MyData"] >> m; // Read your own structure_
  115. cout << endl
  116. << "R = " << R << endl;
  117. cout << "T = " << T << endl << endl;
  118. cout << "MyData = " << endl << m << endl << endl;
  119. //Show default behavior for non existing nodes
  120. cout << "Attempt to read NonExisting (should initialize the data structure with its default).";
  121. fs["NonExisting"] >> m;
  122. cout << endl << "NonExisting = " << endl << m << endl;
  123. }
  124. cout << endl
  125. << "Tip: Open up " << filename << " with a text editor to see the serialized data." << endl;
  126. return 0;
  127. }


编译后,在命令行进入到文件目录,执行test test.xml,运行结果如下,生成一个test . xml文件内容如下:

  1. <?xml version="1.0" ?>
  2. - <opencv_storage>
  3. <iterationNr>100</iterationNr>
  4. <strings>image1.jpg Awesomeness baboon.jpg</strings>
  5. - <Mapping>
  6. <One>1</One>
  7. <Two>2</Two>
  8. </Mapping>
  9. - <R type_id="opencv-matrix">
  10. <rows>3</rows>
  11. <cols>3</cols>
  12. <dt>u</dt>
  13. <data>1 0 0 0 1 0 0 0 1</data>
  14. </R>
  15. - <T type_id="opencv-matrix">
  16. <rows>3</rows>
  17. <cols>1</cols>
  18. <dt>d</dt>
  19. <data>0. 0. 0.</data>
  20. </T>
  21. - <MyData>
  22. <A>97</A>
  23. <X>3.1415926535897931e+000</X>
  24. <id>mydata1234</id>
  25. </MyData>
  26. </opencv_storage>

猜你在找的XML相关文章