Ask Your Question
3

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

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

Baris Evrim Demiroz gravatar image

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

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:

%YAML:1.0
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

Comments

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

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

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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
5

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

Marcos Nieto gravatar image

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

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:

%YAML:1.0
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

Comments

1

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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 3,584 times

Last updated: Feb 19 '13