1 | initial version |
Finally I read the manual. I don't how to use operator << and >> bu now I can save and read my own data using method write(fs) and read(fs).
#include <opencv2/opencv.hpp>
#include <iostream>
#include <map>
#include <fstream>
#include "B.h"
using namespace cv;
using namespace std;
int main (int argc,char **argv)
{
vector< Point> p;
B x,y;
x.entier["one"]=1;
x.entier["two"]=2;
x.reel["pi"]=acos(-1.0);
x.reel["e"]=exp(1);
{
FileStorage fs("test.yml", FileStorage::WRITE);
x.write(fs);
}
FileStorage fs("test.yml",FileStorage::READ);
y.read(fs["Class_B"]);
cout<<"Class B Entier\n";
for (auto it = y.entier.begin(); it != y.entier.end(); it++)
cout << it->first << "=" << it->second << "\n";
cout << "Class B reel\n";
for (auto it = y.reel.begin(); it != y.reel.end(); it++)
cout << it->first << "=" << it->second << "\n";
// fs<<"Class B"<<x; I don't know how to do this
};
and
#include "B.h"
using namespace cv;
using namespace std;
void B::write(cv::FileStorage& fs) const //Write serialization for this class
{
int i=0;
fs<<"mapint"<<"[";
for (auto iti = entier.begin(); iti != entier.end(); iti++, i++)
{
fs<<"{:"<<"key"<<iti->first<<"val"<<iti->second<<"}";
}
i=0;
fs << "]"<<"mapreel" << "[";
for (auto iti = reel.begin(); iti != reel.end(); iti++)
{
fs << "{:" << "key" << iti->first << "val" << iti->second << "}";
}
}
void B::read(const cv::FileNode& node) //Read serialization for this class
{
FileNode mapint = node["mapint"];
FileNodeIterator it = mapint.begin(), it_end = mapint.end();
for (; it != it_end; ++it)
{
entier[(string)(*it)["key"]] = (int)(*it)["val"];
}
FileNode mapreel = node["mapreel"];
FileNodeIterator itr = mapreel.begin(), itr_end = mapreel.end();
for (; itr != itr_end; ++itr)
{
reel[(string)(*itr)["key"]] = (double)(*itr)["val"];
}
}
and
#ifndef __CLASS_B__
#define __CLASS_B__
#include <opencv2/opencv.hpp>
#include <iostream>
#include <map>
#include <fstream>
#include <stdio.h>
class B {
public :
explicit B(){};
std::map< cv::String,int> entier;
std::map< cv::String,double> reel;
void write(cv::FileStorage& fs) const; //Write serialization for this class
void read(const cv::FileNode& node); //Read serialization for this class
};
#endif