Ask Your Question
0

Confusion over FileStorage syntax

asked 2015-08-28 16:37:40 -0500

viktor gravatar image

updated 2015-08-29 09:40:00 -0500

Hallo friends,

I'm confused over how to use FileStorage in order to write a sequence of mappings to a file.
Here's an example:

vector<pair<uint, Point>> vec; // holds results of video analysis
// append pairs to vec...
FileStorage fs(file, FileStorage::WRITE);   
fs << "Positions" << "[";
    for (Result::iterator i = vec.begin(); i != vec.end(); ++i)
    {
        fs << "{" 
            << "Frame" << (int)i->first
            << "X" << i->second.x 
            << "Y" << i->second.y 
            << "}";
    }
fs << "]";

When I'm running the above code I trigger the following CV_Error (persistence.hpp:1064):

if( fs.state == FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP )
        CV_Error( Error::StsError, "No element name has been given" );

While trying to write the first element of the mapping. Of course I've checked the docs (see: http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html) but was suprised to see, that they basically do the exact same thing there.
Have they changed the syntax in OpenCV3.0 or am I overlooking something here?

I've already recompiled the current master branch (faa6684) after cleaning the build directory.
I'm running a Win 7 x64 with VS2012 as a compiler.

Many thanks,
Viktor

edit retag flag offensive close merge delete

Comments

fs << "{:" // it needs a colon there

berak gravatar imageberak ( 2015-08-28 23:57:45 -0500 )edit

I have tried that already. Still triggers CV_Error. Also, the docs state: To store a mapping/sequence in a compact form, put ”:” after the opening character. So the colon operator is anyway just a YAML specific thing.
Are you able to reproduce this behaviour with the latest master branch @berak?

viktor gravatar imageviktor ( 2015-08-29 07:27:40 -0500 )edit

the colon also applies, when you save to xml

berak gravatar imageberak ( 2015-08-29 09:46:05 -0500 )edit

From what I've read, they are being ignored when writing XML. The 2.4.11 doc reads: When the data is written to XML, those extra ”:” are ignored. Anyway, I don't think this is the cause of my problems, since I receive the exact same error no matter if colon present or not.
Anybody able to reproduce the error?

viktor gravatar imageviktor ( 2015-08-29 10:29:01 -0500 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2015-08-29 10:36:23 -0500

LorenaGdL gravatar image

updated 2015-08-29 11:02:52 -0500

Not really an answer! I CAN'T reproduce the error either with 2.4.12 or with 3.0.0 using this code:

void main(){
    vector<pair<uint, Point>> vec;
    pair<uint, Point> myFirstPair(3, Point(5, 6));
    pair<uint, Point> mySecondPair(87, Point(54, 67));
    vec.push_back(myFirstPair);
    vec.push_back(mySecondPair);

    FileStorage fs("mySavedFile.xml", FileStorage::WRITE);
    fs << "Positions" << "[";
    for (vector<pair<uint, Point>>::iterator i = vec.begin(); i != vec.end(); ++i)
    {
        fs << "{"
            << "Frame" << (int)i->first
            << "X" << i->second.x
            << "Y" << i->second.y
            << "}";
    }
    fs << "]";
}

Are you giving a name to the .xml/.yml file? I guessed that file in your FileStorage fs(file, FileStorage::WRITE); referred to a previously defined string but I'm not so sure now.

edit flag offensive delete link more

Comments

Thanks for your reply.
Yes, I'm giving a name to the output file. file is a std::string I construct using code from Windows' "Shlwapi.h". I've observed that I'm able to emmit single element to the FileStorage. Only when writing sequences of mappings I trigger the warning. What OS are you running?
Good to know you are unable to reproduce that error. This is the only time I'm using FileStorage though, so I'm kind of clueless on what else could lead to this problem.

viktor gravatar imageviktor ( 2015-08-29 10:55:46 -0500 )edit

By the way, I've run your example code and encountered the same error :(

viktor gravatar imageviktor ( 2015-08-29 10:58:58 -0500 )edit

I'm on Windows 7 and Visual Studio. I'm not familiar with Shlwapi.h. Sorry, I don't have any ideas on the problem either

LorenaGdL gravatar imageLorenaGdL ( 2015-08-29 11:02:13 -0500 )edit

Shlwapi.h is used for windows-based path operation. Can be quite usefull e.g. when extracting a file extension and so on. But this has nothing to do with the error.
Are you using a pre-build version? You've wrote that you are using 3.0.0, so this may be be a bug introduced in the latest commits?

viktor gravatar imageviktor ( 2015-08-29 11:14:23 -0500 )edit

I was using a custom built version forked some weeks ago from the github repo. Just in case, I've just build the latest version (last commit still faa6684) and the code still runs without problems.

LorenaGdL gravatar imageLorenaGdL ( 2015-08-29 12:07:26 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-08-28 16:37:40 -0500

Seen: 238 times

Last updated: Aug 29 '15