@H_301_1@
@H_301_1@
@H_301_1@XML\YAML文件在OpenCV中的数据结构为FileStorage,打开操作例如:
@H_301_1@[cpp]view plaincopy
@H_301_1@
文件关闭操作会在FileStorage结构销毁时自动进行,但也可调用如下函数实现
@H_301_1@[cpp]view plaincopy
- @H_301_1@fs.release();
@H_301_1@
@H_301_1@[cpp]view plaincopy
- @H_301_1@fs<<"iterationNr"<<100;
@H_301_1@[cpp]view plaincopy
@H_301_1@
3. OpenCV数据结构的输入和输出,和基本的C++形式相同
@H_301_1@[cpp]view plaincopy
@H_301_1@
4. vector(arrays)和 maps的输入和输出
@H_301_1@vector要注意在第一个元素前加上“[”,在最后一个元素前加上"]"。例如:
@H_301_1@[cpp]view plaincopy
@H_301_1@
对于map结构的操作使用的符号是"{"和"}",例如:
@H_301_1@[cpp]view plaincopy
@H_301_1@
读取这些结构的时候,会用到FileNode和FileNodeIterator数据结构。
@H_301_1@对FileStorage类的[]操作符会返回FileNode数据类型,对于一连串的node,可以使用FileNodeIterator结构,例如:
@H_301_1@[cpp]view plaincopy
- @H_301_1@FileNoden=fs["strings"];//Readstringsequence-Getnode
- @H_301_1@if(n.type()!=FileNode::SEQ)
- @H_301_1@{
- @H_301_1@cerr<<"stringsisnotasequence!FAIL"<<endl;
- @H_301_1@return1;
- @H_301_1@}
- @H_301_1@FileNodeIteratorit=n.begin(),it_end=n.end();//Gothroughthenode
- @H_301_1@for(;it!=it_end;++it)
- @H_301_1@cout<<(string)*it<<endl;
@H_301_1@
5. 读写自己的数据结构
@H_301_1@这部分比较复杂,参考最后的实例中的MyData结构自己领悟吧
@H_301_1@最后,我这里上一个实例,供大家参考。
@H_301_1@[cpp]view plaincopy
- @H_301_1@#include<opencv2/core/core.hpp>
- @H_301_1@#include<iostream>
- @H_301_1@#include<string>
- @H_301_1@
- @H_301_1@usingnamespacecv;
- @H_301_1@usingnamespacestd;
- @H_301_1@
- @H_301_1@voidhelp(char**av)
- @H_301_1@{
- @H_301_1@cout<<endl
- @H_301_1@<<av[0]<<"showstheusageoftheOpenCVserializationfunctionality."<<endl
- @H_301_1@<<"usage:"<<endl
- @H_301_1@<<av[0]<<"outputfile.yml.gz"<<endl
- @H_301_1@<<"TheoutputfilemaybeeitherXML(xml)orYAML(yml/yaml).Youcanevencompressitby"
- @H_301_1@<<"specifyingthisinitsextensionlikexml.gzyaml.gzetc..."<<endl
- @H_301_1@<<"WithFileStorageyoucanserializeobjectsinOpenCVbyusingthe<<and>>operators"<<endl
- @H_301_1@<<"Forexample:-createaclassandhaveitserialized"<<endl
- @H_301_1@<<"-useittoreadandwritematrices."<<endl;
- @H_301_1@}
- @H_301_1@
- @H_301_1@classMyData
- @H_301_1@{
- @H_301_1@public:
- @H_301_1@MyData():A(0),X(0),id()
- @H_301_1@{}
- @H_301_1@explicitMyData(int):A(97),X(CV_PI),id("mydata1234")//explicittoavoidimplicitconversion
- @H_301_1@{}
- @H_301_1@voidwrite(FileStorage&fs)const//Writeserializationforthisclass
- @H_301_1@{
- @H_301_1@fs<<"{"<<"A"<<A<<"X"<<X<<"id"<<id<<"}";
- @H_301_1@}
- @H_301_1@voidread(constFileNode&node)//Readserializationforthisclass
- @H_301_1@{
- @H_301_1@A=(int)node["A"];
- @H_301_1@X=(double)node["X"];
- @H_301_1@id=(string)node["id"];
- @H_301_1@}
- @H_301_1@public://DataMembers
- @H_301_1@intA;
- @H_301_1@doubleX;
- @H_301_1@stringid;
- @H_301_1@};
- @H_301_1@
- @H_301_1@//ThesewriteandreadfunctionsmustbedefinedfortheserializationinFileStoragetowork
- @H_301_1@voidwrite(FileStorage&fs,conststd::string&,constMyData&x)
- @H_301_1@{
- @H_301_1@x.write(fs);
- @H_301_1@}
- @H_301_1@voidread(constFileNode&node,MyData&x,constMyData&default_value=MyData()){
- @H_301_1@if(node.empty())
- @H_301_1@x=default_value;
- @H_301_1@else
- @H_301_1@x.read(node);
- @H_301_1@}
- @H_301_1@
- @H_301_1@//Thisfunctionwillprintourcustomclasstotheconsole
- @H_301_1@ostream&operator<<(ostream&out,constMyData&m)
- @H_301_1@{
- @H_301_1@out<<"{id="<<m.id<<",";
- @H_301_1@out<<"X="<<m.X<<",";
- @H_301_1@out<<"A="<<m.A<<"}";
- @H_301_1@returnout;
- @H_301_1@intmain(intac,char**av)
- @H_301_1@{
- @H_301_1@if(ac!=2)
- @H_301_1@{
- @H_301_1@help(av);
- @H_301_1@return1;
- @H_301_1@stringfilename=av[1];
- @H_301_1@{//write
- @H_301_1@MyDatam(1);
- @H_301_1@
- @H_301_1@FileStoragefs(filename,FileStorage::WRITE);
- @H_301_1@
- @H_301_1@fs<<"iterationNr"<<100;
- @H_301_1@fs<<"strings"<<"[";//text-stringsequence
- @H_301_1@fs<<"image1.jpg"<<"Awesomeness"<<"baboon.jpg";
- @H_301_1@fs<<"]";//closesequence
- @H_301_1@
- @H_301_1@fs<<"Mapping";//text-mapping
- @H_301_1@fs<<"{"<<"One"<<1;
- @H_301_1@fs<<"Two"<<2<<"}";
- @H_301_1@
- @H_301_1@fs<<"R"<<R;//cv::Mat
- @H_301_1@fs<<"T"<<T;
- @H_301_1@
- @H_301_1@fs<<"MyData"<<m;//yourowndatastructures
- @H_301_1@
- @H_301_1@fs.release();//explicitclose
- @H_301_1@cout<<"WriteDone."<<endl;
- @H_301_1@{//read
- @H_301_1@cout<<endl<<"Reading:"<<endl;
- @H_301_1@FileStoragefs;
- @H_301_1@fs.open(filename,FileStorage::READ);
- @H_301_1@
- @H_301_1@intitNr;
- @H_301_1@//fs["iterationNr"]>>itNr;
- @H_301_1@itNr=(int)fs["iterationNr"];
- @H_301_1@cout<<itNr;
- @H_301_1@if(!fs.isOpened())
- @H_301_1@{
- @H_301_1@cerr<<"Failedtoopen"<<filename<<endl;
- @H_301_1@FileNoden=fs["strings"];//Readstringsequence-Getnode
- @H_301_1@if(n.type()!=FileNode::SEQ)
- @H_301_1@return1;
- @H_301_1@}
- @H_301_1@
- @H_301_1@FileNodeIteratorit=n.begin(),it_end=n.end();//Gothroughthenode
- @H_301_1@for(;it!=it_end;++it)
- @H_301_1@cout<<(string)*it<<endl;
- @H_301_1@
- @H_301_1@
- @H_301_1@n=fs["Mapping"];//Readmappingsfromasequence
- @H_301_1@cout<<"Two"<<(int)(n["Two"])<<";";
- @H_301_1@cout<<"One"<<(int)(n["One"])<<endl<<endl;
- @H_301_1@
- @H_301_1@
- @H_301_1@MyDatam;
- @H_301_1@MatR,T;
- @H_301_1@
- @H_301_1@fs["R"]>>R;//Readcv::Mat
- @H_301_1@fs["T"]>>T;
- @H_301_1@fs["MyData"]>>m;//Readyourownstructure_
- @H_301_1@
- @H_301_1@cout<<endl
- @H_301_1@<<"R="<<R<<endl;
- @H_301_1@cout<<"T="<<T<<endl<<endl;
- @H_301_1@cout<<"MyData="<<endl<<m<<endl<<endl;
- @H_301_1@
- @H_301_1@//Showdefaultbehaviorfornonexistingnodes
- @H_301_1@cout<<"AttempttoreadNonExisting(shouldinitializethedatastructurewithitsdefault).";
- @H_301_1@fs["NonExisting"]>>m;
- @H_301_1@cout<<endl<<"NonExisting="<<endl<<m<<endl;
- @H_301_1@}
- @H_301_1@
- @H_301_1@cout<<endl
- @H_301_1@<<"Tip:Openup"<<filename<<"withatexteditortoseetheserializeddata."<<endl;
- @H_301_1@
- @H_301_1@return0;
- @H_301_1@}
@H_301_1@
编译后,在命令行进入到文件目录,执行test test.xml,运行结果如下,生成一个test . xml文件,内容如下:
@H_301_1@[html]view plaincopy
- @H_301_1@<?xmlversion="1.0"?>
- @H_301_1@-<opencv_storage>
- @H_301_1@<iterationNr>100</iterationNr>
- @H_301_1@<strings>image1.jpgAwesomenessbaboon.jpg</strings>
- @H_301_1@-<Mapping>
- @H_301_1@<One>1</One>
- @H_301_1@<Two>2</Two>
- @H_301_1@</Mapping>
- @H_301_1@-<Rtype_id="opencv-matrix">
- @H_301_1@<rows>3</rows>
- @H_301_1@<cols>3</cols>
- @H_301_1@<dt>u</dt>
- @H_301_1@<data>100010001</data>
- @H_301_1@</R>
- @H_301_1@-<Ttype_id="opencv-matrix">
- @H_301_1@<rows>3</rows>
- @H_301_1@<cols>1</cols>
- @H_301_1@<dt>d</dt>
- @H_301_1@<data>0.0.0.</data>
- @H_301_1@</T>
- @H_301_1@-<MyData>
- @H_301_1@<A>97</A>
- @H_301_1@<X>3.1415926535897931e+000</X>
- @H_301_1@<id>mydata1234</id>
- @H_301_1@</MyData>
- @H_301_1@</opencv_storage>