opencv轻松读写yaml/xml文件

有时候我们需要把参数写入文档,利用opencv库可以轻松实现读写yaml/xml文件,相对于txt文件,参数可以更加直观。(以下部分代码需要编译器支持C++11)

样例1(单个变量)

写文件

#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
	FileStorage fs("test.yaml", FileStorage::WRITE); //换成xml效果一样,yaml文件格式相对更直观
	//FileStorage fs("test.xml", FileStorage::WRITE);

	fs << "v" << 1.23;

	fs.release();

	std::cout << "文件读写完毕!" << std::endl;

	return 0;
}

test.yaml文件结果

%YAML:1.0
---
v: 1.2300000000000000e+00

读文件

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main()
{
    FileStorage fs("test.yaml", FileStorage::READ); //换成xml效果一样,yaml文件格式相对更直观
    //FileStorage fs("test.xml", FileStorage::READ);

    if (!fs.isOpened())
    {
        cout << "No file!" << endl;
        return false;
    }

    double v = (double)fs["v"];
    cout << "v: " << v << endl;

    fs.release();

    std::cout << "文件读取完毕!" << std::endl;

    return 0;
}

样例2(多个变量)

写文件

#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
	FileStorage fs("test.yaml", FileStorage::WRITE); //换成xml效果一样,yaml文件格式相对更直观
	//FileStorage fs("test.xml", FileStorage::WRITE);

	fs << "v" << 1.23;

	Mat Matrix1 = (Mat_<double>(3, 3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
	Mat Matrix2 = (Mat_<double>(3, 3) << 100, 0, 32, 0, 100, 24, 0, 0, 0.1);
	std::vector<Mat> Mat_vector;

	Mat_vector.push_back(Matrix1);
	Mat_vector.push_back(Matrix2);

	fs << "Matrix1" << Matrix1;
	fs << "Mat_vector"
	   << "[" << Mat_vector << "]";

	Point3d p1(1.1, 2.2, 3.3);
	Point3d p2(1.11, 2.22, 3.33);
	std::vector<Point3d> point_vector;
	point_vector.push_back(p1);
	point_vector.push_back(p2);

	fs << "p1" << p1;
	fs << "point_vector"
	   << "[" << point_vector << "]";

	fs.release();

	std::cout << "文件读写完毕!" << std::endl;

	return 0;
}

test.yaml文件结果

%YAML:1.0
---
v: 1.2300000000000000e+00
Matrix1: !!opencv-matrix
   rows: 3
   cols: 3
   dt: d
   data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
Mat_vector:
   -
      - !!opencv-matrix
         rows: 3
         cols: 3
         dt: d
         data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
      - !!opencv-matrix
         rows: 3
         cols: 3
         dt: d
         data: [ 100., 0., 32., 0., 100., 24., 0., 0.,
             1.0000000000000001e-01 ]
p1: [ 1.1000000000000001e+00, 2.2000000000000002e+00,
    3.2999999999999998e+00 ]
point_vector:
   - [ 1.1000000000000001e+00, 2.2000000000000002e+00,
       3.2999999999999998e+00, 1.1100000000000001e+00,
       2.2200000000000002e+00, 3.3300000000000001e+00 ]

读文件

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main()
{
    FileStorage fs("test.yaml", FileStorage::READ); //换成xml效果一样,yaml文件格式相对更直观
    //FileStorage fs("test.xml", FileStorage::READ);

    if (!fs.isOpened())
    {
        cout << "No file!" << endl;
        return false;
    }

    double v = (double)fs["v"];
    cout << "v: " << v << endl;

    Mat Matrix1;
    fs["Matrix1"] >> Matrix1;
    cout << "Matrix1: " << Matrix1 << endl;

    vector<Mat> Mat_vector;
    fs["Mat_vector"][0] >> Mat_vector;
    cout << "Mat_vector: " << endl;
    for (Mat temp : Mat_vector)
    {
        cout << temp << endl;
    }

    Point3d p1;
    fs["p1"] >> p1;
    cout << "p1: " << p1 << endl;

    vector<Point3d> point_vector;
    fs["point_vector"][0] >> point_vector;
    cout << "point_vector: " << endl;
    for (Point3d temp : point_vector)
    {
        cout << temp << endl;
    }

    fs.release();

    std::cout << "文件读取完毕!" << std::endl;

    return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值