OpenCV中XML文件的相关操作

该文主要介绍OpenCV中XML文件的读写。大部分代码来自于OpenCV官方文档。

一、向XML文件中写入数据

1.1 打开文件

首先以写得方式打开一个xml文件。

//以写得方式打开一个xml文件
string filename = "123.xml";
FileStorage fs(filename, FileStorage::WRITE);
//filename:为文件的路径
//注:如果文件不存在则创建文件,如果文件存在会将文件中已有的内容清空。

1.2向文件中写入数据

通过不同的写入方式可以向文件中写入不同形式的数据,以下将一一介绍。

写入方式一:

向文件中写入一个节点和一个值,在xml中的形式如下:
在这里插入图片描述
写入如上形式的xml数据的代码如下:

//1、节点名和节点值一一对应,
//2、不能连续在一个节点名后面跟两个值(如:fs << "iterationNr" << 100 << 20;这种是错误的)。
//3、节点名必须是字符串类型。
fs << "iterationNr" << 100;
写入方式二:

向一个节点中写入多个值,这时需要使用中括号,在xml文件中该节点的值用空格间隔。在xml中的形式如下:
在这里插入图片描述
写入如上形式的xml数据的代码如下:

//写入的这多个值要放在中括号中
string a = "node1";
fs << a << "[" << 30;
fs << 20 << "]" ;
写入方式三:

创建多级节点,创建多级节点需要使用大括号。在xml中的形式如下:
在这里插入图片描述
写入如上形式的xml数据的代码如下:

fs << "ParentsNode" << "{" << "SubNode1";
fs << "{" << "SubNode2" << "{";
fs << "name" << "zhangsan" << "age" << 18 << "}" << "}" << "}";
写入方式四:

向XML文件中写入OpenCV中的Mat对象,在xml中的形式如下:
在这里插入图片描述

写入如上形式的xml数据的代码如下:

Mat F3 = Mat_<Vec3f>::eye(3, 3);
fs << "F3" << F3;
code

xml文件的写入的测试代码如下:

string filename = "123.xml";
Mat R = Mat_<uchar>::eye(3, 3);
Mat T = Mat_<double>::zeros(3, 1);
FileStorage fs(filename, FileStorage::WRITE);
fs << "iterationNr" << 100;
string a = "node1";
fs << a << "[" << 30;
fs << 20 << "]" ;
fs << "strings" << "["
	<< "image1.jpg" << "Awesomeness" << "../data/baboon.jpg";
fs << "]";

fs << "ParentsNode" << "{" << "SubNode1";
fs << "{" << "SubNode2" << "{";
fs << "name" << "zhangsan" << "age" << 18 << "}" << "}" << "}";
	

fs << "Mapping";
fs << "{" << "One" << 1;
fs << "Two" << 2 << "}";

Mat R3 = Mat_<Vec3b>::eye(3, 3);
Mat F3 = Mat_<Vec3f>::eye(3, 3);


fs << "R3" << R3;
fs << "F3" << F3;

fs << "R" << R;
fs << "T" << T;

		
fs.release();

cout << "Write Done." << endl;

通过代码写入后xml的文件形式如下:
在这里插入图片描述

二、从XML文件中读取数据

在OpenCV中读取xml文件中的数据有如下几种方式。

方式一:

读取如下形式的数据:
在这里插入图片描述
读取代码为:

//读取方法一:
int itNr;
itNr = (int)fs["iterationNr"];
cout << itNr << endl;
//读取方法二:
FileNode nod = fs["iterationNr"];
for (FileNodeIterator it = nod.begin(), it_end = nod.end(); it != it_end; it++)
{
	cout << (int)*it << endl;
}
方式二:

读取如下形式的数据:
在这里插入图片描述
读取代码为:

FileNode n = fs["strings"];
if (n.type() != FileNode::SEQ)
{
	cerr << "strings is not a sequence! FAIL" << endl;
	return 1;
}
FileNodeIterator it = n.begin(), it_end = n.end();
for (; it != it_end; ++it)
	cout << (string)*it << endl;
方式三:

读取如下形式的数据:
在这里插入图片描述
读取代码为:

FileNode n3 = fs["ParentsNode"];
FileNode n4 = n3["SubNode1"];
FileNode n5 = n4["SubNode2"];
cout << n5.name() << endl;
cout << "name = " << (string)(n5["name"]) << endl;
cout << "age = " << (int)(n5["age"]) << endl;
方式四:

读取如下形式的数据:
在这里插入图片描述
读取代码为:

//矩阵的读取方式
Mat F3;
fs["F3"] >> F3;
cout << "F3 = " << F3 << endl;
code

xml文件数据的读取的测试代码如下:

cout << "endl" << "Reading: " << endl;
string filename = "123.xml";
FileStorage fs;
fs.open(filename, FileStorage::READ);
if (!fs.isOpened())
{
	cerr << "Failed to open " << filename << endl;
	return 1;
}

int itNr;
itNr = (int)fs["iterationNr"];
cout << itNr << endl;

FileNode nod = fs["iterationNr"];
for (FileNodeIterator it = nod.begin(), it_end = nod.end(); it != it_end; it++)
{
	cout << (int)*it << endl;
}

string n2 = (string)fs["strings"];
cout << "n2 = " << n2 << endl;

FileNode n = fs["strings"];
if (n.type() != FileNode::SEQ)
{
	cerr << "strings is not a sequence! FAIL" << endl;
	return 1;
}
FileNodeIterator it = n.begin(), it_end = n.end();
for (; it != it_end; ++it)
	cout << (string)*it << endl;


FileNode n3 = fs["ParentsNode"];
FileNode n4 = n3["SubNode1"];
FileNode n5 = n4["SubNode2"];
cout << n5.name() << endl;
cout << "name = " << (string)(n5["name"]) << endl;
cout << "age = " << (int)(n5["age"]) << endl;


//矩阵的读取方式
Mat F3;
fs["F3"] >> F3;
cout << "F3 = " << F3 << endl;
fs.release();
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值