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 working code :
#include <iostream>
#include <string>
#include "opencv2/core.hpp"
using namespace cv;
using namespace std;
int main (int, char** argv){
FileStorage fs("test.yml", FileStorage::WRITE);
int fps = 5;
fs << "FPS" << fps;
Mat m1 = Mat::eye(2,3, CV_32F);
Mat m2 = Mat::ones(3,2, CV_32F);
Mat result = (m1+1).mul(m1+3);
fs << "Result" << result;
fs.release();
FileStorage fs2("test.yml", FileStorage::READ);
Mat r;
fs2["Result"] >> r;
cout << r << endl;
fs2.release();
return 0;
}
What I have tried:
There's no error at all in the program, but there's no output (no matrix result output) when I run the program. Therefore I don't know where to go from here.
1. According to
OpenCV docs, adding File "open" is necessary, so I modified into this :
int main (int, char** argv){
FileStorage fs, fs2;
fs("test.yml", FileStorage::WRITE);
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 :
fs2.open("test.yml", FileStorage::READ);
Mat r;
fs2["Result"] >> r;
if (!r.data)
{
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 working directory. If this information is relevant, I compile using CMake. What did I miss here ?
UPDATE
3. I added cout to check if my matrices have correct values 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;
All matrices were printed out correctly. But the .*yml file won't be generated.
4. Last, 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. What did I miss here ?