Ask Your Question

FileStorage: can write but can't read Size, Rect, etc.

asked 2012-08-24 07:04:26 -0500

Baris Evrim Demiroz gravatar image

updated 2012-08-24 07:05:23 -0500

I am writing Size to a yaml file using FileStorage:

FileStorage fs("info.yml", FileStorage::WRITE);
fs << "gridSize" << Size(31,31);

The file content is after writing:

gridSize: [ 31, 31 ]

I cannot read the Size object again by doing:

Size gridSize;
FileStorage fs("info.yml", FileStorage::READ);
fs["gridSize"] >> gridSize;

Is this a bug? Lack of feature? Or is this completely planned behavior? What is the best way to read OpenCV data structures?

edit retag flag offensive close merge delete


A workaround would be to save integers: gridSizeWidth and gridSizeHeight, although that may look ugly

sammy gravatar imagesammy ( 2012-08-24 08:14:49 -0500 )edit

Did you find an acceptable solution for this problem? I'm experiencing the same thing and it will be annoying to have to alter my xml file.

Chris Dickson gravatar imageChris Dickson ( 2012-10-25 04:45:13 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-02-19 02:16:00 -0500

Marcos Nieto gravatar image

updated 2013-02-19 02:17:06 -0500

Hi guys. I think the cv::Rect class is written as an array of integers. So that

FileStorage fs(fileName, FileStorage::WRITE);   
Rect r(0,0,0,0);
fs << "myRectangle" << r;

Results in a file with an entry like this:

myRectangle: [ 0, 0, 0, 0 ]

You can access it reading it as a vector of int and (if needed) construct a cv::Rect with its data.

FileStorage fs(fileName, FileStorage::READ);
std::vector<int> rect;
fs["myRectangle"] >> rect;
cv::Rect myRect(rect[0], rect[1], rect[2], rect[3]);

It worked for me! Best regards.

edit flag offensive delete link more



Yeah I usually do something like this too - however I wonder if this could be done cleanly? The problem is also there for cv::Vec I think, maybe we should open a ticket?

pickle27 gravatar imagepickle27 ( 2013-08-03 19:39:23 -0500 )edit

Question Tools

1 follower


Asked: 2012-08-24 07:04:26 -0500

Seen: 2,756 times

Last updated: Feb 19 '13