C++/OpenCV:将数据保存到xml、yaml / 从xml、yaml读取数据

  • Post author:
  • Post category:其他


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

using namespace std;

#define WRITE_OR_READ

int main() {
//===========将数据写入到xml文件中================
#ifdef WRITE_OR_READ 
    string name = "insomnia";
    int age = 18;
    float height = 1.83;
    char sex = 'M';
    cv::Mat matrix_eye = cv::Mat::eye(3, 3, CV_64F);

    cv::FileStorage fs("./test.xml", cv::FileStorage::WRITE);//会覆盖当前文件,不存在则新建文件
    if (fs.isOpened())
    {
        fs << "name" << name << "age" << age << "height" << height << "sex" << sex;//可以连续写入
        fs << "matrix_eye" << matrix_eye;//也可以依次写入
        fs.release();//release after used
    }
//===========从xml文件中读取数据================
#else 
    string name;
    int age;
    float height;
    char sex;
    cv::Mat matrix_eye;

    cv::FileStorage fs("./test.xml", cv::FileStorage::READ);
    if (fs.isOpened()) {
        fs["name"] >> name;
        fs["age"] >> age;
        fs["height"] >> height;
        int temp;
        fs["sex"] >> temp;//这里不能直接读到char,所以转换了一下
        sex = (char)temp;
        fs["matrix_eye"] >> matrix_eye;

        fs.release();

        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
        cout << "height: " << height << endl;
        cout << "sex: " << sex << endl;
        cout << "matrix_eye: " << endl << matrix_eye << endl;

        cout << "matrix_eye.size().height: " << matrix_eye.size().height << endl;
        cout << "matrix_eye.at<double>(1, 0): " << matrix_eye.at<double>(1, 0) << endl;
    }
#endif

    return 0;
}

将数据写入到xml文件后,打开查看一下

格式是自动生成的,只是将数据填充了进去。可以看到第一行是xml版本信息,不用考虑。第二行和最后一行是最外层的标签。

然后所有保存的数据都是一个并列的关系,同等级。只是像Mat这种数据类型,又有细分属性而已。

从xml文件中读取一下数据,看下输出结果


再看yaml文件的情况

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

using namespace std;
using namespace cv;

//#define WRITE_OR_READ

int main()
{
#ifdef WRITE_OR_READ
	//1.创建文件
	cv::FileStorage fwrite("./test.yaml", cv::FileStorage::WRITE);
	//2.写入数据
	string name = "insomnia";
	int age = 18;
	float height = 1.83;
	char sex = 'M';
	cv::Mat matrix_eye = cv::Mat::eye(3, 3, CV_64F);
	fwrite << "name " << name;
	fwrite << "age " << age;
	fwrite << "height " << height;
	fwrite << "sex " << sex;
	fwrite << "matrix_eye " << matrix_eye;
	//3.关闭文件
	fwrite.release();
	return 0;
#else
	//1.读取文件指针	
	string strSettingsFile = "./test.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.打开文件后读取数据
	string name;
	int age;
	float height;
	char sex;
	cv::Mat matrix_eye;

	fread["name"] >> name;
	fread["age"] >> age;
	fread["height"] >> height;
	int temp;
	fread["sex"] >> temp;
	sex = (char)temp;
	fread["matrix_eye"] >> matrix_eye;
	cout << "name=" << name << endl;
	cout << "age=" << age << endl;
	cout << "height=" << height << endl;
	cout << "sex=" << sex << endl;
	cout << "matrix_eye=" << endl << matrix_eye << endl;

	cout << matrix_eye.size().height << endl;

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

}

保存的yaml文件

读取文件的结果



版权声明:本文为cd_yourheart原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。