Ask Your Question
1

How to read JSON files using OpenCV FileStorage with C++ Program

asked 2018-01-21 09:18:17 -0600

Sakthivel S gravatar image

updated 2018-01-21 11:18:52 -0600

I'm new to OpenCV and also C++ Programming. I have multiple JSON files and have read each file and store the values in vectors or arrays. I'm not sure which is best way store the values, since json file has huge number of comma separated values and need to store each value in three parameter structure.

Example JSON file,

{
    "version":1.0,
    "points":[
        {
            "val1":[
                516.85,444.377,0.859822,539.18,466.636,0.790611,477.802,455.542,0.847968,366.262,461.046,0.805154,321.499,410.842,0.783296
            ],
            "val2":[

            ]

        },
        {
            "val1":[
                0,0,0,0,0,0,1125.33,586.703,0.0834661,1013.77,589.487,0.311553,924.413,597.834,0.348648
            ],
            "val2":[

            ]
        }
    ]
}

I have t oread only val1 using json and the json file may have multiple entries of val1. I have to read all val1 values and store it an array/vector. After reading val1, have to store combination of three values in three parameter structure. So, val1 itself will be stored in an array of structures and second val1 also should store in another continuous array of structure. And each file should store in continuous array of structure.

Expected results as follows, val1 : 516.85,444.377,0.859822,539.18,466.636,0.790611,477.802,455.542,0.847968,366.262,461.046,0.805154,321.499,410.842,0.783296

struct a { float a, float b, float c }

struct a arr[][];

arr[0][0] = {a=516.85, b = 444.377, c=0.859822} arr[0][1] = {a=539.18, b=466.636, c=0.790611}

val2 : 0,0,0,0,0,0,1125.33,586.703,0.0834661 arr[1][0] = {a=0. b=0, c=0} arr[1][1] = {a=0, b=0, c=0} arr[1][2] = {a=1125.33, b=586.703, =0.0834661}

Please let me know if any solution available.

Thanks in advance.

edit retag flag offensive close merge delete

Comments

1
  • please, replace the image with a text version, so folks here can actually try it.

  • is that the whole file ?

  • do your json files have all the same structure ? like: 2 val1 entries a 15 elements ?
  • do you have to use arrays like this ? (you would need to pre-allocate those before, and you probably don't know, how many you need)
berak gravatar imageberak ( 2018-01-21 10:18:17 -0600 )edit
1

i have removed image. yes. some files have multiple val1 entries and 15 elements is not fixed, it may vary as well. Is it possible to use dynamically growing dictionary / any other concept ?

Sakthivel S gravatar imageSakthivel S ( 2018-01-21 11:21:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-01-22 03:56:04 -0600

berak gravatar image

something like this should get you going:

struct data {
    float a,b,c;
};

int main(int argc, char** argv)
{
    vector<vector<data> > D; // use a dynamic container
    FileStorage fs("x.json", 0);
    FileNode root = fs["points"];
    for (int i=0; i<root.size(); i++) {
        FileNode val1 = root[i]["val1"]; // we only want the content of val1
        vector<data> row;
        for (int j=0; j<val1.size(); j+=3) { // read 3 consecutive values
            data  d; 
            d.a = val1[j].real();
            d.b = val1[j+1].real();
            d.c = val1[j+2].real();
            row.push_back(d);
        }
        D.push_back(row);
    }
    ....
edit flag offensive delete link more

Comments

Thanks a lot

Sakthivel S gravatar imageSakthivel S ( 2018-01-29 03:29:55 -0600 )edit

Does the code snippet work? I want to use json in OpenCV too.

jiangjixiang gravatar imagejiangjixiang ( 2018-09-20 07:09:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-21 09:18:17 -0600

Seen: 6,977 times

Last updated: Jan 22 '18