nasil122002
nasil122002

Reputation: 145

Read data from yaml file issue - opencv

I have a data in .yml file format, which looks like following:

 %YAML:1.0
 rotMatrix1:
 - !!opencv-matrix
  rows: 3
  cols: 1
  dt: d
  data: [ -2.3829520379560337e-01, -3.7857313177578661e-01, -1.2002438267340013e-01 ]
- !!opencv-matrix
  rows: 3
  cols: 1
  dt: d
  data: [ -1.9532717166408006e-01, -1.9842197713512208e-01, -2.4142492122139054e-02 ]
- !!opencv-matrix
  rows: 3
  cols: 1
  dt: d
  data: [ 7.0848561493555007e-02, -1.5300625945509461e-01, -2.2789227495909796e-03 ]
- !!opencv-matrix
  rows: 3
  cols: 1
  dt: d
  data: [ -6.4432249078642076e-02, 2.6156730119463567e-01, -1.0216960054886982e-01 ]

The file contains only one node name (rotMatrix1) but under this node e.g 10 different data are stored. How can I access the individual 1x3 matrices with using opencv?

What I already tried is to use cv::FileNodeIterator but I got noting

FileStorage fs(inputData, CV_STORAGE_READ)
FileNode fn= fs["rotMatrix1"];

for(cv::FileNodeIterator it= fn.begin(); it!= fn.end(); ++it)
{
  cv::FileNode node= *it;
  double data= (double)node
}
fs.release();

Thanks for any help!

Upvotes: 0

Views: 4236

Answers (1)

Miki
Miki

Reputation: 41775

You can parse the file (the full file is linked in comments and is available here) with the code below.

I recommend to follow the tutorials here and here to become familiar with the concepts of FileStorage, FileNode and FileNodeIterator.

Note that I had to copy & paste the file content in a new file, because of some encoding issues with original file.

Code:

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

int main()
{
    FileStorage fs("calib.yml", FileStorage::READ);

    string time;
    int calibrationImageWidth;
    int calibrationImageHeight;
    int numberOfCrossPointsInWidth;
    int numberOfCrossPointsInHeight;
    double squareSize;
    int numberOfCalibratedImages;
    Mat cameraMatrix1;
    Mat distortionCoefficient1;
    vector<Mat> rotationMatrix1;
    vector<Mat> translationMatrix1;

    // Read data
    FileNode fn_time = fs.root();
    time = fn_time["time"];

    calibrationImageWidth = fs["calibrationImageWidth"];
    calibrationImageHeight = fs["calibrationImageHeight"];
    numberOfCrossPointsInWidth = fs["numberOfCrossPointsInWidth"];
    numberOfCrossPointsInHeight = fs["numberOfCrossPointsInHeight"];
    squareSize = fs["squareSize"];
    numberOfCalibratedImages = fs["numberOfCalibratedImages"];

    fs["cameraMatrix1"] >> cameraMatrix1;
    fs["distortionCoefficient1"] >> distortionCoefficient1;

    FileNode fn_rot = fs["rotationMatrix1"];
    FileNodeIterator fn_rot_it = fn_rot.begin(), fn_rot_it_end = fn_rot.end();
    for (; fn_rot_it != fn_rot_it_end; ++fn_rot_it)
    {
        Mat tmp;
        (*fn_rot_it) >> tmp;

        rotationMatrix1.push_back(tmp.clone());
    }

    FileNode fn_tr = fs["translationMatrix1"];
    FileNodeIterator fn_tr_it = fn_tr.begin(), fn_tr_it_end = fn_tr.end();
    for (; fn_tr_it != fn_tr_it_end; ++fn_tr_it)
    {
        Mat tmp;
        (*fn_tr_it) >> tmp;

        translationMatrix1.push_back(tmp.clone());
    }

    return 0;
}

Upvotes: 1

Related Questions