简体   繁体   中英

How to create this yml file in c++ (using opencv)

I have a yml file which is created in Matlab and some other application can read it without any problem.

the generated yml file is as follow:

%YAML:1.0
myitems:
  -
    l: !!opencv-matrix
      rows: 4
      cols: 1
      dt: d
      data: [1.6040597671471776e+03, -5.9317478925963056e-02, -4.0214684178724552e-04, -1.0686329088084243e-03]
    c: !!opencv-matrix
      rows: 2
      cols: 1
      dt: d
      data: [1.3524793229822997e+03, 1.8265878647908009e+03]
    t: !!opencv-matrix
      rows: 4
      cols: 4
      dt: d
      data: [9.3969262078590843e-01, 0.0000000000000000e+00, -3.4202014332566871e-01, -4.2438920171761561e-02, 0.0000000000000000e+00, 1.0000000000000000e+00, 0.0000000000000000e+00, 0.0000000000000000e+00, 3.4202014332566871e-01, 0.0000000000000000e+00, 9.3969262078590843e-01, 0.0000000000000000e+00, 0.0000000000000000e+00, 0.0000000000000000e+00, 0.0000000000000000e+00, 1.0000000000000000e+00]
  -
    l: !!opencv-matrix
      rows: 4
      cols: 1
      dt: d
      data: [1.6035513799260334e+03, -5.8611163583081945e-02, -1.9961041844717854e-03, -5.7772898125416506e-04]
    c: !!opencv-matrix
      rows: 2
      cols: 1
      dt: d
      data: [1.3748405387479991e+03, 1.8628349599457656e+03]
    t: !!opencv-matrix
      rows: 4
      cols: 4
      dt: d
      data: [-3.5201238807168519e-03, 9.3725029294111806e-01, -3.4863949447768083e-01, -4.2438920171761561e-02, -9.9998222086817701e-01, -1.6211348792076591e-03, 5.7384553018963221e-03, 0.0000000000000000e+00, 4.8131772679647451e-03, 3.4865349604369644e-01, 9.3723933604021081e-01, 0.0000000000000000e+00, 0.0000000000000000e+00, 0.0000000000000000e+00, 0.0000000000000000e+00, 1.0000000000000000e+00]
  -
    l: !!opencv-matrix
      rows: 4
      cols: 1
      dt: d
      data: [1.5975668104752576e+03, -6.0221428459880273e-02, 8.9637904632662471e-04, -1.4944214307671109e-03]
    c: !!opencv-matrix
      rows: 2
      cols: 1
      dt: d
      data: [1.3643523108057761e+03, 1.8451412053529805e+03]
    t: !!opencv-matrix
      rows: 4
      cols: 4
      dt: d
      data: [-9.4399816022253547e-01, -6.0251161920311046e-04, -3.2995016362507984e-01, -4.2438920171761561e-02, 1.5488563810861311e-03, -9.9999540679978660e-01, -2.6052683623591024e-03, 0.0000000000000000e+00, -3.2994707839345838e-01, -2.9704139573240884e-03, 9.4399475745395067e-01, 0.0000000000000000e+00, 0.0000000000000000e+00, 0.0000000000000000e+00, 0.0000000000000000e+00, 1.0000000000000000e+00]
  -
    l: !!opencv-matrix
      rows: 4
      cols: 1
      dt: d
      data: [1.5966964716049326e+03, -5.7396164460785576e-02, -1.9997766878354986e-03, -6.3293353675991561e-04]
    c: !!opencv-matrix
      rows: 2
      cols: 1
      dt: d
      data: [1.3583936708783401e+03, 1.8439285997544673e+03]
    t: !!opencv-matrix
      rows: 4
      cols: 4
      dt: d
      data: [-1.0822465368656472e-03, -9.3985931913280085e-01, -3.4156008107163421e-01, -4.2438920171761561e-02, 9.9999940770171103e-01, -9.7771219684243270e-04, -4.7819984029824587e-04, 0.0000000000000000e+00, 1.1549311909389662e-04, -3.4156039629630369e-01, 9.3985982058137330e-01, 0.0000000000000000e+00, 0.0000000000000000e+00, 0.0000000000000000e+00, 0.0000000000000000e+00, 1.0000000000000000e+00]

I have this code in opencv (C++) to create the same yml file:

 cv::FileStorage fs(fileName, cv::FileStorage::WRITE);
 fs << "myitems";
 fs<<"{" ;
 for(int i=0;i<4;i++)
 {
     fs<<"-" ;
     fs<<"{" ;

     fs<<"l"<<cv::Mat(l);
     fs<<"c"<<cv::Mat(c);
     fs<<"t"<<cv::Mat(t);
     fs << "}" ;
  }
  fs << "}" ;

but when I am running this code, I am getting error: Incorrect element name on the following line:

fs<<"-";

How can I create a similar yml file in opencv?

Please note that as the file is generated by matlab and used in other applications, I can not change the format of yml file.

Here is how to create a similar YAML file :

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

int main()
{
    cv::FileStorage fs("out.yaml", cv::FileStorage::WRITE);
    fs << "myitems";
    fs << "[" ;
    for(int i=0; i<4; i++) {
        fs << "{" ;
        fs << "l" << cv::Mat::zeros(4,1,CV_64F);
        fs << "c" << cv::Mat::zeros(2,1,CV_64F);
        fs << "t" << cv::Mat::zeros(4,4,CV_64F);
        fs << "}" ;
    }
    fs << "]" ;

    return 0;
}

The top level node "myitems" is a sequence of 4 nodes ( [ , ... , ] ). Each sequence item is an unnamed mapping ( { , ... , } ). Each of those mappings contains three matrices named "l", "c", and "t".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM