#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
using std::cerr;
using std::ostream;
static void help(char** av)
{
cout << "\nfilestorage_sample demonstrate the usage of the opencv serialization functionality.\n"
<< "usage:\n"
<< av[0] << " outputfile.yml.gz\n"
<< "\n outputfile above can have many different extensions, see below."
<< "\nThis program demonstrates the use of FileStorage for serialization, that is in use << and >> in OpenCV\n"
<< "For example, how to create a class and have it serialize, but also how to use it to read and write matrices.\n"
<< "FileStorage allows you to serialize to various formats specified by the file end type."
<< "\nYou should try using different file extensions.(e.g. yaml yml xml xml.gz yaml.gz etc...)\n" << endl;
}
struct MyData
{
MyData() :
A(0), X(0), id()
{
}
explicit MyData(int) :
A(97), X(
CV_PI), id(
"mydata1234")
{
}
int A;
double X;
string id;
void write(FileStorage& fs)
const
{
fs << "{" << "A" << A << "X" << X << "id" << id << "}";
}
void read(
const FileNode& node)
{
A = (int)node["A"];
X = (double)node["X"];
id = (string)node["id"];
}
};
x.write(fs);
}
static void read(
const FileNode& node, MyData& x,
const MyData& default_value = MyData()){
x = default_value;
else
x.read(node);
}
static ostream&
operator<<(ostream& out,
const MyData& m){
out << "{ id = " << m.id << ", ";
out << "X = " << m.X << ", ";
out << "A = " << m.A << "}";
return out;
}
int main(
int ac,
char** av)
{
"{@input||}{help h ||}"
);
if (parser.has("help"))
{
help(av);
return 0;
}
string filename = parser.get<string>("@input");
if (filename.empty())
{
help(av);
return 1;
}
{
cout << "writing images\n";
fs << "images" << "[";
fs << "image1.jpg" << "myfi.png" << "baboon.jpg";
cout << "image1.jpg" << " myfi.png" << " baboon.jpg" << endl;
fs << "]";
cout << "writing mats\n";
cout << "R = " << R << "\n";
cout << "T = " << T << "\n";
fs << "R" << R;
fs << "T" << T;
cout << "writing MyData struct\n";
MyData m(1);
fs << "mdata" << m;
cout << m << endl;
}
{
{
cerr << "failed to open " << filename << endl;
help(av);
return 1;
}
{
cerr << "images is not a sequence! FAIL" << endl;
return 1;
}
cout << "reading images\n";
for (; it != it_end; ++it)
{
cout << (string)*it << "\n";
}
cout << "reading R and T" << endl;
fs["R"] >> R;
fs["T"] >> T;
cout << "R = " << R << "\n";
cout << "T = " << T << endl;
MyData m;
fs["mdata"] >> m;
cout << "read mdata\n";
cout << m << endl;
cout << "attempting to read mdata_b\n";
fs["mdata_b"] >> m;
cout << "read mdata_b\n";
cout << m << endl;
}
cout << "Try opening " << filename << " to see the serialized data." << endl << endl;
{
cout << "Read data from string\n";
string dataString =
"%YAML:1.0\n"
"mdata:\n"
" A: 97\n"
" X: 3.1415926535897931e+00\n"
" id: mydata1234\n";
MyData m;
cout << "attempting to read mdata_b from string\n";
fs["mdata"] >> m;
cout << "read mdata\n";
cout << m << endl;
}
{
cout << "Write data to string\n";
cout << "writing MyData struct\n";
MyData m(1);
fs << "mdata" << m;
cout << m << endl;
cout << "Created string:\n" << createdString << "\n";
}
return 0;
}
Designed for command line parsing.
Definition utility.hpp:820
used to iterate through sequences and mappings.
Definition persistence.hpp:634
File Storage Node class.
Definition persistence.hpp:482
FileNodeIterator begin() const
returns iterator pointing to the first node element
FileNodeIterator end() const
returns iterator pointing to the element following the last node element
bool empty() const
returns true if the node is empty
int type() const
Returns type of the node.
@ SEQ
sequence
Definition persistence.hpp:493
XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or readi...
Definition persistence.hpp:304
virtual String releaseAndGetString()
Closes the file and releases all the memory buffers.
@ FORMAT_YAML
flag, YAML format
Definition persistence.hpp:317
@ WRITE
value, open the file for writing
Definition persistence.hpp:310
@ READ
value, open the file for reading
Definition persistence.hpp:309
@ MEMORY
Definition persistence.hpp:312
virtual bool isOpened() const
Checks whether the file is opened.
static CV_NODISCARD_STD MatExpr eye(int rows, int cols)
static CV_NODISCARD_STD MatExpr zeros(int rows, int cols)
overridden forms of Mat::zeros() etc. Data type is omitted, of course
n-dimensional dense array class
Definition mat.hpp:812
static String & operator<<(String &out, Ptr< Formatted > fmtd)
Definition core.hpp:3164
#define CV_PI
Definition cvdef.h:380
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
"black box" representation of the file storage associated with a file on disk.
Definition core.hpp:102
void write(FileStorage &fs, const String &name, int value)
void read(const FileNode &node, int &value, int default_value)