Merge pull request #3038 from yury-gorbachev:core_arithm_neon
[profile/ivi/opencv.git] / samples / cpp / filestorage.cpp
1 /*
2  * filestorage_sample demonstrate the usage of the opencv serialization functionality
3  */
4
5 #include "opencv2/core/core.hpp"
6 #include <iostream>
7 #include <string>
8
9 using std::string;
10 using std::cout;
11 using std::endl;
12 using std::cerr;
13 using std::ostream;
14 using namespace cv;
15
16 static void help(char** av)
17 {
18   cout << "\nfilestorage_sample demonstrate the usage of the opencv serialization functionality.\n"
19       << "usage:\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;
26 }
27
28 struct MyData
29 {
30   MyData() :
31     A(0), X(0), id()
32   {
33   }
34   explicit MyData(int) :
35     A(97), X(CV_PI), id("mydata1234")
36   {
37   }
38   int A;
39   double X;
40   string id;
41   void write(FileStorage& fs) const //Write serialization for this class
42   {
43     fs << "{" << "A" << A << "X" << X << "id" << id << "}";
44   }
45   void read(const FileNode& node)  //Read serialization for this class
46   {
47
48     A = (int)node["A"];
49     X = (double)node["X"];
50     id = (string)node["id"];
51   }
52 };
53
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){
56   x.write(fs);
57 }
58 static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){
59   if(node.empty())
60     x = default_value;
61   else
62     x.read(node);
63 }
64
65 static ostream& operator<<(ostream& out, const MyData& m){
66   out << "{ id = " << m.id << ", ";
67   out << "X = " << m.X << ", ";
68   out << "A = " << m.A << "}";
69   return out;
70 }
71 int main(int ac, char** av)
72 {
73   if (ac != 2)
74   {
75     help(av);
76     return 1;
77   }
78
79   string filename = av[1];
80
81   //write
82   {
83     FileStorage fs(filename, FileStorage::WRITE);
84
85     cout << "writing images\n";
86     fs << "images" << "[";
87
88     fs << "image1.jpg" << "myfi.png" << "baboon.jpg";
89     cout << "image1.jpg" << " myfi.png" << " baboon.jpg" << endl;
90
91     fs << "]";
92
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";
97     fs << "R" << R;
98     fs << "T" << T;
99
100     cout << "writing MyData struct\n";
101     MyData m(1);
102     fs << "mdata" << m;
103     cout << m << endl;
104   }
105
106   //read
107   {
108     FileStorage fs(filename, FileStorage::READ);
109
110     if (!fs.isOpened())
111     {
112       cerr << "failed to open " << filename << endl;
113       help(av);
114       return 1;
115     }
116
117     FileNode n = fs["images"];
118     if (n.type() != FileNode::SEQ)
119     {
120       cerr << "images is not a sequence! FAIL" << endl;
121       return 1;
122     }
123
124     cout << "reading images\n";
125     FileNodeIterator it = n.begin(), it_end = n.end();
126     for (; it != it_end; ++it)
127     {
128       cout << (string)*it << "\n";
129     }
130
131     Mat R, T;
132     cout << "reading R and T" << endl;
133
134     fs["R"] >> R;
135     fs["T"] >> T;
136
137     cout << "R = " << R << "\n";
138     cout << "T = " << T << endl;
139
140     MyData m;
141     fs["mdata"] >> m;
142
143     cout << "read mdata\n";
144     cout << m << endl;
145
146     cout << "attempting to read mdata_b\n";   //Show default behavior for empty matrix
147     fs["mdata_b"] >> m;
148     cout << "read mdata_b\n";
149     cout << m << endl;
150
151   }
152
153   cout << "Try opening " << filename << " to see the serialized data." << endl << endl;
154
155   //read from string
156   {
157     cout << "Read data from string\n";
158     string dataString =
159         "%YAML:1.0\n"
160         "mdata:\n"
161         "   A: 97\n"
162         "   X: 3.1415926535897931e+00\n"
163         "   id: mydata1234\n";
164     MyData m;
165     FileStorage fs(dataString, FileStorage::READ | FileStorage::MEMORY);
166     cout << "attempting to read mdata_b from string\n";   //Show default behavior for empty matrix
167     fs["mdata"] >> m;
168     cout << "read mdata\n";
169     cout << m << endl;
170   }
171
172   //write to string
173   {
174     cout << "Write data to string\n";
175     FileStorage fs(filename, FileStorage::WRITE | FileStorage::MEMORY | FileStorage::FORMAT_YAML);
176
177     cout << "writing MyData struct\n";
178     MyData m(1);
179     fs << "mdata" << m;
180     cout << m << endl;
181     string createdString = fs.releaseAndGetString();
182     cout << "Created string:\n" << createdString << "\n";
183   }
184
185   return 0;
186 }