opencv xml读写

#include <opencv2/core/core.hpp>
#include <iostream>
#include <string>
#include <time.h> 

using namespace cv;
using namespace std;


int main(int argc,char** argv) 
{

	if (0)//write
	{
		FileStorage fs("test.xml",FileStorage::WRITE);  

		fs << "frameCount" << 5;  
		time_t rawtime; time(&rawtime);  
		fs << "calibrationDate" << asctime(localtime(&rawtime));  
		Mat cameraMatrix = (Mat_<double>(3,3) << 1000,320,1000,240,1);  
		Mat distCoeffs = (Mat_<double>(5,1) << 0.1,0.01,-0.001,0);  
		fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;  
		fs << "features" << "[";  
		for( int i = 0; i < 3; i++ )  
		{  
		int x = rand() % 640;  
		int y = rand() % 480;  
		uchar lbp = rand() % 256;  

		fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";  
		for( int j = 0; j < 8; j++ )  
			fs << ((lbp >> j) & 1);  
		fs << "]" << "}";  
		}  
		fs << "]";  
		fs.release();   
	}
	else
	{
		FileStorage fs2("test.xml",FileStorage::READ);

		// first method: use (type) operator on FileNode.
		int frameCount = (int)fs2["frameCount"];

		std::string date;
		// second method: use FileNode::operator >>
		fs2["calibrationDate"] >> date;

		Mat cameraMatrix2,distCoeffs2;
		fs2["cameraMatrix"] >> cameraMatrix2;
		fs2["distCoeffs"] >> distCoeffs2;

		cout << "frameCount: " << frameCount << endl
		 << "calibration date: " << date << endl
		 << "camera matrix: " << cameraMatrix2 << endl
		 << "distortion coeffs: " << distCoeffs2 << endl;

		FileNode features = fs2["features"];
		FileNodeIterator it = features.begin(),it_end = features.end();
		int idx = 0;
		std::vector<uchar> lbpval;

		// iterate through a sequence using FileNodeIterator
		for( ; it != it_end; ++it,idx++ )
		{
		cout << "feature #" << idx << ": ";
			cout << "x=" << (int)(*it)["x"] << ",y=" << (int)(*it)["y"] << ",lbp: (";
			// you can also easily read numerical arrays using FileNode >> std::vector operator.
			(*it)["lbp"] >> lbpval;
			for( int i = 0; i < (int)lbpval.size(); i++ )
				cout << " " << (int)lbpval[i];
			cout << ")" << endl;
		}
		fs2.release();
	}
	return 0; 
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇