Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cv::FileStorage is a "key-value" store, to save different objects, you need unique keys, "name" isn't unique.

so, you either do, what @sturkmen proposes, give any item a unique name, or wrap it into "objects", like this:

Mat m1(10,10,CV_8UC3); m1=17;
Mat m2(6,8,CV_32F); m2=3;

FileStorage fs("my.yml", FileStorage::WRITE);

fs << "object1" << "{"; // an object with a unique name.
fs << "Date of storage" << "Wed Jul 26 16:45:46 2017\n";
fs << "Mat" << m1; // the "internal" keys can be as generic as you want
fs << "}";

fs << "object2" << "{";
fs << "Date of storage" << "Wed Jul 26 16:48:16 2017\n";
fs << "Mat" << m2;
fs << "}";

fs.release();

fs.open("my.yml", FileStorage::READ);
FileNode fn = fs["object2"];  // retrieve the object node
Mat M2;  String date;
fn["Mat"] >> M2;      // and its data
fn["Date of storage"] >> date;
cerr << date << endl;
cerr << M2 << endl;