Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/filestorage.cpp
16337 views
1
/*
2
* filestorage_sample demonstrate the usage of the opencv serialization functionality
3
*/
4
5
#include "opencv2/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 extensions, see below."
22
<< "\nThis program demonstrates the use of FileStorage for serialization, that is in 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
cv::CommandLineParser parser(ac, av,
74
"{@input||}{help h ||}"
75
);
76
if (parser.has("help"))
77
{
78
help(av);
79
return 0;
80
}
81
string filename = parser.get<string>("@input");
82
if (filename.empty())
83
{
84
help(av);
85
return 1;
86
}
87
88
//write
89
{
90
FileStorage fs(filename, FileStorage::WRITE);
91
92
cout << "writing images\n";
93
fs << "images" << "[";
94
95
fs << "image1.jpg" << "myfi.png" << "../data/baboon.jpg";
96
cout << "image1.jpg" << " myfi.png" << " ../data/baboon.jpg" << endl;
97
98
fs << "]";
99
100
cout << "writing mats\n";
101
Mat R =Mat_<double>::eye(3, 3),T = Mat_<double>::zeros(3, 1);
102
cout << "R = " << R << "\n";
103
cout << "T = " << T << "\n";
104
fs << "R" << R;
105
fs << "T" << T;
106
107
cout << "writing MyData struct\n";
108
MyData m(1);
109
fs << "mdata" << m;
110
cout << m << endl;
111
}
112
113
//read
114
{
115
FileStorage fs(filename, FileStorage::READ);
116
117
if (!fs.isOpened())
118
{
119
cerr << "failed to open " << filename << endl;
120
help(av);
121
return 1;
122
}
123
124
FileNode n = fs["images"];
125
if (n.type() != FileNode::SEQ)
126
{
127
cerr << "images is not a sequence! FAIL" << endl;
128
return 1;
129
}
130
131
cout << "reading images\n";
132
FileNodeIterator it = n.begin(), it_end = n.end();
133
for (; it != it_end; ++it)
134
{
135
cout << (string)*it << "\n";
136
}
137
138
Mat R, T;
139
cout << "reading R and T" << endl;
140
141
fs["R"] >> R;
142
fs["T"] >> T;
143
144
cout << "R = " << R << "\n";
145
cout << "T = " << T << endl;
146
147
MyData m;
148
fs["mdata"] >> m;
149
150
cout << "read mdata\n";
151
cout << m << endl;
152
153
cout << "attempting to read mdata_b\n"; //Show default behavior for empty matrix
154
fs["mdata_b"] >> m;
155
cout << "read mdata_b\n";
156
cout << m << endl;
157
158
}
159
160
cout << "Try opening " << filename << " to see the serialized data." << endl << endl;
161
162
//read from string
163
{
164
cout << "Read data from string\n";
165
string dataString =
166
"%YAML:1.0\n"
167
"mdata:\n"
168
" A: 97\n"
169
" X: 3.1415926535897931e+00\n"
170
" id: mydata1234\n";
171
MyData m;
172
FileStorage fs(dataString, FileStorage::READ | FileStorage::MEMORY);
173
cout << "attempting to read mdata_b from string\n"; //Show default behavior for empty matrix
174
fs["mdata"] >> m;
175
cout << "read mdata\n";
176
cout << m << endl;
177
}
178
179
//write to string
180
{
181
cout << "Write data to string\n";
182
FileStorage fs(filename, FileStorage::WRITE | FileStorage::MEMORY | FileStorage::FORMAT_YAML);
183
184
cout << "writing MyData struct\n";
185
MyData m(1);
186
fs << "mdata" << m;
187
cout << m << endl;
188
string createdString = fs.releaseAndGetString();
189
cout << "Created string:\n" << createdString << "\n";
190
}
191
192
return 0;
193
}
194
195