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

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

@H_301_1@

OpenCV——XMLYAML文件实现I/O操作

@H_301_1@

@H_301_1@1. XMLYAML文件的打开和关闭

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

@H_301_1@[cpp]view plaincopy

  1. @H_301_1@stringfilename="I.xml";
  2. @H_301_1@FileStoragefs(filename,FileStorage::WRITE);
  3. @H_301_1@\\...
  4. @H_301_1@fs.open(filename,FileStorage::READ);

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

@H_301_1@[cpp]view plaincopy

  1. @H_301_1@fs.release();

@H_301_1@

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

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

@H_301_1@[cpp]view plaincopy

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

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

@H_301_1@[cpp]view plaincopy

  1. @H_301_1@intitNr;
  2. @H_301_1@fs["iterationNr"]>>itNr;
  3. @H_301_1@itNr=(int)fs["iterationNr"];

@H_301_1@

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

@H_301_1@[cpp]view plaincopy

  1. @H_301_1@MatR=Mat_<uchar>::eye(3,3),
  2. @H_301_1@T=Mat_<double>::zeros(3,1);
  3. @H_301_1@fs<<"R"<<R;//Writecv::Mat
  4. @H_301_1@fs<<"T"<<T;
  5. @H_301_1@fs["R"]>>R;//Readcv::Mat
  6. @H_301_1@fs["T"]>>T;

@H_301_1@

4. vector
arrays maps的输入和输出

@H_301_1@vector要注意在第一个元素前加上“[”,在最后一个元素前加上"]"。例如:

@H_301_1@[cpp]view plaincopy

  1. @H_301_1@fs<<"strings"<<"[";//text-stringsequence
  2. @H_301_1@fs<<"image1.jpg"<<"Awesomeness"<<"baboon.jpg";
  3. @H_301_1@fs<<"]";//closesequence

@H_301_1@
对于map结构的操作使用的符号是"{""}",例如:

@H_301_1@[cpp]view plaincopy

  1. @H_301_1@fs<<"Mapping";//text-mapping
  2. @H_301_1@fs<<"{"<<"One"<<1;
  3. @H_301_1@fs<<"Two"<<2<<"}";

@H_301_1@
读取这些结构的时候,会用到FileNodeFileNodeIterator数据结构。

@H_301_1@FileStorage类的[]操作符会返回FileNode数据类型,对于一连串的node,可以使用FileNodeIterator结构,例如:

@H_301_1@[cpp]view plaincopy

  1. @H_301_1@FileNoden=fs["strings"];//Readstringsequence-Getnode
  2. @H_301_1@if(n.type()!=FileNode::SEQ)
  3. @H_301_1@{
  4. @H_301_1@cerr<<"stringsisnotasequence!FAIL"<<endl;
  5. @H_301_1@return1;
  6. @H_301_1@}
  7. @H_301_1@FileNodeIteratorit=n.begin(),it_end=n.end();//Gothroughthenode
  8. @H_301_1@for(;it!=it_end;++it)
  9. @H_301_1@cout<<(string)*it<<endl;

@H_301_1@
5.
读写自己的数据结构

@H_301_1@这部分比较复杂,参考最后的实例中的MyData结构自己领悟吧

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

@H_301_1@源文件里填入如下代码

@H_301_1@[cpp]view plaincopy

  1. @H_301_1@#include<opencv2/core/core.hpp>
  2. @H_301_1@#include<iostream>
  3. @H_301_1@#include<string>
  4. @H_301_1@
  5. @H_301_1@usingnamespacecv;
  6. @H_301_1@usingnamespacestd;
  7. @H_301_1@
  8. @H_301_1@voidhelp(char**av)
  9. @H_301_1@{
  10. @H_301_1@cout<<endl
  11. @H_301_1@<<av[0]<<"showstheusageoftheOpenCVserializationfunctionality."<<endl
  12. @H_301_1@<<"usage:"<<endl
  13. @H_301_1@<<av[0]<<"outputfile.yml.gz"<<endl
  14. @H_301_1@<<"TheoutputfilemaybeeitherXML(xml)orYAML(yml/yaml).Youcanevencompressitby"
  15. @H_301_1@<<"specifyingthisinitsextensionlikexml.gzyaml.gzetc..."<<endl
  16. @H_301_1@<<"WithFileStorageyoucanserializeobjectsinOpenCVbyusingthe<<and>>operators"<<endl
  17. @H_301_1@<<"Forexample:-createaclassandhaveitserialized"<<endl
  18. @H_301_1@<<"-useittoreadandwritematrices."<<endl;
  19. @H_301_1@}
  20. @H_301_1@
  21. @H_301_1@classMyData
  22. @H_301_1@{
  23. @H_301_1@public:
  24. @H_301_1@MyData():A(0),X(0),id()
  25. @H_301_1@{}
  26. @H_301_1@explicitMyData(int):A(97),X(CV_PI),id("mydata1234")//explicittoavoidimplicitconversion
  27. @H_301_1@{}
  28. @H_301_1@voidwrite(FileStorage&fs)const//Writeserializationforthisclass
  29. @H_301_1@{
  30. @H_301_1@fs<<"{"<<"A"<<A<<"X"<<X<<"id"<<id<<"}";
  31. @H_301_1@}
  32. @H_301_1@voidread(constFileNode&node)//Readserializationforthisclass
  33. @H_301_1@{
  34. @H_301_1@A=(int)node["A"];
  35. @H_301_1@X=(double)node["X"];
  36. @H_301_1@id=(string)node["id"];
  37. @H_301_1@}
  38. @H_301_1@public://DataMembers
  39. @H_301_1@intA;
  40. @H_301_1@doubleX;
  41. @H_301_1@stringid;
  42. @H_301_1@};
  43. @H_301_1@
  44. @H_301_1@//ThesewriteandreadfunctionsmustbedefinedfortheserializationinFileStoragetowork
  45. @H_301_1@voidwrite(FileStorage&fs,conststd::string&,constMyData&x)
  46. @H_301_1@{
  47. @H_301_1@x.write(fs);
  48. @H_301_1@}
  49. @H_301_1@voidread(constFileNode&node,MyData&x,constMyData&default_value=MyData()){
  50. @H_301_1@if(node.empty())
  51. @H_301_1@x=default_value;
  52. @H_301_1@else
  53. @H_301_1@x.read(node);
  54. @H_301_1@}
  55. @H_301_1@
  56. @H_301_1@//Thisfunctionwillprintourcustomclasstotheconsole
  57. @H_301_1@ostream&operator<<(ostream&out,constMyData&m)
  58. @H_301_1@{
  59. @H_301_1@out<<"{id="<<m.id<<",";
  60. @H_301_1@out<<"X="<<m.X<<",";
  61. @H_301_1@out<<"A="<<m.A<<"}";
  62. @H_301_1@returnout;
  63. @H_301_1@intmain(intac,char**av)
  64. @H_301_1@{
  65. @H_301_1@if(ac!=2)
  66. @H_301_1@{
  67. @H_301_1@help(av);
  68. @H_301_1@return1;
  69. @H_301_1@stringfilename=av[1];
  70. @H_301_1@{//write
  71. @H_301_1@MyDatam(1);
  72. @H_301_1@
  73. @H_301_1@FileStoragefs(filename,FileStorage::WRITE);
  74. @H_301_1@
  75. @H_301_1@fs<<"iterationNr"<<100;
  76. @H_301_1@fs<<"strings"<<"[";//text-stringsequence
  77. @H_301_1@fs<<"image1.jpg"<<"Awesomeness"<<"baboon.jpg";
  78. @H_301_1@fs<<"]";//closesequence
  79. @H_301_1@
  80. @H_301_1@fs<<"Mapping";//text-mapping
  81. @H_301_1@fs<<"{"<<"One"<<1;
  82. @H_301_1@fs<<"Two"<<2<<"}";
  83. @H_301_1@
  84. @H_301_1@fs<<"R"<<R;//cv::Mat
  85. @H_301_1@fs<<"T"<<T;
  86. @H_301_1@
  87. @H_301_1@fs<<"MyData"<<m;//yourowndatastructures
  88. @H_301_1@
  89. @H_301_1@fs.release();//explicitclose
  90. @H_301_1@cout<<"WriteDone."<<endl;
  91. @H_301_1@{//read
  92. @H_301_1@cout<<endl<<"Reading:"<<endl;
  93. @H_301_1@FileStoragefs;
  94. @H_301_1@fs.open(filename,FileStorage::READ);
  95. @H_301_1@
  96. @H_301_1@intitNr;
  97. @H_301_1@//fs["iterationNr"]>>itNr;
  98. @H_301_1@itNr=(int)fs["iterationNr"];
  99. @H_301_1@cout<<itNr;
  100. @H_301_1@if(!fs.isOpened())
  101. @H_301_1@{
  102. @H_301_1@cerr<<"Failedtoopen"<<filename<<endl;
  103. @H_301_1@FileNoden=fs["strings"];//Readstringsequence-Getnode
  104. @H_301_1@if(n.type()!=FileNode::SEQ)
  105. @H_301_1@return1;
  106. @H_301_1@}
  107. @H_301_1@
  108. @H_301_1@FileNodeIteratorit=n.begin(),it_end=n.end();//Gothroughthenode
  109. @H_301_1@for(;it!=it_end;++it)
  110. @H_301_1@cout<<(string)*it<<endl;
  111. @H_301_1@
  112. @H_301_1@
  113. @H_301_1@n=fs["Mapping"];//Readmappingsfromasequence
  114. @H_301_1@cout<<"Two"<<(int)(n["Two"])<<";";
  115. @H_301_1@cout<<"One"<<(int)(n["One"])<<endl<<endl;
  116. @H_301_1@
  117. @H_301_1@
  118. @H_301_1@MyDatam;
  119. @H_301_1@MatR,T;
  120. @H_301_1@
  121. @H_301_1@fs["R"]>>R;//Readcv::Mat
  122. @H_301_1@fs["T"]>>T;
  123. @H_301_1@fs["MyData"]>>m;//Readyourownstructure_
  124. @H_301_1@
  125. @H_301_1@cout<<endl
  126. @H_301_1@<<"R="<<R<<endl;
  127. @H_301_1@cout<<"T="<<T<<endl<<endl;
  128. @H_301_1@cout<<"MyData="<<endl<<m<<endl<<endl;
  129. @H_301_1@
  130. @H_301_1@//Showdefaultbehaviorfornonexistingnodes
  131. @H_301_1@cout<<"AttempttoreadNonExisting(shouldinitializethedatastructurewithitsdefault).";
  132. @H_301_1@fs["NonExisting"]>>m;
  133. @H_301_1@cout<<endl<<"NonExisting="<<endl<<m<<endl;
  134. @H_301_1@}
  135. @H_301_1@
  136. @H_301_1@cout<<endl
  137. @H_301_1@<<"Tip:Openup"<<filename<<"withatexteditortoseetheserializeddata."<<endl;
  138. @H_301_1@
  139. @H_301_1@return0;
  140. @H_301_1@}

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

@H_301_1@[html]view plaincopy

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

猜你在找的XML相关文章