opencv 使用XML和YAML格式来输入输出文件

源码

链接 link

#include <opencv2/core.hpp>
#include <iostream>
#include <string>

using namespace cv;
using namespace std;

// 帮助信息函数
static void help(char** av)
{
   
    cout << endl
         << av[0] << " shows the usage of the OpenCV serialization functionality." << endl
         << "usage: " << endl
         << av[0] << " outputfile.yml.gz" << endl
         << "The output file may be either XML (xml) or YAML (yml/yaml). You can even compress it by "
         << "specifying this in its extension like xml.gz yaml.gz etc... " << endl
         << "With FileStorage you can serialize objects in OpenCV by using the << and >> operators" << endl
         << "For example: - create a class and have it serialized" << endl
         << " - use it to read and write matrices." << endl;
}

// 自定义数据结构 MyData
// 主要提供序列化和反序列化相关的读写功能
class MyData
{
   
public:
    MyData() : A(0), X(0), id() {
   }
    explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") {
   }
    
    void write(FileStorage& fs) const
    {
   
        fs << "{" << "A" << A << "X" << X << "id" << id << "}";
    }

    void read(const FileNode& node)
    {
   
        A = (int)node["A"];
        X = (double)node["X"];
        id = (string)node["id"];
    }

public:
    int A;
    double X;
    string id;
};

// 序列化和反序列化的辅助函数
static void write(FileStorage& fs, const std::string&, const MyData& x)
{
   
    x.write(fs);
}

static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData())
{
   
    if (node.empty())
        x = default_value
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值