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

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

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


1. XMLYAML文件的打开和关闭

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

[cpp]view plaincopy

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


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

[cpp]view plaincopy

  1. fs.release();

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

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

[cpp]view plaincopy

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


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

[cpp]view plaincopy

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



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

[cpp]view plaincopy

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



4. vector
arrays maps的输入和输出

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

[cpp]view plaincopy

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


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

[cpp]view plaincopy

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


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

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

[cpp]view plaincopy

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


5.
读写自己的数据结构

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

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

文件里填入如下代码

[cpp]view plaincopy

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


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

[html]view plaincopy

  1. <?xmlversion="1.0"?>
  2. -<opencv_storage>
  3. <iterationNr>100</iterationNr>
  4. <strings>image1.jpgAwesomenessbaboon.jpg</strings>
  5. -<Mapping>
  6. <One>1</One>
  7. <Two>2</Two>
  8. </Mapping>
  9. -<Rtype_id="opencv-matrix">
  10. <rows>3</rows>
  11. <cols>3</cols>
  12. <dt>u</dt>
  13. <data>100010001</data>
  14. </R>
  15. -<Ttype_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>
原文链接:https://www.f2er.com/xml/296267.html

猜你在找的XML相关文章