Ask Your Question
2

how do I save/load a cv::Rect ?

asked 2013-11-07 18:33:11 -0600

synthnassizer gravatar image

Hi all, newbiw with openCV here. I am trying to save/load a cv::Rect and was hoping it would happen in an easy way - just as is the case with cv::Mat.

ok saving works as expected:

FileStorage fs(ofToDataPath(filename.c_str()), FileStorage::APPEND);
fs << "boundRect" << aCvRect;

opening the file I'll see appended

boundRect: [ 271, 100, 56, 72 ]

but then I fail to use the "expected" way to load this variable. Ideally, this should work (as does with cv::Mat):

FileStorage fs(ofToDataPath(filename.c_str()), FileStorage::READ);
fs["boundRect"] >> loadedCvRect;

well the above does not compile saying:

../../../addons/ofxOpenCv/libs/opencv/include/opencv2/core/operations.hpp|2825|error: no matching function for call to ‘read(const cv::FileNode&, cv::Rect_<int>&, cv::Rect_<int>)’|

If saving didn't work as expected, I would simply choose another way to save the rect, but since saving works, it seems odd that the loading has not been implemented. Am I missing something here? and how can I load the Rect values?

If this is not implemented, what is the suggested way? To store x,y,w,h in a 2x2 Mat and then store that?

thank you for your help

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2013-11-14 09:27:38 -0600

synthnassizer gravatar image

The opencv cheat sheet came to the rescue. link text

it appears saving a Rect is easy, but loading it requires a few more steps:

FileNode mr = fs["myRest"];
Rect r; 

r.x = (int)mr[0];
r.y = (int)mr[1];
r.width = (int)mr[2];
r.height = (int)mr[3];

so good enough :)

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2013-11-07 18:33:11 -0600

Seen: 887 times

Last updated: Nov 14 '13