Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I've found the solution. Old C functions cvStartWriteStruct and cvEndWriteStruct can do the job.

#include <opencv2/core/core_c.h>
...
Mat matrix;
FeatureDetector detector;
DescriptorExtractor extractor;

/* fill matrix, detector and extractor*/

cvStartWriteStruct(*fs, "detector", CV_NODE_MAP);
detector.write(fs);
cvEndWriteStruct(*fs);
FileStorage fs(file_name, FileStorage::WRITE);
fs << "matrix" << matrix;

This produces the following.

<opencv_storage>
<detector>
<name>Feature2D.ORB</name>
<WTA_K>2</WTA_K>
....
<scoreType>0</scoreType></detector>
<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
  ...

The reading code then would become

FileNode fn = fs["detector"];
if (!fn.empty()) {
    string name = fn["name"];
    ORB det;
    det.read(fn);
}

I've found the solution. Old C functions cvStartWriteStruct and cvEndWriteStruct can do the job.

#include <opencv2/core/core_c.h>
...
Mat matrix;
FeatureDetector detector;
DescriptorExtractor extractor;

/* fill matrix, detector and extractor*/

cvStartWriteStruct(*fs, "detector", CV_NODE_MAP);
detector.write(fs);
cvEndWriteStruct(*fs);
FileStorage fs(file_name, FileStorage::WRITE);
fs << "matrix" << matrix;

This produces the following.

<opencv_storage>
<detector>
<name>Feature2D.ORB</name>
<WTA_K>2</WTA_K>
....
<scoreType>0</scoreType></detector>
<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
  ...

The reading code then would become

FileNode fn = fs["detector"];
if (!fn.empty()) {
    string name = fn["name"];
    ORB det;
    det.read(fn);
}

UPDATE. C++ API can also do this. Same result can be achieved with

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

I've found the solution. Old C functions cvStartWriteStruct and cvEndWriteStruct can do the job.

#include <opencv2/core/core_c.h>
...
Mat matrix;
FeatureDetector detector;
DescriptorExtractor extractor;

/* fill matrix, detector and extractor*/

FileStorage fs(file_name, FileStorage::WRITE);
cvStartWriteStruct(*fs, "detector", CV_NODE_MAP);
detector.write(fs);
cvEndWriteStruct(*fs);
FileStorage fs(file_name, FileStorage::WRITE);
fs << "matrix" << matrix;

This produces the following.

<opencv_storage>
<detector>
<name>Feature2D.ORB</name>
<WTA_K>2</WTA_K>
....
<scoreType>0</scoreType></detector>
<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
  ...

The reading code then would become

FileNode fn = fs["detector"];
if (!fn.empty()) {
    string name = fn["name"];
    ORB det;
    det.read(fn);
}

UPDATE. C++ API can also do this. Same result can be achieved with

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