Ask Your Question
0

.yml file reading - complex structure

asked 2016-04-20 02:47:25 -0600

Alice gravatar image

updated 2016-04-20 03:11:58 -0600

Good morning everyone,

I wish to read data from .yml file. The structure is a bit deeper than the one showed in the example: http://docs.opencv.org/3.0-rc1/d4/da4...

Keeping same names as in the mentioned example, I have a file like this:

%YAML:1.0
features:
   -
      x: 103
      y: 166
      lbp:
         -
            a: 43
            b: 56
         -
            a: 23
            b: 87
         -
            a: 45
            b: 34
   -
      x: 115
      y: 113
      lbp:
         -
            a: 76
            b: 54
         -
            a: 42
            b: 2
         -
            a: 64
            b: 14
   -
      x: 586
      y: 12
      lbp:
         -
            a: 6
            b: 45
         -
            a: 34
            b: 34
         -
            a: 87
            b: 34

The code which read values x and y is:

FileStorage fs2("test.yml", FileStorage::READ);

FileNode features = fs2["features"];
FileNodeIterator it = features.begin(), it_end = features.end();
int idx = 0;

// iterate through a sequence using FileNodeIterator
for( ; it != it_end; ++it, idx++ )
{
    cout << "feature #" << idx << ": ";
    cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"];
    cout << endl;
}

fs2.release();

My question is - how can I read values a and b ? I know it must be simple, however I can't do that :P

-

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-04-20 03:21:31 -0600

berak gravatar image

you will need "nested" FileNodeIterators for this, one for the "features" node, and one for each "lbp" node:

FileStorage fs("test.yml",0);
FileNode features = fs["features"];
FileNodeIterator it = features.begin();
for (; it != features.end(); ++it)
{
    int x,y;
    (*it)["x"] >> x;
    (*it)["y"] >> y;
    cerr << "xy " << x << " " << y << endl;
    FileNode lbp = (*it)["lbp"];
    FileNodeIterator it2 = lbp.begin();
    for (; it2 != lbp.end(); ++it2)
    {
        int a,b;
        (*it2)["a"] >> a;
        (*it2)["b"] >> b;
        cerr << " ab " << a << " " << b << endl;
    }
}

// output:
xy 103 166
 ab 43 56
 ab 23 87
 ab 45 34
xy 115 113
 ab 76 54
 ab 42 2
 ab 64 14
xy 586 12
 ab 6 45
 ab 34 34
 ab 87 34
edit flag offensive delete link more

Comments

1

Works flawlessly - thank you :)

Alice gravatar imageAlice ( 2016-04-20 04:44:41 -0600 )edit

you were already on your way, all it needed was one more iteration..

berak gravatar imageberak ( 2016-04-20 04:49:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-20 02:47:25 -0600

Seen: 638 times

Last updated: Apr 20 '16