|
|
|
@ -57,8 +57,9 @@ Several functions that are described below take CvFileStorage\* as inputs and al |
|
|
|
|
save or to load hierarchical collections that consist of scalar values, standard CXCore objects |
|
|
|
|
(such as matrices, sequences, graphs), and user-defined objects. |
|
|
|
|
|
|
|
|
|
OpenCV can read and write data in XML (<http://www.w3c.org/XML>) or YAML (<http://www.yaml.org>)
|
|
|
|
|
formats. Below is an example of 3x3 floating-point identity matrix A, stored in XML and YAML files |
|
|
|
|
OpenCV can read and write data in XML (<http://www.w3c.org/XML>), YAML (<http://www.yaml.org>) or
|
|
|
|
|
JSON (<http://www.json.org/>) formats. Below is an example of 3x3 floating-point identity matrix A,
|
|
|
|
|
stored in XML and YAML files |
|
|
|
|
using CXCore functions: |
|
|
|
|
XML: |
|
|
|
|
@code{.xml} |
|
|
|
@ -85,7 +86,8 @@ As it can be seen from the examples, XML uses nested tags to represent hierarchy |
|
|
|
|
indentation for that purpose (similar to the Python programming language). |
|
|
|
|
|
|
|
|
|
The same functions can read and write data in both formats; the particular format is determined by |
|
|
|
|
the extension of the opened file, ".xml" for XML files and ".yml" or ".yaml" for YAML. |
|
|
|
|
the extension of the opened file, ".xml" for XML files, ".yml" or ".yaml" for YAML and ".json" for |
|
|
|
|
JSON. |
|
|
|
|
*/ |
|
|
|
|
typedef struct CvFileStorage CvFileStorage; |
|
|
|
|
typedef struct CvFileNode CvFileNode; |
|
|
|
@ -101,20 +103,20 @@ namespace cv { |
|
|
|
|
|
|
|
|
|
/** @addtogroup core_xml
|
|
|
|
|
|
|
|
|
|
XML/YAML file storages. {#xml_storage} |
|
|
|
|
XML/YAML/JSON file storages. {#xml_storage} |
|
|
|
|
======================= |
|
|
|
|
Writing to a file storage. |
|
|
|
|
-------------------------- |
|
|
|
|
You can store and then restore various OpenCV data structures to/from XML (<http://www.w3c.org/XML>)
|
|
|
|
|
or YAML (<http://www.yaml.org>) formats. Also, it is possible 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. |
|
|
|
|
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 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 or YAML: |
|
|
|
|
Use the following procedure to write something to XML, YAML or JSON: |
|
|
|
|
-# Create new FileStorage and open it for writing. It can be done with a single call to |
|
|
|
|
FileStorage::FileStorage constructor that takes a filename, or you can use the default constructor |
|
|
|
|
and then call FileStorage::open. Format of the file (XML or YAML) is determined from the filename |
|
|
|
|
extension (".xml" and ".yml"/".yaml", respectively) |
|
|
|
|
and then call FileStorage::open. Format of the file (XML, YAML or JSON) is determined from the filename |
|
|
|
|
extension (".xml", ".yml"/".yaml" and ".json", respectively) |
|
|
|
|
-# Write all the data you want using the streaming operator `<<`, just like in the case of STL |
|
|
|
|
streams. |
|
|
|
|
-# Close the file using FileStorage::release. FileStorage destructor also closes the file. |
|
|
|
@ -177,19 +179,19 @@ features: |
|
|
|
|
- { x:344, y:158, lbp:[ 1, 1, 0, 0, 0, 0, 1, 0 ] } |
|
|
|
|
@endcode |
|
|
|
|
|
|
|
|
|
As an exercise, you can replace ".yml" with ".xml" in the sample above and see, how the |
|
|
|
|
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) consists of heterogeneous collections that can be nested. There are 2 |
|
|
|
|
types of collections: named collections (mappings) and unnamed collections (sequences). In mappings |
|
|
|
|
- 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 is a mapping. Each matrix is stored as a mapping, and the matrix |
|
|
|
|
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. |
|
|
|
|
|
|
|
|
@ -205,7 +207,7 @@ Several things can be noted by looking at the sample code and the output: |
|
|
|
|
- To write a sequence, you first write the special string `[`, then write the elements, then |
|
|
|
|
write the closing `]`. |
|
|
|
|
|
|
|
|
|
- In YAML (but not XML), mappings and sequences can be written in a compact Python-like inline |
|
|
|
|
- In YAML/JSON (but not XML), mappings and sequences can be written in a compact Python-like inline |
|
|
|
|
form. In the sample above matrix elements, as well as each feature, including its lbp value, is |
|
|
|
|
stored in such inline form. To store a mapping/sequence in a compact form, put `:` after the |
|
|
|
|
opening character, e.g. use `{:` instead of `{` and `[:` instead of `[`. When the |
|
|
|
@ -213,7 +215,7 @@ Several things can be noted by looking at the sample code and the output: |
|
|
|
|
|
|
|
|
|
Reading data from a file storage. |
|
|
|
|
--------------------------------- |
|
|
|
|
To read the previously written XML or YAML file, do the following: |
|
|
|
|
To read the previously written XML, YAML or JSON file, do the following: |
|
|
|
|
-# Open the file storage using FileStorage::FileStorage constructor or FileStorage::open method. |
|
|
|
|
In the current implementation the whole file is parsed and the whole representation of file |
|
|
|
|
storage is built in memory as a hierarchy of file nodes (see FileNode) |
|
|
|
@ -294,8 +296,8 @@ A complete example using the FileStorage interface |
|
|
|
|
class CV_EXPORTS FileNode; |
|
|
|
|
class CV_EXPORTS FileNodeIterator; |
|
|
|
|
|
|
|
|
|
/** @brief XML/YAML file storage class that encapsulates all the information necessary for writing or reading
|
|
|
|
|
data to/from a file. |
|
|
|
|
/** @brief XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or
|
|
|
|
|
reading data to/from a file. |
|
|
|
|
*/ |
|
|
|
|
class CV_EXPORTS_W FileStorage |
|
|
|
|
{ |
|
|
|
@ -312,6 +314,7 @@ public: |
|
|
|
|
FORMAT_AUTO = 0, //!< flag, auto format
|
|
|
|
|
FORMAT_XML = (1<<3), //!< flag, XML format
|
|
|
|
|
FORMAT_YAML = (2<<3), //!< flag, YAML format
|
|
|
|
|
FORMAT_JSON = (3<<3), //!< flag, JSON format
|
|
|
|
|
|
|
|
|
|
BASE64 = 64, //!< flag, write rawdata in Base64 by default. (consider using WRITE_BASE64)
|
|
|
|
|
WRITE_BASE64 = BASE64 | WRITE, //!< flag, enable both WRITE and BASE64
|
|
|
|
@ -333,9 +336,9 @@ public: |
|
|
|
|
|
|
|
|
|
/** @overload
|
|
|
|
|
@param source Name of the file to open or the text string to read the data from. Extension of the |
|
|
|
|
file (.xml or .yml/.yaml) determines its format (XML or YAML respectively). Also you can append .gz |
|
|
|
|
to work with compressed files, for example myHugeMatrix.xml.gz. If both FileStorage::WRITE and |
|
|
|
|
FileStorage::MEMORY flags are specified, source is used just to specify the output file format (e.g. |
|
|
|
|
file (.xml, .yml/.yaml, or .json) determines its format (XML, YAML or JSON respectively). Also you can |
|
|
|
|
append .gz to work with compressed files, for example myHugeMatrix.xml.gz. If both FileStorage::WRITE |
|
|
|
|
and FileStorage::MEMORY flags are specified, source is used just to specify the output file format (e.g. |
|
|
|
|
mydata.xml, .yml etc.). |
|
|
|
|
@param flags Mode of operation. See FileStorage::Mode |
|
|
|
|
@param encoding Encoding of the file. Note that UTF-16 XML encoding is not supported currently and |
|
|
|
@ -354,12 +357,12 @@ public: |
|
|
|
|
See description of parameters in FileStorage::FileStorage. The method calls FileStorage::release |
|
|
|
|
before opening the file. |
|
|
|
|
@param filename Name of the file to open or the text string to read the data from. |
|
|
|
|
Extension of the file (.xml or .yml/.yaml) determines its format (XML or YAML respectively). |
|
|
|
|
Also you can append .gz to work with compressed files, for example myHugeMatrix.xml.gz. If both |
|
|
|
|
Extension of the file (.xml, .yml/.yaml or .json) determines its format (XML, YAML or JSON |
|
|
|
|
respectively). Also you can append .gz to work with compressed files, for example myHugeMatrix.xml.gz. If both |
|
|
|
|
FileStorage::WRITE and FileStorage::MEMORY flags are specified, source is used just to specify |
|
|
|
|
the output file format (e.g. mydata.xml, .yml etc.). A file name can also contain parameters. |
|
|
|
|
You can use this format, "*?base64" (e.g. "file.xml?base64"), as an alternative to |
|
|
|
|
FileStorage::BASE64 flag. Note: it is case sensitive. |
|
|
|
|
You can use this format, "*?base64" (e.g. "file.json?base64" (case sensitive)), as an alternative to |
|
|
|
|
FileStorage::BASE64 flag. |
|
|
|
|
@param flags Mode of operation. One of FileStorage::Mode |
|
|
|
|
@param encoding Encoding of the file. Note that UTF-16 XML encoding is not supported currently and |
|
|
|
|
you should use 8-bit encoding instead of it. |
|
|
|
|