2 * filestorage_sample demonstrate the usage of the opencv serialization functionality
5 #include "opencv2/core/core.hpp"
16 static void help(char** av)
18 cout << "\nfilestorage_sample demonstrate the usage of the opencv serialization functionality.\n"
20 << av[0] << " outputfile.yml.gz\n"
21 << "\n outputfile above can have many different extenstions, see below."
22 << "\nThis program demonstrates the use of FileStorage for serialization, that is use << and >> in OpenCV\n"
23 << "For example, how to create a class and have it serialize, but also how to use it to read and write matrices.\n"
24 << "FileStorage allows you to serialize to various formats specified by the file end type."
25 << "\nYou should try using different file extensions.(e.g. yaml yml xml xml.gz yaml.gz etc...)\n" << endl;
34 explicit MyData(int) :
35 A(97), X(CV_PI), id("mydata1234")
41 void write(FileStorage& fs) const //Write serialization for this class
43 fs << "{" << "A" << A << "X" << X << "id" << id << "}";
45 void read(const FileNode& node) //Read serialization for this class
49 X = (double)node["X"];
50 id = (string)node["id"];
54 //These write and read functions must exist as per the inline functions in operations.hpp
55 static void write(FileStorage& fs, const std::string&, const MyData& x){
58 static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){
65 static ostream& operator<<(ostream& out, const MyData& m){
66 out << "{ id = " << m.id << ", ";
67 out << "X = " << m.X << ", ";
68 out << "A = " << m.A << "}";
71 int main(int ac, char** av)
79 string filename = av[1];
83 FileStorage fs(filename, FileStorage::WRITE);
85 cout << "writing images\n";
86 fs << "images" << "[";
88 fs << "image1.jpg" << "myfi.png" << "baboon.jpg";
89 cout << "image1.jpg" << " myfi.png" << " baboon.jpg" << endl;
93 cout << "writing mats\n";
94 Mat R =Mat_<double>::eye(3, 3),T = Mat_<double>::zeros(3, 1);
95 cout << "R = " << R << "\n";
96 cout << "T = " << T << "\n";
100 cout << "writing MyData struct\n";
108 FileStorage fs(filename, FileStorage::READ);
112 cerr << "failed to open " << filename << endl;
117 FileNode n = fs["images"];
118 if (n.type() != FileNode::SEQ)
120 cerr << "images is not a sequence! FAIL" << endl;
124 cout << "reading images\n";
125 FileNodeIterator it = n.begin(), it_end = n.end();
126 for (; it != it_end; ++it)
128 cout << (string)*it << "\n";
132 cout << "reading R and T" << endl;
137 cout << "R = " << R << "\n";
138 cout << "T = " << T << endl;
143 cout << "read mdata\n";
146 cout << "attempting to read mdata_b\n"; //Show default behavior for empty matrix
148 cout << "read mdata_b\n";
153 cout << "Try opening " << filename << " to see the serialized data." << endl << endl;
157 cout << "Read data from string\n";
162 " X: 3.1415926535897931e+00\n"
165 FileStorage fs(dataString, FileStorage::READ | FileStorage::MEMORY);
166 cout << "attempting to read mdata_b from string\n"; //Show default behavior for empty matrix
168 cout << "read mdata\n";
174 cout << "Write data to string\n";
175 FileStorage fs(filename, FileStorage::WRITE | FileStorage::MEMORY | FileStorage::FORMAT_YAML);
177 cout << "writing MyData struct\n";
181 string createdString = fs.releaseAndGetString();
182 cout << "Created string:\n" << createdString << "\n";