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

参考网址: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值