Use of simple cv :: FileStorage

Reference blog https://blog.csdn.net/ktigerhero3/article/details/77523094
create a file and write data files

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

using namespace std;
using namespace cv;

int main()
{
	//1.创建文件
	cv::FileStorage fwrite("./setting.yaml",cv::FileStorage::WRITE);
	//2.写入数据
	float fx = 100.0;
	float fy = 101.0;
	fwrite<<"fx "<< fx;
	fwrite<<"fy "<< fy;
	//3.关闭文件
	fwrite.release();
	return 0;
}

2. Read data from the file yaml

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

using namespace std;
using namespace cv;

int main()
{
	//1.读取文件指针	
	string strSettingsFile = "./setting.yaml";
	cv::FileStorage fread(strSettingsFile.c_str(),cv::FileStorage::READ);

	//2.判断是否打开成功
	if(!fread.isOpened())
	{
		cout<<"Failed to open settings file at: "<<strSettingsFile<<endl;
		return 0 ;
	}
	else cout<<"success to open file at: "<<strSettingsFile<<endl;

	//3.打开文件后读取数据
	float fxread,fyread;
	fread["fx"]>>fxread;
	fread["fy"]>>fyread;
	cout<<"fxread="<<fxread<<endl;
	cout<<"fyread="<<fxread<<endl;

	//4.关闭文件
	fread.release();
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_34122731/article/details/90722950