Click here to Skip to main content
15,888,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :

C++
#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 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 :
C++
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 :

C++
 //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 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.
C++
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 ?
Posted
Updated 28-Feb-20 8:29am
v12
Comments
Richard MacCutchan 27-Feb-20 17:13pm    
The file will be stored in the same directory that the executable program is run from, or the directory that your terminal is set to when you run it. You can find it by searching from $HOME with the find command.
lock&_lock 27-Feb-20 17:44pm    
Yes, my working directory that I mentioned is also the directory where I run my terminal, where the executable file is there as well. I've also tried with manual searching, the file is not exist. I'm really confused now since I don't encounter any error. Even from debugging there's no crash report or anything.
Richard MacCutchan 27-Feb-20 17:53pm    
Put some more cout statements in your program so you can actually see what is happening when it runs.
lock&_lock 28-Feb-20 14:08pm    
Yep, done. There were printed out correctly, but no .yml file generated.
Richard MacCutchan 29-Feb-20 4:37am    
Sorry, I don't have OpenCV so cannot try a test for you. You could see if there isd a specialist forum anywhere that may be able to help.

1 solution

Time to debug your code.

You should put a breakpoint on the first line of the main method, and start a debug session. This will allow you to execute the code line-by-line, watching the values in your variables along the way. This allows you to spot where/when things start to differ from expectations, giving the chance to investigate why.

Search for a tutorial on debugging with your current IDE, if you are not familiar with the concept of debugging. You should try it, that's fun. And extremely useful and valuable. Debugging is an essential skill to a developper.
 
Share this answer
 
Comments
lock&_lock 27-Feb-20 14:48pm    
Yes, I'm not familiar with debugging and gonna have to search for tutorial on it, and actually I'm working on terminal, not IDE. I will update my progress, thank you.
lock&_lock 27-Feb-20 15:48pm    
Update : This is extremely hard.
phil.o 27-Feb-20 17:48pm    
Maybe you should use an IDE with proper debugging functionality?
lock&_lock 27-Feb-20 18:47pm    
Thanks, but to be honest I don't think installing an IDE just for debugging is the answer here because I already have debugging tool.
Earlier, I just went on to install 'gdb' for terminal debugging and read few tutorials on it. I tried several breakpoints (before writing file and before reading file) too and, there no crash, error or anything. Indeed it's not easy with gdb, not to mention I just learned how to use it like 3 hours ago. I agree it is an essential skill and I'm planning to learn more about gdb.
But if I still need to even install an entire IDE, do setup and learn how to debug with it just for debugging purpose (since I know I'm not gonna use IDE for writing code), I don't think it's worth it. I'm stuck and super confused, it's supposed to be an easy code, just to read and write file.
No, I'm not giving up, for now I'm just gonna take a break and get back to it again later. Again, thanks a lot for your reply and debugging advice. I'll update my post when I have progress.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900