提问者:小点点

OpenCV yaml:如何读写嵌套(递归)yaml文件


我想写一个嵌套的yaml文件。 类似于以下内容:

features:
      x: 103
      y: 166
      lbp: "no"
      depth: 
             cam1: "no"
             cam2: "yes"
             lidar: "yes"
      dim: 3

我有一个代码写简单的结构,但我不明白如何写嵌套与它。 我找到了一种读取嵌套YAML的方法:https://answers.opencv.org/question/93257/yml-file-reading-complex-structure/

但是似乎找不到如何编写嵌套的YAML。

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

using namespace cv;
using namespace std;

int main()
{
    const string out_fname = "/tmp/test.yaml";

    cout << "Write YAML file: " << out_fname << endl;
    cv::FileStorage opencv_file( out_fname, cv::FileStorage::WRITE);
    cv::Mat file_matrix;
    file_matrix = (cv::Mat_<int>(3, 3) << 1, 2, 3,
                                            3, 4, 6,
                                            7, 8, 9);
    opencv_file << "my_matrix" << file_matrix;

    opencv_file << "noise" << 43.3;
    opencv_file << "noise_type" << "Gaussian";
    opencv_file << "dim" << 3;

    // doesnt seem to work....!
    // opencv_file["feature"]["camera"] = 2.3;
    // opencv_file["feature"]["imu"] = 10;

    opencv_file.writeComment("a double value", 0);



    opencv_file.release();
}

共1个答案

匿名用户

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

using namespace cv;
using namespace std;

int main()
{
    const string out_fname = "/tmp/test.yaml";

    cout << "Write YAML file: " << out_fname << endl;
    cv::FileStorage opencv_file( out_fname, cv::FileStorage::WRITE);
    cv::Mat file_matrix;
    file_matrix = (cv::Mat_<int>(3, 3) << 1, 2, 3,
            3, 4, 6,
            7, 8, 9);
    opencv_file << "my_matrix" << file_matrix;

    opencv_file << "noise" << 43.3;
    opencv_file << "noise_type" << "Gaussian";
    opencv_file << "dim" << 3;

    opencv_file << "feature";                             
    opencv_file << "{" << "camera" << 2.3;
    opencv_file <<        "imu" << 10 << "}";
    opencv_file.release();
}

输出:

%YAML:1.0
my_matrix: !!opencv-matrix
   rows: 3
   cols: 3
   dt: i
   data: [ 1, 2, 3, 3, 4, 6, 7, 8, 9 ]
noise: 4.3299999999999997e+01
noise_type: Gaussian
dim: 3
feature:
   camera: 2.2999999999999998e+00
   imu: 10