Trying to parse OpenCV YAML with yaml-cpp

I have a series of OpenCv generated YAML files and would like to analyze them using yaml-cpp

Everything is fine with simple things, but the matrix representation is complicated.

# Center of table
tableCenter: !!opencv-matrix
   rows: 1
   cols: 2
   dt: f
   data: [ 240,    240]

This should map to vector

240
240

with type float . My code looks like this:

#include "yaml.h"
#include <fstream>
#include <string>

struct Matrix {
    int x;
};

void operator >> (const YAML::Node& node, Matrix& matrix) {
   unsigned rows;
   node["rows"] >> rows;
}

int main()
{
   std::ifstream fin("monsters.yaml");
   YAML::Parser parser(fin);
   YAML::Node doc;

    Matrix m;
    doc["tableCenter"] >> m;

   return 0;
}

But I get

terminate called after throwing an instance of 'YAML::BadDereference'
  what():  yaml-cpp: error at line 0, column 0: bad dereference
Abort trap

I searched for any documentation for yaml-cpp, but there seems to be none other than a short introductory example for parsing and emitting. Unfortunately, none of these two helps in this particular case.

As I understand it !! indicate that this is a user type, but I don't see with yaml-cpp how to parse this.

+3
source share
1 answer

yaml-cpp, . ++ , , , - . node , ( ).

, OpenCV, - :

class Matrix {
public:
   Matrix(unsigned r, unsigned c, const std::vector<float>& d): rows(r), cols(c), data(d) { /* init */ }
   Matrix(const Matrix&) { /* copy */ }
   ~Matrix() { /* delete */ }
   Matrix& operator = (const Matrix&) { /* assign */ }

private:
   unsigned rows, cols;
   std::vector<float> data;
};

-

void operator >> (const YAML::Node& node, Matrix& matrix) {
   unsigned rows, cols;
   std::vector<float> data;
   node["rows"] >> rows;
   node["cols"] >> cols;
   node["data"] >> data;
   matrix = Matrix(rows, cols, data);
}

. , ; , YAML::Node. :

std::ifstream fin("monsters.yaml");
YAML::Parser parser(fin);
YAML::Node doc;
parser.GetNextDocument(doc); // <-- this line was missing!

Matrix m;
doc["tableCenter"] >> m;

. , dt: f " - float". , , Matrix . ( ), , , . ( , , , .)

+4

Source: https://habr.com/ru/post/1741923/


All Articles