How do I write into YML format and read it with opencv and C++ ?
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 :
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.
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.
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.
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 ?
no problem on the code (tested with OpenCV 4.2.0 on Windows)
@sturkmen Did you manage to write and read .yml file ? is there any .yml file generated ? I'm also using OpenCV2.
i get the following output
@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 ?
give full path here FileStorage fs("c:/temp/test.yml", FileStorage::WRITE);
@LBerger Just tried it. Doesn't work.