您的位置:首页 > 编程语言 > C语言/C++

如何使用opencv的c++接口来读取、写结构体数组到xml文件中

2013-10-21 14:13 1001 查看
参考网址:http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html#filestorage-writeraw

#include<opencv2\opencv.hpp>
#include<time.h>
using namespace std;
using namespace cv;
typedef struct _t{
int x;
int y;
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<<"{:";//一个结构体开始的标志
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐