filestorage_mod.cpp

modified sample source code to test FileStorage << vector<T> - Koji Miyazato, 2014-03-10 07:18 am

Download (5 kB)

 
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
    cout << "writing vector of int\n";
106
    std::vector<int> intvec(3);
107
    intvec[0] = 6;
108
    intvec[1] = 12;
109
    intvec[2] = 22;
110
    fs << "int_vec" << intvec;
111
    
112
    cout << "writing vector of string\n";
113
    std::vector<string> strvec(3);
114
    strvec[0] = "str1";
115
    strvec[1] = string("[with brackets]");
116
    strvec[2] = string("\"with quotes\"");
117
    fs << "str_vec" << strvec;
118
    
119
    cout << "writing vector of MyData struct \n";
120
    std::vector<MyData> mvec(3, m);
121
    fs << "mdata_vec" << mvec;
122
  }
123
124
  //read
125
  {
126
    FileStorage fs(filename, FileStorage::READ);
127
128
    if (!fs.isOpened())
129
    {
130
      cerr << "failed to open " << filename << endl;
131
      help(av);
132
      return 1;
133
    }
134
135
    FileNode n = fs["images"];
136
    if (n.type() != FileNode::SEQ)
137
    {
138
      cerr << "images is not a sequence! FAIL" << endl;
139
      return 1;
140
    }
141
142
    cout << "reading images\n";
143
    FileNodeIterator it = n.begin(), it_end = n.end();
144
    for (; it != it_end; ++it)
145
    {
146
      cout << (string)*it << "\n";
147
    }
148
149
    Mat R, T;
150
    cout << "reading R and T" << endl;
151
152
    fs["R"] >> R;
153
    fs["T"] >> T;
154
155
    cout << "R = " << R << "\n";
156
    cout << "T = " << T << endl;
157
158
    MyData m;
159
    fs["mdata"] >> m;
160
161
    cout << "read mdata\n";
162
    cout << m << endl;
163
164
    cout << "attempting to read mdata_b\n";   //Show default behavior for empty matrix
165
    fs["mdata_b"] >> m;
166
    cout << "read mdata_b\n";
167
    cout << m << endl;
168
169
  }
170
171
  cout << "Try opening " << filename << " to see the serialized data." << endl << endl;
172
173
  //read from string
174
  {
175
    cout << "Read data from string\n";
176
    string dataString =
177
        "%YAML:1.0\n"
178
        "mdata:\n"
179
        "   A: 97\n"
180
        "   X: 3.1415926535897931e+00\n"
181
        "   id: mydata1234\n";
182
    MyData m;
183
    FileStorage fs(dataString, FileStorage::READ | FileStorage::MEMORY);
184
    cout << "attempting to read mdata_b from string\n";   //Show default behavior for empty matrix
185
    fs["mdata"] >> m;
186
    cout << "read mdata\n";
187
    cout << m << endl;
188
  }
189
190
  //write to string
191
  {
192
    cout << "Write data to string\n";
193
    FileStorage fs(filename, FileStorage::WRITE | FileStorage::MEMORY | FileStorage::FORMAT_YAML);
194
195
    cout << "writing MyData struct\n";
196
    MyData m(1);
197
    fs << "mdata" << m;
198
    cout << m << endl;
199
    string createdString = fs.releaseAndGetString();
200
    cout << "Created string:\n" << createdString << "\n";
201
  }
202
203
  return 0;
204
}