OpenCV
Open Source Computer Vision
|
Classes | |
class | cv::FileNode |
File Storage Node class. More... | |
class | cv::FileNodeIterator |
used to iterate through sequences and mappings. More... | |
class | cv::FileStorage |
XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or reading data to/from a file. More... | |
You can store and then restore various OpenCV data structures to/from XML (http://www.w3c.org/XML), YAML (http://www.yaml.org) or JSON (http://www.json.org/) formats. Also, it is possible to store and load arbitrarily complex data structures, which include OpenCV data structures, as well as primitive data types (integer and floating-point numbers and text strings) as their elements.
Use the following procedure to write something to XML, YAML or JSON:
<<
, just like in the case of STL streams.Here is an example:
The sample above stores to YML an integer, a text string (calibration date), 2 matrices, and a custom structure "feature", which includes feature coordinates and LBP (local binary pattern) value. Here is output of the sample:
As an exercise, you can replace ".yml" with ".xml" or ".json" in the sample above and see, how the corresponding XML file will look like.
Several things can be noted by looking at the sample code and the output:
The produced YAML (and XML/JSON) consists of heterogeneous collections that can be nested. There are 2 types of collections: named collections (mappings) and unnamed collections (sequences). In mappings each element has a name and is accessed by name. This is similar to structures and std::map in C/C++ and dictionaries in Python. In sequences elements do not have names, they are accessed by indices. This is similar to arrays and std::vector in C/C++ and lists, tuples in Python. "Heterogeneous" means that elements of each single collection can have different types.
Top-level collection in YAML/XML/JSON is a mapping. Each matrix is stored as a mapping, and the matrix elements are stored as a sequence. Then, there is a sequence of features, where each feature is represented a mapping, and lbp value in a nested sequence.
<<
operator.{
to the storage, then write the elements as pairs (fs << <element_name> << <element_value>
) and then write the closing }
.[
, then write the elements, then write the closing ]
.:
after the opening character, e.g. use {:
instead of {
and [:
instead of [
. When the data is written to XML, those extra :
are ignored.To read the previously written XML, YAML or JSON file, do the following:
Here is how to read the file created by the code sample above:
([count]{u|c|w|s|i|f|d})
... where the characters correspond to fundamental C++ types:
u
8-bit unsigned numberc
8-bit signed numberw
16-bit unsigned numbers
16-bit signed numberi
32-bit signed numberf
single precision floating-point numberd
double precision floating-point numberr
pointer, 32 lower bits of which are written as a signed integer. The type can be used to store structures with links between the elements.count
is the optional counter of values of a given type. For example, 2if
means that each array element is a structure of 2 integers, followed by a single-precision floating-point number. The equivalent notations of the above specification are iif
, 2i1f
and so forth. Other examples: u
means that the array consists of bytes, and 2d
means the array consists of pairs of doubles.