OpenCV中XML与YAML文件读取

XML与YAML文件读取

参考《OpenCV3编程入门》
1、说明;
2、Code;
3、效果;

说明

1、XML:可扩展标识语言,开发者可以根据自己的需要定义定义自己的标记,是一种语义/结构化语言;
2、YAML : 一种置标语言,强调语言以数据为中心,而不是以置标语言为中心,是一个可读性高,用来表达资料序列的格式;
3、XML与YAML是使用非常广泛的文件格式,可以利用XML或者YAML格式的文件存储和还原各式各样的数据结构,包括OpenCV的相关数据结构,以及各种原始数据类型,如整数,浮点数,文本字符串;
4、使用步骤:
①实例化FileStorage类对象,用默认带参构造函数完成初始化,或者调用open()方法;
②使用流操作符<< 或>>进行读写操作;
③调用release()函数析构FileStorage类对象;
5、demo:

//write
FileStorage fs("abc.xml" , FileStorage::WRITE);
//or
FileStorage fs;
fs.open("abc.xml" , FileStorage::WRITE);
//read
FileStorage fs;
fs.open("abc.xml", FileStorage::WRITE);
//1、文本和数字输入
fs << "iterationNr" << 100;
int itNr;
fs["iterationNr"] >> itNr;
itNr = (int) fs["iterationNr"];
//2、OpenCV中 数据结构输入输出同C++基本类型一样
Mat R = Mat_<uchar>:: eyes(3 , 3);
Mat T = Mat_<double> :: zeros(3,1);
//write
fs << "R" << R;
fs<< "T" << T;
//read
fs["R"] >> R;
fs["T"] >> T;
//3、vector与maps类型输入输出
//vector 结构的输入与输出,在第一个元素前加"[",最后一个元素前加"]"
fs << "strings" << "[";
fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
fs << "]";  //关闭序列
//序列结构,使用“{”  “}”
fs << "Mapping";
fs << "{" << "One" << 1;
fs << "Two" << 2 << "}";

FileNode n = fs["strings"];  
if(n.type() != FileNode::SEQ)
{
	cerr << "发生错误!字符串不是一个序列! "  << endl;
	return 1;
}
//对于一连串的node,可以使用FileNodeIterator结构
 FileNodeIterator it = n.begin() , it_end = n.end();
 for(; it != it_end; ++it)
 {
 	cout << (string) * it << endl;
 }

Code

#include <opencv2/opencv.hpp>
#include <iostream>
#include <time.h>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
	//改变console字体颜色
	system("color 6F");

	//初始化
	FileStorage fs("test.yaml", FileStorage::READ);
	
	//读取文本内容
	int frameCount = (int)fs["frameCount"];
	string date;
	fs["calibrationDate"] >> date;

	Mat cameraMatrix2, distCoeffs2;

	fs["cameraMatrix"] >> cameraMatrix2;
	fs["distCoeffs"] >> distCoeffs2;

	cout << "frameCount: " << frameCount << endl
		<< "calibration Date: " << date << endl
		<< "cameraMatrix: " << cameraMatrix2 << endl
		<< "distCoeffs: " << distCoeffs2 << endl;

	FileNode features = fs["features"];
	FileNodeIterator it = features.begin(), it_end = features.end();
	int idx = 0;
	vector<uchar> lbpval;
	
	//使用FileNodeIterator遍历序列
	for (; it != it_end; it++,idx++)
	{
		cout << "feature #" << idx << ": ";
		cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
		(*it)["lbp"] >> lbpval;

		for (int i = 0; i < (int)lbpval.size(); i++)
		{
			cout << " " << (int)lbpval[i];
		}
		cout << ")" << endl;
	}

	fs.release();  //关闭文件

	printf("\n文件读取完毕,请输入任意键结束程序~");
	getchar();

	return 0;
}

/*
//文件写操作
int main(int argc, char** argv)
{
	//初始化
	FileStorage fs("test.yaml", FileStorage::WRITE);
	if (!fs.isOpened())
	{
		cout << "文件打开失败....";
	}

	//开始写入文件
	fs << "frameCount" << 5;
	//获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,
	//其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数
	time_t rawtime; time(&rawtime); 
	fs << "calibrationDate" << asctime(localtime(&rawtime));

	Mat cameraMatrix = (Mat_<double>(3, 3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
	Mat distCoeffs = (Mat_<double>(1, 5) << 0.1, 0.01, -0.001, 0, 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();
	printf("文件读写完毕,请在工程目录下查看生成的文件~");	
	getchar();
	return 0;
}
*/

运行结果

!!需要注意文件夹一定具有可写权限,否则text.yaml文件无法正常写入。
在这里插入图片描述
在这里插入图片描述

发布了61 篇原创文章 · 获赞 5 · 访问量 4476

猜你喜欢

转载自blog.csdn.net/hellohake/article/details/104643611