Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to save/load matrix and parameters of several algorithms to a file?

Hi all. I've got a matrix cv::Mat, keypoint detector and descriptor extractor objects. And I want to save all of them to the single file using cv::FileStorage. Is it possible?

I tried

Mat matrix;
FeatureDetector detector;
DescriptorExtractor extractor;

/* fill matrix, detector and extractor*/

FileStorage fs(file_name, FileStorage::WRITE);
fs << "matrix" << matrix;
detector.write(fs);
extractor.write(fs);

This code writes everything to the file.

But the resulting file is invalid, since it contains two pairs of <name> </name> tags, and the code, reading data from disk, throws the exception "Duplicated key".

Here is the example of what I've got

<opencv_storage>
<name>Feature2D.ORB</name>
<WTA_K>2</WTA_K>
....  (other saved ORB parameters are skipped for brevity)
<name>Feature2D.BRISK</name>
<octaves>3</octaves>
<thres>30</thres>
<matrix type_id="opencv-matrix">
   <rows>500</rows>
   <cols>64</cols>
   <dt>u</dt>
   <data>
1 96 0 0 140 191 127 60 28 0 0 0 72 240 249 79 55 8 32 128 99 28 199
48 28 31 12 24 192 128 0 102 24 115 254 255 255 157 113 134 25 34 0
0 0 24 134 96 182 255 255 255 163 64 254 255 219 4 2 0 0 0 96 196 3
...

One possible solution could be wrapping the detector and extractor with custom tags, like

<opencv_storage>
<detector>
<name>Feature2D.ORB</name>
<WTA_K>2</WTA_K>
....
</detector>
<extractor>
<name>Feature2D.BRISK</name>
<octaves>3</octaves>
<thres>30</thres>
</extractor>
... (the rest of file)

Is it possible with FileStorage? I tried

fs << "detector";
detector.write(fs);

However, this code throws the exception "no element was given".