1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16
// Third party copyrights are property of their respective owners.
18
// Redistribution and use in source and binary forms, with or without modification,
19
// are permitted provided that the following conditions are met:
21
// * Redistribution's of source code must retain the above copyright notice,
22
// this list of conditions and the following disclaimer.
24
// * Redistribution's in binary form must reproduce the above copyright notice,
25
// this list of conditions and the following disclaimer in the documentation
26
// and/or other materials provided with the distribution.
28
// * The name of the copyright holders may not be used to endorse or promote products
29
// derived from this software without specific prior written permission.
31
// This software is provided by the copyright holders and contributors "as is" and
32
// any express or implied warranties, including, but not limited to, the implied
33
// warranties of merchantability and fitness for a particular purpose are disclaimed.
34
// In no event shall the Intel Corporation or contributors be liable for any direct,
35
// indirect, incidental, special, exemplary, or consequential damages
36
// (including, but not limited to, procurement of substitute goods or services;
37
// loss of use, data, or profits; or business interruption) however caused
38
// and on any theory of liability, whether in contract, strict liability,
39
// or tort (including negligence or otherwise) arising in any way out of
40
// the use of this software, even if advised of the possibility of such damage.
44
#ifndef __OPENCV_CORE_PERSISTENCE_HPP__
45
#define __OPENCV_CORE_PERSISTENCE_HPP__
48
# error persistence.hpp header must be compiled as C++
51
//! @addtogroup core_c
54
/** @brief "black box" representation of the file storage associated with a file on disk.
56
Several functions that are described below take CvFileStorage\* as inputs and allow the user to
57
save or to load hierarchical collections that consist of scalar values, standard CXCore objects
58
(such as matrices, sequences, graphs), and user-defined objects.
60
OpenCV can read and write data in XML (<http://www.w3c.org/XML>) or YAML (<http://www.yaml.org>)
61
formats. Below is an example of 3x3 floating-point identity matrix A, stored in XML and YAML files
62
using CXCore functions:
67
<A type_id="opencv-matrix">
71
<data>1. 0. 0. 0. 1. 0. 0. 0. 1.</data>
82
data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1.]
84
As it can be seen from the examples, XML uses nested tags to represent hierarchy, while YAML uses
85
indentation for that purpose (similar to the Python programming language).
87
The same functions can read and write data in both formats; the particular format is determined by
88
the extension of the opened file, ".xml" for XML files and ".yml" or ".yaml" for YAML.
90
typedef struct CvFileStorage CvFileStorage;
91
typedef struct CvFileNode CvFileNode;
95
#include "opencv2/core/types.hpp"
96
#include "opencv2/core/mat.hpp"
100
/** @addtogroup core_xml
102
XML/YAML file storages. {#xml_storage}
103
=======================
104
Writing to a file storage.
105
--------------------------
106
You can store and then restore various OpenCV data structures to/from XML (<http://www.w3c.org/XML>)
107
or YAML (<http://www.yaml.org>) formats. Also, it is possible store and load arbitrarily complex
108
data structures, which include OpenCV data structures, as well as primitive data types (integer and
109
floating-point numbers and text strings) as their elements.
111
Use the following procedure to write something to XML or YAML:
112
-# Create new FileStorage and open it for writing. It can be done with a single call to
113
FileStorage::FileStorage constructor that takes a filename, or you can use the default constructor
114
and then call FileStorage::open. Format of the file (XML or YAML) is determined from the filename
115
extension (".xml" and ".yml"/".yaml", respectively)
116
-# Write all the data you want using the streaming operator `<<`, just like in the case of STL
118
-# Close the file using FileStorage::release. FileStorage destructor also closes the file.
122
#include "opencv2/opencv.hpp"
127
int main(int, char** argv)
129
FileStorage fs("test.yml", FileStorage::WRITE);
131
fs << "frameCount" << 5;
132
time_t rawtime; time(&rawtime);
133
fs << "calibrationDate" << asctime(localtime(&rawtime));
134
Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
135
Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
136
fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
137
fs << "features" << "[";
138
for( int i = 0; i < 3; i++ )
140
int x = rand() % 640;
141
int y = rand() % 480;
142
uchar lbp = rand() % 256;
144
fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
145
for( int j = 0; j < 8; j++ )
146
fs << ((lbp >> j) & 1);
154
The sample above stores to XML and integer, text string (calibration date), 2 matrices, and a custom
155
structure "feature", which includes feature coordinates and LBP (local binary pattern) value. Here
156
is output of the sample:
160
calibrationDate: "Fri Jun 17 14:09:29 2011\n"
161
cameraMatrix: !!opencv-matrix
165
data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
166
distCoeffs: !!opencv-matrix
170
data: [ 1.0000000000000001e-01, 1.0000000000000000e-02,
171
-1.0000000000000000e-03, 0., 0. ]
173
- { x:167, y:49, lbp:[ 1, 0, 0, 1, 1, 0, 1, 1 ] }
174
- { x:298, y:130, lbp:[ 0, 0, 0, 1, 0, 0, 1, 1 ] }
175
- { x:344, y:158, lbp:[ 1, 1, 0, 0, 0, 0, 1, 0 ] }
178
As an exercise, you can replace ".yml" with ".xml" in the sample above and see, how the
179
corresponding XML file will look like.
181
Several things can be noted by looking at the sample code and the output:
183
- The produced YAML (and XML) consists of heterogeneous collections that can be nested. There are 2
184
types of collections: named collections (mappings) and unnamed collections (sequences). In mappings
185
each element has a name and is accessed by name. This is similar to structures and std::map in
186
C/C++ and dictionaries in Python. In sequences elements do not have names, they are accessed by
187
indices. This is similar to arrays and std::vector in C/C++ and lists, tuples in Python.
188
"Heterogeneous" means that elements of each single collection can have different types.
190
Top-level collection in YAML/XML is a mapping. Each matrix is stored as a mapping, and the matrix
191
elements are stored as a sequence. Then, there is a sequence of features, where each feature is
192
represented a mapping, and lbp value in a nested sequence.
194
- When you write to a mapping (a structure), you write element name followed by its value. When you
195
write to a sequence, you simply write the elements one by one. OpenCV data structures (such as
196
cv::Mat) are written in absolutely the same way as simple C data structures - using `<<`
199
- To write a mapping, you first write the special string `{` to the storage, then write the
200
elements as pairs (`fs << <element_name> << <element_value>`) and then write the closing
203
- To write a sequence, you first write the special string `[`, then write the elements, then
204
write the closing `]`.
206
- In YAML (but not XML), mappings and sequences can be written in a compact Python-like inline
207
form. In the sample above matrix elements, as well as each feature, including its lbp value, is
208
stored in such inline form. To store a mapping/sequence in a compact form, put `:` after the
209
opening character, e.g. use `{:` instead of `{` and `[:` instead of `[`. When the
210
data is written to XML, those extra `:` are ignored.
212
Reading data from a file storage.
213
---------------------------------
214
To read the previously written XML or YAML file, do the following:
215
-# Open the file storage using FileStorage::FileStorage constructor or FileStorage::open method.
216
In the current implementation the whole file is parsed and the whole representation of file
217
storage is built in memory as a hierarchy of file nodes (see FileNode)
219
-# Read the data you are interested in. Use FileStorage::operator [], FileNode::operator []
220
and/or FileNodeIterator.
222
-# Close the storage using FileStorage::release.
224
Here is how to read the file created by the code sample above:
226
FileStorage fs2("test.yml", FileStorage::READ);
228
// first method: use (type) operator on FileNode.
229
int frameCount = (int)fs2["frameCount"];
232
// second method: use FileNode::operator >>
233
fs2["calibrationDate"] >> date;
235
Mat cameraMatrix2, distCoeffs2;
236
fs2["cameraMatrix"] >> cameraMatrix2;
237
fs2["distCoeffs"] >> distCoeffs2;
239
cout << "frameCount: " << frameCount << endl
240
<< "calibration date: " << date << endl
241
<< "camera matrix: " << cameraMatrix2 << endl
242
<< "distortion coeffs: " << distCoeffs2 << endl;
244
FileNode features = fs2["features"];
245
FileNodeIterator it = features.begin(), it_end = features.end();
247
std::vector<uchar> lbpval;
249
// iterate through a sequence using FileNodeIterator
250
for( ; it != it_end; ++it, idx++ )
252
cout << "feature #" << idx << ": ";
253
cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
254
// you can also easily read numerical arrays using FileNode >> std::vector operator.
255
(*it)["lbp"] >> lbpval;
256
for( int i = 0; i < (int)lbpval.size(); i++ )
257
cout << " " << (int)lbpval[i];
263
Format specification {#format_spec}
265
`([count]{u|c|w|s|i|f|d})`... where the characters correspond to fundamental C++ types:
266
- `u` 8-bit unsigned number
267
- `c` 8-bit signed number
268
- `w` 16-bit unsigned number
269
- `s` 16-bit signed number
270
- `i` 32-bit signed number
271
- `f` single precision floating-point number
272
- `d` double precision floating-point number
273
- `r` pointer, 32 lower bits of which are written as a signed integer. The type can be used to
274
store structures with links between the elements.
276
`count` is the optional counter of values of a given type. For example, `2if` means that each array
277
element is a structure of 2 integers, followed by a single-precision floating-point number. The
278
equivalent notations of the above specification are `iif`, `2i1f` and so forth. Other examples: `u`
279
means that the array consists of bytes, and `2d` means the array consists of pairs of doubles.
281
@see @ref filestorage.cpp
286
/** @example filestorage.cpp
287
A complete example using the FileStorage interface
290
////////////////////////// XML & YAML I/O //////////////////////////
292
class CV_EXPORTS FileNode;
293
class CV_EXPORTS FileNodeIterator;
295
/** @brief XML/YAML file storage class that encapsulates all the information necessary for writing or reading
298
class CV_EXPORTS_W FileStorage
301
//! file storage mode
304
READ = 0, //!< value, open the file for reading
305
WRITE = 1, //!< value, open the file for writing
306
APPEND = 2, //!< value, open the file for appending
307
MEMORY = 4, //!< flag, read data from source or write data to the internal buffer (which is
308
//!< returned by FileStorage::release)
309
FORMAT_MASK = (7<<3), //!< mask for format flags
310
FORMAT_AUTO = 0, //!< flag, auto format
311
FORMAT_XML = (1<<3), //!< flag, XML format
312
FORMAT_YAML = (2<<3) //!< flag, YAML format
322
/** @brief The constructors.
324
The full constructor opens the file. Alternatively you can use the default constructor and then
325
call FileStorage::open.
327
CV_WRAP FileStorage();
330
@param source Name of the file to open or the text string to read the data from. Extension of the
331
file (.xml or .yml/.yaml) determines its format (XML or YAML respectively). Also you can append .gz
332
to work with compressed files, for example myHugeMatrix.xml.gz. If both FileStorage::WRITE and
333
FileStorage::MEMORY flags are specified, source is used just to specify the output file format (e.g.
334
mydata.xml, .yml etc.).
335
@param flags Mode of operation. See FileStorage::Mode
336
@param encoding Encoding of the file. Note that UTF-16 XML encoding is not supported currently and
337
you should use 8-bit encoding instead of it.
339
CV_WRAP FileStorage(const String& source, int flags, const String& encoding=String());
342
FileStorage(CvFileStorage* fs, bool owning=true);
344
//! the destructor. calls release()
345
virtual ~FileStorage();
347
/** @brief Opens a file.
349
See description of parameters in FileStorage::FileStorage. The method calls FileStorage::release
350
before opening the file.
351
@param filename Name of the file to open or the text string to read the data from.
352
Extension of the file (.xml or .yml/.yaml) determines its format (XML or YAML respectively).
353
Also you can append .gz to work with compressed files, for example myHugeMatrix.xml.gz. If both
354
FileStorage::WRITE and FileStorage::MEMORY flags are specified, source is used just to specify
355
the output file format (e.g. mydata.xml, .yml etc.).
356
@param flags Mode of operation. One of FileStorage::Mode
357
@param encoding Encoding of the file. Note that UTF-16 XML encoding is not supported currently and
358
you should use 8-bit encoding instead of it.
360
CV_WRAP virtual bool open(const String& filename, int flags, const String& encoding=String());
362
/** @brief Checks whether the file is opened.
364
@returns true if the object is associated with the current file and false otherwise. It is a
365
good practice to call this method after you tried to open a file.
367
CV_WRAP virtual bool isOpened() const;
369
/** @brief Closes the file and releases all the memory buffers.
371
Call this method after all I/O operations with the storage are finished.
373
CV_WRAP virtual void release();
375
/** @brief Closes the file and releases all the memory buffers.
377
Call this method after all I/O operations with the storage are finished. If the storage was
378
opened for writing data and FileStorage::WRITE was specified
380
CV_WRAP virtual String releaseAndGetString();
382
/** @brief Returns the first element of the top-level mapping.
383
@returns The first element of the top-level mapping.
385
CV_WRAP FileNode getFirstTopLevelNode() const;
387
/** @brief Returns the top-level mapping
388
@param streamidx Zero-based index of the stream. In most cases there is only one stream in the file.
389
However, YAML supports multiple streams and so there can be several.
390
@returns The top-level mapping.
392
CV_WRAP FileNode root(int streamidx=0) const;
394
/** @brief Returns the specified element of the top-level mapping.
395
@param nodename Name of the file node.
396
@returns Node with the given name.
398
FileNode operator[](const String& nodename) const;
401
CV_WRAP FileNode operator[](const char* nodename) const;
403
/** @brief Returns the obsolete C FileStorage structure.
404
@returns Pointer to the underlying C FileStorage structure
406
CvFileStorage* operator *() { return fs.get(); }
409
const CvFileStorage* operator *() const { return fs.get(); }
411
/** @brief Writes multiple numbers.
413
Writes one or more numbers of the specified format to the currently written structure. Usually it is
414
more convenient to use operator `<<` instead of this method.
415
@param fmt Specification of each array element, see @ref format_spec "format specification"
416
@param vec Pointer to the written array.
417
@param len Number of the uchar elements to write.
419
void writeRaw( const String& fmt, const uchar* vec, size_t len );
421
/** @brief Writes the registered C structure (CvMat, CvMatND, CvSeq).
422
@param name Name of the written object.
423
@param obj Pointer to the object.
424
@see ocvWrite for details.
426
void writeObj( const String& name, const void* obj );
428
/** @brief Returns the normalized object name for the specified name of a file.
429
@param filename Name of a file
430
@returns The normalized object name.
432
static String getDefaultObjectName(const String& filename);
434
Ptr<CvFileStorage> fs; //!< the underlying C FileStorage structure
435
String elname; //!< the currently written element
436
std::vector<char> structs; //!< the stack of written structures
437
int state; //!< the writer state
440
template<> CV_EXPORTS void DefaultDeleter<CvFileStorage>::operator ()(CvFileStorage* obj) const;
442
/** @brief File Storage Node class.
444
The node is used to store each and every element of the file storage opened for reading. When
445
XML/YAML file is read, it is first parsed and stored in the memory as a hierarchical collection of
446
nodes. Each node can be a “leaf” that is contain a single number or a string, or be a collection of
447
other nodes. There can be named collections (mappings) where each element has a name and it is
448
accessed by a name, and ordered collections (sequences) where elements do not have names but rather
449
accessed by index. Type of the file node can be determined using FileNode::type method.
451
Note that file nodes are only used for navigating file storages opened for reading. When a file
452
storage is opened for writing, no data is stored in memory after it is written.
454
class CV_EXPORTS_W_SIMPLE FileNode
457
//! type of the file storage node
460
NONE = 0, //!< empty node
461
INT = 1, //!< an integer
462
REAL = 2, //!< floating-point number
463
FLOAT = REAL, //!< synonym or REAL
464
STR = 3, //!< text string in UTF-8 encoding
465
STRING = STR, //!< synonym for STR
466
REF = 4, //!< integer of size size_t. Typically used for storing complex dynamic structures where some elements reference the others
467
SEQ = 5, //!< sequence
468
MAP = 6, //!< mapping
470
FLOW = 8, //!< compact representation of a sequence or mapping. Used only by YAML writer
471
USER = 16, //!< a registered object (e.g. a matrix)
472
EMPTY = 32, //!< empty structure (sequence or mapping)
473
NAMED = 64 //!< the node has a name (i.e. it is element of a mapping)
475
/** @brief The constructors.
477
These constructors are used to create a default file node, construct it from obsolete structures or
478
from the another file node.
483
@param fs Pointer to the obsolete file storage structure.
484
@param node File node to be used as initialization for the created file node.
486
FileNode(const CvFileStorage* fs, const CvFileNode* node);
489
@param node File node to be used as initialization for the created file node.
491
FileNode(const FileNode& node);
493
/** @brief Returns element of a mapping node or a sequence node.
494
@param nodename Name of an element in the mapping node.
495
@returns Returns the element with the given identifier.
497
FileNode operator[](const String& nodename) const;
500
@param nodename Name of an element in the mapping node.
502
CV_WRAP FileNode operator[](const char* nodename) const;
505
@param i Index of an element in the sequence node.
507
CV_WRAP FileNode operator[](int i) const;
509
/** @brief Returns type of the node.
510
@returns Type of the node. See FileNode::Type
512
CV_WRAP int type() const;
514
//! returns true if the node is empty
515
CV_WRAP bool empty() const;
516
//! returns true if the node is a "none" object
517
CV_WRAP bool isNone() const;
518
//! returns true if the node is a sequence
519
CV_WRAP bool isSeq() const;
520
//! returns true if the node is a mapping
521
CV_WRAP bool isMap() const;
522
//! returns true if the node is an integer
523
CV_WRAP bool isInt() const;
524
//! returns true if the node is a floating-point number
525
CV_WRAP bool isReal() const;
526
//! returns true if the node is a text string
527
CV_WRAP bool isString() const;
528
//! returns true if the node has a name
529
CV_WRAP bool isNamed() const;
530
//! returns the node name or an empty string if the node is nameless
531
CV_WRAP String name() const;
532
//! returns the number of elements in the node, if it is a sequence or mapping, or 1 otherwise.
533
CV_WRAP size_t size() const;
534
//! returns the node content as an integer. If the node stores floating-point number, it is rounded.
535
operator int() const;
536
//! returns the node content as float
537
operator float() const;
538
//! returns the node content as double
539
operator double() const;
540
//! returns the node content as text string
541
operator String() const;
543
operator std::string() const;
546
//! returns pointer to the underlying file node
547
CvFileNode* operator *();
548
//! returns pointer to the underlying file node
549
const CvFileNode* operator* () const;
551
//! returns iterator pointing to the first node element
552
FileNodeIterator begin() const;
553
//! returns iterator pointing to the element following the last node element
554
FileNodeIterator end() const;
556
/** @brief Reads node elements to the buffer with the specified format.
558
Usually it is more convenient to use operator `>>` instead of this method.
559
@param fmt Specification of each array element. See @ref format_spec "format specification"
560
@param vec Pointer to the destination array.
561
@param len Number of elements to read. If it is greater than number of remaining elements then all
562
of them will be read.
564
void readRaw( const String& fmt, uchar* vec, size_t len ) const;
566
//! reads the registered object and returns pointer to it
567
void* readObj() const;
569
// do not use wrapper pointer classes for better efficiency
570
const CvFileStorage* fs;
571
const CvFileNode* node;
575
/** @brief used to iterate through sequences and mappings.
577
A standard STL notation, with node.begin(), node.end() denoting the beginning and the end of a
578
sequence, stored in node. See the data reading sample in the beginning of the section.
580
class CV_EXPORTS FileNodeIterator
583
/** @brief The constructors.
585
These constructors are used to create a default iterator, set it to specific element in a file node
586
or construct it from another iterator.
591
@param fs File storage for the iterator.
592
@param node File node for the iterator.
593
@param ofs Index of the element in the node. The created iterator will point to this element.
595
FileNodeIterator(const CvFileStorage* fs, const CvFileNode* node, size_t ofs=0);
598
@param it Iterator to be used as initialization for the created iterator.
600
FileNodeIterator(const FileNodeIterator& it);
602
//! returns the currently observed element
603
FileNode operator *() const;
604
//! accesses the currently observed element methods
605
FileNode operator ->() const;
607
//! moves iterator to the next node
608
FileNodeIterator& operator ++ ();
609
//! moves iterator to the next node
610
FileNodeIterator operator ++ (int);
611
//! moves iterator to the previous node
612
FileNodeIterator& operator -- ();
613
//! moves iterator to the previous node
614
FileNodeIterator operator -- (int);
615
//! moves iterator forward by the specified offset (possibly negative)
616
FileNodeIterator& operator += (int ofs);
617
//! moves iterator backward by the specified offset (possibly negative)
618
FileNodeIterator& operator -= (int ofs);
620
/** @brief Reads node elements to the buffer with the specified format.
622
Usually it is more convenient to use operator `>>` instead of this method.
623
@param fmt Specification of each array element. See @ref format_spec "format specification"
624
@param vec Pointer to the destination array.
625
@param maxCount Number of elements to read. If it is greater than number of remaining elements then
626
all of them will be read.
628
FileNodeIterator& readRaw( const String& fmt, uchar* vec,
629
size_t maxCount=(size_t)INT_MAX );
634
void* seq; /* sequence, beign read; CvSeq */
635
void* block; /* current block; CvSeqBlock */
636
schar* ptr; /* pointer to element be read next */
637
schar* block_min; /* pointer to the beginning of block */
638
schar* block_max; /* pointer to the end of block */
639
int delta_index;/* = seq->first->start_index */
640
schar* prev_elem; /* pointer to previous element */
643
const CvFileStorage* fs;
644
const CvFileNode* container;
651
/////////////////// XML & YAML I/O implementation //////////////////
653
//! @relates cv::FileStorage
656
CV_EXPORTS void write( FileStorage& fs, const String& name, int value );
657
CV_EXPORTS void write( FileStorage& fs, const String& name, float value );
658
CV_EXPORTS void write( FileStorage& fs, const String& name, double value );
659
CV_EXPORTS void write( FileStorage& fs, const String& name, const String& value );
660
CV_EXPORTS void write( FileStorage& fs, const String& name, const Mat& value );
661
CV_EXPORTS void write( FileStorage& fs, const String& name, const SparseMat& value );
662
CV_EXPORTS void write( FileStorage& fs, const String& name, const std::vector<KeyPoint>& value);
663
CV_EXPORTS void write( FileStorage& fs, const String& name, const std::vector<DMatch>& value);
665
CV_EXPORTS void writeScalar( FileStorage& fs, int value );
666
CV_EXPORTS void writeScalar( FileStorage& fs, float value );
667
CV_EXPORTS void writeScalar( FileStorage& fs, double value );
668
CV_EXPORTS void writeScalar( FileStorage& fs, const String& value );
672
//! @relates cv::FileNode
675
CV_EXPORTS void read(const FileNode& node, int& value, int default_value);
676
CV_EXPORTS void read(const FileNode& node, float& value, float default_value);
677
CV_EXPORTS void read(const FileNode& node, double& value, double default_value);
678
CV_EXPORTS void read(const FileNode& node, String& value, const String& default_value);
679
CV_EXPORTS void read(const FileNode& node, Mat& mat, const Mat& default_mat = Mat() );
680
CV_EXPORTS void read(const FileNode& node, SparseMat& mat, const SparseMat& default_mat = SparseMat() );
681
CV_EXPORTS void read(const FileNode& node, std::vector<KeyPoint>& keypoints);
682
CV_EXPORTS void read(const FileNode& node, std::vector<DMatch>& matches);
684
template<typename _Tp> static inline void read(const FileNode& node, Point_<_Tp>& value, const Point_<_Tp>& default_value)
686
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
687
value = temp.size() != 2 ? default_value : Point_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
690
template<typename _Tp> static inline void read(const FileNode& node, Point3_<_Tp>& value, const Point3_<_Tp>& default_value)
692
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
693
value = temp.size() != 3 ? default_value : Point3_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]),
694
saturate_cast<_Tp>(temp[2]));
697
template<typename _Tp> static inline void read(const FileNode& node, Size_<_Tp>& value, const Size_<_Tp>& default_value)
699
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
700
value = temp.size() != 2 ? default_value : Size_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
703
template<typename _Tp> static inline void read(const FileNode& node, Complex<_Tp>& value, const Complex<_Tp>& default_value)
705
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
706
value = temp.size() != 2 ? default_value : Complex<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
709
template<typename _Tp> static inline void read(const FileNode& node, Rect_<_Tp>& value, const Rect_<_Tp>& default_value)
711
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
712
value = temp.size() != 4 ? default_value : Rect_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]),
713
saturate_cast<_Tp>(temp[2]), saturate_cast<_Tp>(temp[3]));
716
template<typename _Tp, int cn> static inline void read(const FileNode& node, Vec<_Tp, cn>& value, const Vec<_Tp, cn>& default_value)
718
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
719
value = temp.size() != cn ? default_value : Vec<_Tp, cn>(&temp[0]);
722
template<typename _Tp> static inline void read(const FileNode& node, Scalar_<_Tp>& value, const Scalar_<_Tp>& default_value)
724
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
725
value = temp.size() != 4 ? default_value : Scalar_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]),
726
saturate_cast<_Tp>(temp[2]), saturate_cast<_Tp>(temp[3]));
729
static inline void read(const FileNode& node, Range& value, const Range& default_value)
731
Point2i temp(value.start, value.end); const Point2i default_temp = Point2i(default_value.start, default_value.end);
732
read(node, temp, default_temp);
733
value.start = temp.x; value.end = temp.y;
738
/** @brief Writes string to a file storage.
739
@relates cv::FileStorage
741
CV_EXPORTS FileStorage& operator << (FileStorage& fs, const String& str);
747
class CV_EXPORTS WriteStructContext
750
WriteStructContext(FileStorage& _fs, const String& name, int flags, const String& typeName = String());
751
~WriteStructContext();
756
template<typename _Tp, int numflag> class VecWriterProxy
759
VecWriterProxy( FileStorage* _fs ) : fs(_fs) {}
760
void operator()(const std::vector<_Tp>& vec) const
762
size_t count = vec.size();
763
for (size_t i = 0; i < count; i++)
770
template<typename _Tp> class VecWriterProxy<_Tp, 1>
773
VecWriterProxy( FileStorage* _fs ) : fs(_fs) {}
774
void operator()(const std::vector<_Tp>& vec) const
776
int _fmt = DataType<_Tp>::fmt;
777
char fmt[] = { (char)((_fmt >> 8) + '1'), (char)_fmt, '\0' };
778
fs->writeRaw(fmt, !vec.empty() ? (uchar*)&vec[0] : 0, vec.size() * sizeof(_Tp));
784
template<typename _Tp, int numflag> class VecReaderProxy
787
VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
788
void operator()(std::vector<_Tp>& vec, size_t count) const
790
count = std::min(count, it->remaining);
792
for (size_t i = 0; i < count; i++, ++(*it))
793
read(**it, vec[i], _Tp());
796
FileNodeIterator* it;
799
template<typename _Tp> class VecReaderProxy<_Tp, 1>
802
VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
803
void operator()(std::vector<_Tp>& vec, size_t count) const
805
size_t remaining = it->remaining;
806
size_t cn = DataType<_Tp>::channels;
807
int _fmt = DataType<_Tp>::fmt;
808
char fmt[] = { (char)((_fmt >> 8)+'1'), (char)_fmt, '\0' };
809
size_t remaining1 = remaining / cn;
810
count = count < remaining1 ? count : remaining1;
812
it->readRaw(fmt, !vec.empty() ? (uchar*)&vec[0] : 0, count*sizeof(_Tp));
815
FileNodeIterator* it;
822
//! @relates cv::FileStorage
825
template<typename _Tp> static inline
826
void write(FileStorage& fs, const _Tp& value)
828
write(fs, String(), value);
832
void write( FileStorage& fs, const int& value )
834
writeScalar(fs, value);
838
void write( FileStorage& fs, const float& value )
840
writeScalar(fs, value);
844
void write( FileStorage& fs, const double& value )
846
writeScalar(fs, value);
850
void write( FileStorage& fs, const String& value )
852
writeScalar(fs, value);
855
template<typename _Tp> static inline
856
void write(FileStorage& fs, const Point_<_Tp>& pt )
862
template<typename _Tp> static inline
863
void write(FileStorage& fs, const Point3_<_Tp>& pt )
870
template<typename _Tp> static inline
871
void write(FileStorage& fs, const Size_<_Tp>& sz )
874
write(fs, sz.height);
877
template<typename _Tp> static inline
878
void write(FileStorage& fs, const Complex<_Tp>& c )
884
template<typename _Tp> static inline
885
void write(FileStorage& fs, const Rect_<_Tp>& r )
893
template<typename _Tp, int cn> static inline
894
void write(FileStorage& fs, const Vec<_Tp, cn>& v )
896
for(int i = 0; i < cn; i++)
900
template<typename _Tp> static inline
901
void write(FileStorage& fs, const Scalar_<_Tp>& s )
910
void write(FileStorage& fs, const Range& r )
916
template<typename _Tp> static inline
917
void write( FileStorage& fs, const std::vector<_Tp>& vec )
919
cv::internal::VecWriterProxy<_Tp, DataType<_Tp>::fmt != 0> w(&fs);
924
template<typename _Tp> static inline
925
void write(FileStorage& fs, const String& name, const Point_<_Tp>& pt )
927
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
931
template<typename _Tp> static inline
932
void write(FileStorage& fs, const String& name, const Point3_<_Tp>& pt )
934
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
938
template<typename _Tp> static inline
939
void write(FileStorage& fs, const String& name, const Size_<_Tp>& sz )
941
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
945
template<typename _Tp> static inline
946
void write(FileStorage& fs, const String& name, const Complex<_Tp>& c )
948
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
952
template<typename _Tp> static inline
953
void write(FileStorage& fs, const String& name, const Rect_<_Tp>& r )
955
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
959
template<typename _Tp, int cn> static inline
960
void write(FileStorage& fs, const String& name, const Vec<_Tp, cn>& v )
962
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
966
template<typename _Tp> static inline
967
void write(FileStorage& fs, const String& name, const Scalar_<_Tp>& s )
969
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
974
void write(FileStorage& fs, const String& name, const Range& r )
976
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
980
template<typename _Tp> static inline
981
void write( FileStorage& fs, const String& name, const std::vector<_Tp>& vec )
983
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+(DataType<_Tp>::fmt != 0 ? FileNode::FLOW : 0));
989
//! @relates cv::FileNode
993
void read(const FileNode& node, bool& value, bool default_value)
996
read(node, temp, (int)default_value);
1001
void read(const FileNode& node, uchar& value, uchar default_value)
1004
read(node, temp, (int)default_value);
1005
value = saturate_cast<uchar>(temp);
1009
void read(const FileNode& node, schar& value, schar default_value)
1012
read(node, temp, (int)default_value);
1013
value = saturate_cast<schar>(temp);
1017
void read(const FileNode& node, ushort& value, ushort default_value)
1020
read(node, temp, (int)default_value);
1021
value = saturate_cast<ushort>(temp);
1025
void read(const FileNode& node, short& value, short default_value)
1028
read(node, temp, (int)default_value);
1029
value = saturate_cast<short>(temp);
1032
template<typename _Tp> static inline
1033
void read( FileNodeIterator& it, std::vector<_Tp>& vec, size_t maxCount = (size_t)INT_MAX )
1035
cv::internal::VecReaderProxy<_Tp, DataType<_Tp>::fmt != 0> r(&it);
1039
template<typename _Tp> static inline
1040
void read( const FileNode& node, std::vector<_Tp>& vec, const std::vector<_Tp>& default_value = std::vector<_Tp>() )
1043
vec = default_value;
1046
FileNodeIterator it = node.begin();
1053
//! @relates cv::FileStorage
1056
/** @brief Writes data to a file storage.
1058
template<typename _Tp> static inline
1059
FileStorage& operator << (FileStorage& fs, const _Tp& value)
1061
if( !fs.isOpened() )
1063
if( fs.state == FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP )
1064
CV_Error( Error::StsError, "No element name has been given" );
1065
write( fs, fs.elname, value );
1066
if( fs.state & FileStorage::INSIDE_MAP )
1067
fs.state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP;
1071
/** @brief Writes data to a file storage.
1074
FileStorage& operator << (FileStorage& fs, const char* str)
1076
return (fs << String(str));
1079
/** @brief Writes data to a file storage.
1082
FileStorage& operator << (FileStorage& fs, char* value)
1084
return (fs << String(value));
1089
//! @relates cv::FileNodeIterator
1092
/** @brief Reads data from a file storage.
1094
template<typename _Tp> static inline
1095
FileNodeIterator& operator >> (FileNodeIterator& it, _Tp& value)
1097
read( *it, value, _Tp());
1101
/** @brief Reads data from a file storage.
1103
template<typename _Tp> static inline
1104
FileNodeIterator& operator >> (FileNodeIterator& it, std::vector<_Tp>& vec)
1106
cv::internal::VecReaderProxy<_Tp, DataType<_Tp>::fmt != 0> r(&it);
1107
r(vec, (size_t)INT_MAX);
1111
//! @} FileNodeIterator
1113
//! @relates cv::FileNode
1116
/** @brief Reads data from a file storage.
1118
template<typename _Tp> static inline
1119
void operator >> (const FileNode& n, _Tp& value)
1121
read( n, value, _Tp());
1124
/** @brief Reads data from a file storage.
1126
template<typename _Tp> static inline
1127
void operator >> (const FileNode& n, std::vector<_Tp>& vec)
1129
FileNodeIterator it = n.begin();
1135
//! @relates cv::FileNodeIterator
1139
bool operator == (const FileNodeIterator& it1, const FileNodeIterator& it2)
1141
return it1.fs == it2.fs && it1.container == it2.container &&
1142
it1.reader.ptr == it2.reader.ptr && it1.remaining == it2.remaining;
1146
bool operator != (const FileNodeIterator& it1, const FileNodeIterator& it2)
1148
return !(it1 == it2);
1152
ptrdiff_t operator - (const FileNodeIterator& it1, const FileNodeIterator& it2)
1154
return it2.remaining - it1.remaining;
1158
bool operator < (const FileNodeIterator& it1, const FileNodeIterator& it2)
1160
return it1.remaining > it2.remaining;
1163
//! @} FileNodeIterator
1167
inline FileNode FileStorage::getFirstTopLevelNode() const { FileNode r = root(); FileNodeIterator it = r.begin(); return it != r.end() ? *it : FileNode(); }
1168
inline FileNode::FileNode() : fs(0), node(0) {}
1169
inline FileNode::FileNode(const CvFileStorage* _fs, const CvFileNode* _node) : fs(_fs), node(_node) {}
1170
inline FileNode::FileNode(const FileNode& _node) : fs(_node.fs), node(_node.node) {}
1171
inline bool FileNode::empty() const { return node == 0; }
1172
inline bool FileNode::isNone() const { return type() == NONE; }
1173
inline bool FileNode::isSeq() const { return type() == SEQ; }
1174
inline bool FileNode::isMap() const { return type() == MAP; }
1175
inline bool FileNode::isInt() const { return type() == INT; }
1176
inline bool FileNode::isReal() const { return type() == REAL; }
1177
inline bool FileNode::isString() const { return type() == STR; }
1178
inline CvFileNode* FileNode::operator *() { return (CvFileNode*)node; }
1179
inline const CvFileNode* FileNode::operator* () const { return node; }
1180
inline FileNode::operator int() const { int value; read(*this, value, 0); return value; }
1181
inline FileNode::operator float() const { float value; read(*this, value, 0.f); return value; }
1182
inline FileNode::operator double() const { double value; read(*this, value, 0.); return value; }
1183
inline FileNode::operator String() const { String value; read(*this, value, value); return value; }
1184
inline FileNodeIterator FileNode::begin() const { return FileNodeIterator(fs, node); }
1185
inline FileNodeIterator FileNode::end() const { return FileNodeIterator(fs, node, size()); }
1186
inline void FileNode::readRaw( const String& fmt, uchar* vec, size_t len ) const { begin().readRaw( fmt, vec, len ); }
1187
inline FileNode FileNodeIterator::operator *() const { return FileNode(fs, (const CvFileNode*)(const void*)reader.ptr); }
1188
inline FileNode FileNodeIterator::operator ->() const { return FileNode(fs, (const CvFileNode*)(const void*)reader.ptr); }
1189
inline String::String(const FileNode& fn): cstr_(0), len_(0) { read(fn, *this, *this); }
1195
#endif // __OPENCV_CORE_PERSISTENCE_HPP__