How do I write into YML format and read it with opencv and C++ ?

asked 2020-02-28 13:09:37 -0500

raisa_ gravatar image

updated 2020-02-28 13:23:23 -0500

I'm following an OpenCV book tutorial to write and read into File Storage. It's supposed to write a file named "test.yml", then store some value in it and read that file again. Here's the code from the book :

#include <iostream>
#include <string>
#include "opencv2/core.hpp"
using namespace cv;
using namespace std;

int main (int, char** argv){
        //create writer
        FileStorage fs("test.yml", FileStorage::WRITE);

        //save an int
        int fps = 5;
        fs << "FPS" << fps;

        //create some Mat sample
        Mat m1 = Mat::eye(2,3, CV_32F);
        Mat m2 = Mat::ones(3,2, CV_32F);
        Mat result = (m1+1).mul(m1+3);

        //write result
        fs << "Result" << result;

        //release file
        fs.release();

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

        Mat r;
        fs2["Result"] >> r;
        cout << r << endl;
        fs2.release();
        return 0;

}

WHAT HAVE I TRIED :

There's no error when I run the program, but there's no output (blank) and no *.yml file generated. Therefore I don't know where to go from here. However, I've been trying several things :

  1. According to OpenCV docs, adding "open" is necessary when reading, so I modified into this :

    int main (int, char** argv){
    
        //create writer
        FileStorage fs, fs2;
        fs("test.yml", FileStorage::WRITE);
    
        //...
    
        //create reader
        fs2.open("test.yml", FileStorage::READ);
    

    But still empty result.

  2. Then I tried to check if there's actually any data in it. I added checking code :

    //create reader
    fs2.open("test.yml", FileStorage::READ);
    
    Mat r;
    fs2["Result"] >> r;
    
    if (!r.data)
    {
            //Fails
            cout << "Could not open file. " << endl;
            return -1;
    }
    cout << r << endl;
    fs2.release();
    return 0;
    

    It return "Could not open file". Turn out, there's no *.yml file generated in my active / project working directory. If this information is relevant, I compile using CMake in terminal Ubuntu.

  3. Added cout to check if my matrices have the correct value and are actually there.

     cout << "FPS " << fps << endl;
    
     cout << "Matrix One :" <<"\n" << m1 << endl;
     cout << "Matrix Two :" <<"\n" << m2 << endl;
     cout << "Result :" <<"\n" << result << endl;
    

    The matrices were printed out correctly. But the .*yml file won't be generated.

  4. I thought maybe my code has some bugs, so I tried to run example code from OpenCV Docs : XML/YAML Persistence, and I encountered the same thing. I can run the program, there's no error but no *.yml file generated. I've been dealing with this for 2 days, what did I miss here ?

edit retag flag offensive close merge delete

Comments

no problem on the code (tested with OpenCV 4.2.0 on Windows)

sturkmen gravatar imagesturkmen ( 2020-02-28 16:41:32 -0500 )edit

@sturkmen Did you manage to write and read .yml file ? is there any .yml file generated ? I'm also using OpenCV2.

raisa_ gravatar imageraisa_ ( 2020-02-28 23:43:00 -0500 )edit

i get the following output

%YAML:1.0
---
FPS: 5
Result: !!opencv-matrix
   rows: 2
   cols: 3
   dt: f
   data: [ 8., 3., 3., 3., 8., 3. ]
sturkmen gravatar imagesturkmen ( 2020-02-29 05:51:33 -0500 )edit

@sturkmen Yes ! that's how the output is supposed to be ! But mine is blank, and there's no .yml file generated everywhere. I even have searched with native file searching and there's no test.yml everywhere. May I know what tools are you using ? IDE or also a terminal ?

raisa_ gravatar imageraisa_ ( 2020-02-29 09:59:54 -0500 )edit

give full path here FileStorage fs("c:/temp/test.yml", FileStorage::WRITE);

LBerger gravatar imageLBerger ( 2020-02-29 11:44:07 -0500 )edit

@LBerger Just tried it. Doesn't work.

raisa_ gravatar imageraisa_ ( 2020-02-29 12:02:42 -0500 )edit