引用地址
参考地址:
http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html#filestorage-writeraw
优先选用yml而不是xml
#include<opencv2\opencv.hpp>
#include<time.h>
using namespace std;
using namespace cv;
typedef struct _t{
int x;
int y; //不支持size_t,unsigned等无符号类型数值的存储,目前只支持int,float,double,string类型(具体解释在代码后)
double c;
}t;
#include "opencv2/opencv.hpp"
#include <time.h>
using namespace cv;
int main(int, char** argv)
{
FileStorage fs("test.xml", FileStorage::WRITE);
fs << "frameCount" << 5;
time_t rawtime; time(&rawtime);
fs << "features" << "[";//结构数组开始的标志
for( int i = 0; i < 3; i++ )
{
int x = rand() % 640;//使用随机数产生数值
int y = rand() % 480;
double c = rand()*0.3;
fs<<"{:";//一个结构体开始的标志,yml才有:,xml自动忽略
fs<<"x"<<x;
fs<<"y"<<y;
fs<<"c"<<c;
fs<<"}";//结构体结束的标志,自动换行
}
fs << "]";//结构体数组结束的标志。
fs.release();
//读取xml文件。
cv::FileStorage fs2("test.xml",FileStorage::READ);
FileNode features = fs2["features"];//读取结构体数组
FileNodeIterator fni = features.begin();
FileNodeIterator fniEnd = features.end();
for(;fni != fniEnd;fni++)//遍历
{
t myT ;
myT.x = (int )(*fni)["x"];
myT.y = (int )(*fni)["y"];
myT.c = (double)(*fni)["c"];
cout<<"x"<<myT.x<<" "<<"y"<<myT.y<<" c"<<myT.c<<endl;
}
return 0;
}
结构体数值支持类型解释:
CV_EXPORTS_W void write( FileStorage& fs, const string& name, int value );
CV_EXPORTS_W void write( FileStorage& fs, const string& name, float value );
CV_EXPORTS_W void write( FileStorage& fs, const string& name, double value );
CV_EXPORTS_W void write( FileStorage& fs, const string& name, const string& value );