OpenCV xml与yaml文件使用

本文是基于毛星云主编的opencv 3编程入门(顺便推荐一下,这本书写的浅显易懂,看完之后,基本操作也都学的差不多了。Opencv3.3.1是本人使用的版本,其中的操作api有些已经更改,现在这里提一下),也算是自己画的重点吧。

XML(eXtensible Markup Language):元标记语言
YAML:出现是为了尝试比XML更为便捷,完成XML的任务

写入或读取XML或YAML文件的过程

  1. 实例化FileStorage类对象(两种解决方案)
  2. 使用流操作符<<进行文件写入,>>进行文件读取操作
  3. FileStorage::release()函数析构类对象,同时关闭文件

实例化类对象

方法1:

FileStorage fs(fileName,FileStorage::WRITE)//实例化对象fs
//fs设定为写入操作
//读取操作时,实例化对象方式写为FileStorage::READ

方法2:

FileStorage fs//实例化对象fs
fs.open(fileName,FileStorage::WRITE)

文件读写操作

文本和数字写入方法:

fs<<"youSetNameHere"<

读取时:

int itNr;//当读取int数据时
fs["youSetNameHere"]>>itNr;//一种读取方式
itNr = (int) fs["youSetNameHere"];//另一种读取方式
//注意数据类型准确
//youSetNameHere在存储数据的xml,yml文件中不存在时
//读取数据为空

vector(arrays)和maps的输入和输出
vector结构的输入与输出,需要在第一个元素前加上"[",在最后一个元素后加入"]"。
map结构需要使用的是" { “和” } "。

文件关闭

调用析构函数

fs.release();

实例

#include 
#include 
using namespace cv;
int main()
{
	/***********************写入*********************/
	FileStorage fs("test.yaml", FileStorage::WRITE);

	fs << "frameCount" << 5;
	time_t rawtime;
	time(&rawtime);

	fs << "calibrationData" << asctime(localtime(&rawtime));
	Mat cameraMatrix = (Mat_(3, 3) << 1000.0, 0, 320, 0, 
		1000, 240, 0, 0, 1);
	Mat distCoeffs = (Mat_(5, 1) << 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();
	/***********************写入*********************/
	/***********************读取*********************/
	system("color 6F");
	FileStorage fs2("test.yaml", FileStorage::READ);
	int frameCount = (int)fs2["frameCount"];

	std::string data;
	fs2["calibrationData"] >> data;

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

	std::cout << "frameCount:\t" << frameCount
		<< "\ncalibration data:\t" << data
		<< "\ncamera matrix:\n" << cameraMatrix2
		<< "\ndistortion coeffs:\t" << distCoeffs2 << std::endl;

	FileNode feature = fs2["features"];
	FileNodeIterator it = feature.begin(), it_end = feature.end();
	int idx = 0;
	std::vector lbpval;
	for (; it != it_end; ++it, idx++)
	{
		std::cout << "features #" << idx << ":";
		std::cout << "x = " << (int)(*it)["x"] << ", y = " << (int)(*it)["y"]
			<< ", lbp : (";
		(*it)["lbp"] >> lbpval;
		for (int i = 0; i < (int)lbpval.size(); i++)
			std::cout << "  " << (int)lbpval[i];
		std::cout << ")" << std::endl;
	}
	fs2.release();

	return 0;
}

经过尝试之后,发现利用python 3.6,cv2库可以实现yaml文件的读取,也就是说,可以利用yaml文件实现c++和python文件通讯了吧。
利用python实现yaml文件读取也比较简单,只需要导入cv2库之后,利用cv2.FileStorage(path, cv2.FILE_STORAGE_READ) 读取,保存的FileStorage文件。
读取其中的节点为loadData.getNode(NodeName),不过读取的节点也是FileStorage文件,需要将其转换为mat()格式

python写也一样

f = cv2.FileStorage('name.yaml', cv2.FILE_STORAGE_APPEND)
f.write(nodeName, nodeValue)

你可能感兴趣的:(OpenCV xml与yaml文件使用)