ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

OpenCV学习(24)

2022-01-26 19:58:52  阅读:125  来源: 互联网

标签:24 XML YAML int 学习 OpenCV fs2 文件 include


输入输出XML和YAML文件(2):示例程序

XML和YAML文件的写入;

XML和YAML文件的读取;

一,XML和YAML文件的写人

源代码如下:

#include<opencv2/opencv.hpp>
#include <vector>
#include <opencv2/core/utils/logger.hpp>
#include <time.h>

using namespace std;
using namespace cv;

int main()
{
	cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);//控制台不在输出日志文件
	//初始化
	FileStorage fs("test.yaml", FileStorage::WRITE);

	//开始文件写入
	fs << "frameCount" << 5;

	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>(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();

	printf("文件读写完毕,请在工程目录下查看生成的文件~\n");

	getchar();

	return 0;
}

运行时可能会报错:



解决方法:

运行结果:在工程目录下生成如图所示文件

 

 

 二,XML和YAML文件的读取

源代码如下:
 

#include<opencv2/opencv.hpp>
#include <vector>
#include <opencv2/core/utils/logger.hpp>
#include <time.h>

using namespace std;
using namespace cv;

int main()
{
	cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);//控制台不在输出日志文件
	//改变console字体颜色
	system("color 6F");

	//初始化
	FileStorage fs2("test.yaml", FileStorage::READ);

	//第一种方法,对FileNode操作
	int frameCount = (int)fs2["frameCount"];

	std::string date;
	//第二种方法,使用FileNode运算符>>
	fs2["calibrationDate"] >> date;

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

	cout << "frameCount: " << frameCount << endl
		<< "calibration date: " << date << endl
		<< "camera matrix:\n" << cameraMatrix << endl
		<< "distortion coeffs:\n" << distCoeffs2 << endl;

	FileNode features = fs2["features"];
	FileNodeIterator it = features.begin(), it_end = features.end();

	int idx = 0;
	std::vector<uchar> lbpval;

	//使用FileNodeIterator遍历序列
	for (; it != it_end; ++it, ++idx)
	{
		cout << "feature #" << idx << ":";
		cout << "x=" << (int)(*it)["x"] << ", y = " << (int)(*it)["*y"] << ", lbp: (";
		//我们也可以使用FileNode >> std::vector操作符来很容易地读数值阵列
		(*it)["lbp"] >> lbpval;
		for (int i = 0; i < (int)lbpval.size(); ++i)
		{
			cout << " " << (int)lbpval[i];
		}
		cout << ")" << endl;
	}

	fs2.release();

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

	return 0;
}

运行截图:

 

 

标签:24,XML,YAML,int,学习,OpenCV,fs2,文件,include
来源: https://blog.csdn.net/m0_53123717/article/details/122707566

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有