44
#ifndef OPENCV_CORE_PERSISTENCE_HPP
45
#define OPENCV_CORE_PERSISTENCE_HPP
49
#define CV__LEGACY_PERSISTENCE
53
# error persistence.hpp header must be compiled as C++
100
#include "opencv2/core/types.hpp"
101
#include "opencv2/core/mat.hpp"
297
class
CV_EXPORTS FileNode;
298
class
CV_EXPORTS FileNodeIterator;
314
FORMAT_MASK = (7<<3),
317
FORMAT_YAML = (2<<3),
318
FORMAT_JSON = (3<<3),
321
WRITE_BASE64 = BASE64 | WRITE,
341
CV_WRAP
FileStorage(
const
String& filename,
int
flags,
const
String& encoding=String());
361
CV_WRAP
virtual
bool
open(
const
String& filename,
int
flags,
const
String& encoding=String());
368
CV_WRAP
virtual
bool
isOpened()
const;
374
CV_WRAP
virtual
void
release();
381
CV_WRAP
virtual
String releaseAndGetString();
409
CV_WRAP
void
write(
const
String& name,
int
val);
411
CV_WRAP
void
write(
const
String& name,
double
val);
413
CV_WRAP
void
write(
const
String& name,
const
String& val);
415
CV_WRAP
void
write(
const
String& name,
const
Mat& val);
417
CV_WRAP
void
write(
const
String& name,
const
std::vector<String>& val);
427
void
writeRaw(
const
String& fmt,
const
void* vec,
size_t
len );
437
CV_WRAP
void
writeComment(
const
String& comment,
bool
append =
false);
445
CV_WRAP
void
startWriteStruct(
const
String& name,
int
flags,
const
String& typeName=String());
449
CV_WRAP
void
endWriteStruct();
460
CV_WRAP
int
getFormat()
const;
545
CV_WRAP std::vector<String> keys()
const;
550
CV_WRAP
int
type()
const;
553
CV_WRAP
bool
empty()
const;
555
CV_WRAP
bool
isNone()
const;
557
CV_WRAP
bool
isSeq()
const;
559
CV_WRAP
bool
isMap()
const;
561
CV_WRAP
bool
isInt()
const;
563
CV_WRAP
bool
isReal()
const;
565
CV_WRAP
bool
isString()
const;
567
CV_WRAP
bool
isNamed()
const;
569
CV_WRAP std::string name()
const;
571
CV_WRAP
size_t
size()
const;
573
CV_WRAP
size_t
rawSize()
const;
575
operator
int()
const;
577
operator
float()
const;
579
operator
double()
const;
581
inline
operator
std::string()
const
{
return
this->
string(); }
583
static
bool
isMap(
int
flags);
584
static
bool
isSeq(
int
flags);
585
static
bool
isCollection(
int
flags);
586
static
bool
isEmptyCollection(
int
flags);
587
static
bool
isFlow(
int
flags);
590
const
uchar* ptr()
const;
605
void
readRaw(
const
String& fmt,
void* vec,
size_t
len )
const;
610
void
setValue(
int
type,
const
void* value,
int
len=-1 );
613
CV_WRAP
double
real()
const;
615
CV_WRAP std::string string()
const;
620
FileNode(FileStorage::Impl* fs,
size_t
blockIdx,
size_t
ofs);
622
FileStorage::Impl* fs;
679
size_t
len=(
size_t)INT_MAX );
687
FileStorage::Impl* fs;
702CV_EXPORTS
void
write(
FileStorage& fs,
const
String& name,
int
value );
703CV_EXPORTS
void
write(
FileStorage& fs,
const
String& name,
float
value );
704CV_EXPORTS
void
write(
FileStorage& fs,
const
String& name,
double
value );
705CV_EXPORTS
void
write(
FileStorage& fs,
const
String& name,
const
String& value );
706CV_EXPORTS
void
write(
FileStorage& fs,
const
String& name,
const
Mat& value );
708
#ifdef CV__LEGACY_PERSISTENCE
709CV_EXPORTS
void
write(
FileStorage& fs,
const
String& name,
const
std::vector<KeyPoint>& value);
710CV_EXPORTS
void
write(
FileStorage& fs,
const
String& name,
const
std::vector<DMatch>& value);
713CV_EXPORTS
void
writeScalar(
FileStorage& fs,
int
value );
714CV_EXPORTS
void
writeScalar(
FileStorage& fs,
float
value );
715CV_EXPORTS
void
writeScalar(
FileStorage& fs,
double
value );
716CV_EXPORTS
void
writeScalar(
FileStorage& fs,
const
String& value );
723CV_EXPORTS
void
read(
const
FileNode& node,
int& value,
int
default_value);
724CV_EXPORTS
void
read(
const
FileNode& node,
float& value,
float
default_value);
725CV_EXPORTS
void
read(
const
FileNode& node,
double& value,
double
default_value);
726CV_EXPORTS
void
read(
const
FileNode& node, std::string& value,
const
std::string& default_value);
727CV_EXPORTS
void
read(
const
FileNode& node,
Mat& mat,
const
Mat& default_mat =
Mat() );
729
#ifdef CV__LEGACY_PERSISTENCE
730CV_EXPORTS
void
read(
const
FileNode& node, std::vector<KeyPoint>& keypoints);
731CV_EXPORTS
void
read(
const
FileNode& node, std::vector<DMatch>& matches);
739
value = temp.size() != 2 ? default_value :
Point_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
742
template<
typename
_Tp>
static
inline
void
read(
const
FileNode& node, Point3_<_Tp>& value,
const
Point3_<_Tp>& default_value)
744
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
745
value = temp.size() != 3 ? default_value : Point3_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]),
746
saturate_cast<_Tp>(temp[2]));
749
template<
typename
_Tp>
static
inline
void
read(
const
FileNode& node, Size_<_Tp>& value,
const
Size_<_Tp>& default_value)
751
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
752
value = temp.size() != 2 ? default_value : Size_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
755
template<
typename
_Tp>
static
inline
void
read(
const
FileNode& node, Complex<_Tp>& value,
const
Complex<_Tp>& default_value)
757
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
758
value = temp.size() != 2 ? default_value : Complex<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
761
template<
typename
_Tp>
static
inline
void
read(
const
FileNode& node, Rect_<_Tp>& value,
const
Rect_<_Tp>& default_value)
763
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
764
value = temp.size() != 4 ? default_value : Rect_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]),
765
saturate_cast<_Tp>(temp[2]), saturate_cast<_Tp>(temp[3]));
768
template<
typename
_Tp,
int
cn>
static
inline
void
read(
const
FileNode& node, Vec<_Tp, cn>& value,
const
Vec<_Tp, cn>& default_value)
770
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
771
value = temp.size() != cn ? default_value : Vec<_Tp, cn>(&temp[0]);
774
template<
typename
_Tp,
int
m,
int
n>
static
inline
void
read(
const
FileNode& node, Matx<_Tp, m, n>& value,
const
Matx<_Tp, m, n>& default_matx = Matx<_Tp, m, n>())
780
value = default_matx;
782
value = Matx<_Tp, m, n>(temp);
785
template<
typename
_Tp>
static
inline
void
read(
const
FileNode& node, Scalar_<_Tp>& value,
const
Scalar_<_Tp>& default_value)
787
std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
788
value = temp.size() != 4 ? default_value : Scalar_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]),
789
saturate_cast<_Tp>(temp[2]), saturate_cast<_Tp>(temp[3]));
792
static
inline
void
read(
const
FileNode& node, Range& value,
const
Range& default_value)
794
Point2i temp(value.start, value.end);
const
Point2i default_temp = Point2i(default_value.start, default_value.end);
795
read(node, temp, default_temp);
796
value.start = temp.x; value.end = temp.y;
810
class
CV_EXPORTS WriteStructContext
813
WriteStructContext(
FileStorage& _fs,
const
String& name,
int
flags,
const
String& typeName = String());
814
~WriteStructContext();
819
template<
typename
_Tp,
int
numflag>
class
VecWriterProxy
823
void
operator()(
const
std::vector<_Tp>& vec)
const
825
size_t
count = vec.size();
826
for
(
size_t
i = 0; i < count; i++)
833
template<
typename
_Tp>
class
VecWriterProxy<_Tp, 1>
836
VecWriterProxy( FileStorage* _fs ) : fs(_fs) {}
837
void
operator()(
const
std::vector<_Tp>& vec)
const
839
int
_fmt = traits::SafeFmt<_Tp>::fmt;
840
char
fmt[] = { (char)((_fmt >> 8) +
'1'), (
char)_fmt,
'\0'
};
841
fs->writeRaw(fmt, !vec.empty() ? (uchar*)&vec[0] : 0, vec.size() *
sizeof(_Tp));
847
template<
typename
_Tp,
int
numflag>
class
VecReaderProxy
850
VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
851
void
operator()(std::vector<_Tp>& vec,
size_t
count)
const
853
count =
std::min(count, it->remaining());
855
for
(
size_t
i = 0; i < count; i++, ++(*it))
856
read(**it, vec[i], _Tp());
859
FileNodeIterator* it;
862
template<
typename
_Tp>
class
VecReaderProxy<_Tp, 1>
865
VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
866
void
operator()(std::vector<_Tp>& vec,
size_t
count)
const
868
size_t
remaining = it->remaining();
869
size_t
cn = DataType<_Tp>::channels;
870
int
_fmt = traits::SafeFmt<_Tp>::fmt;
872
char
fmt[] = { (char)((_fmt >> 8)+
'1'), (
char)_fmt,
'\0'
};
874
size_t
remaining1 = remaining / cn;
875
count = count > remaining1 ? remaining1 : count;
877
it->readRaw(fmt, !vec.empty() ? (uchar*)&vec[0] : 0, count*
sizeof(_Tp));
880
FileNodeIterator* it;
890
template<
typename
_Tp>
static
inline
891
void
write(FileStorage& fs,
const
_Tp& value)
893
write(fs, String(), value);
897
void
write( FileStorage& fs,
const
int& value )
899
writeScalar(fs, value);
903
void
write( FileStorage& fs,
const
float& value )
905
writeScalar(fs, value);
909
void
write( FileStorage& fs,
const
double& value )
911
writeScalar(fs, value);
915
void
write( FileStorage& fs,
const
String& value )
917
writeScalar(fs, value);
920
template<
typename
_Tp>
static
inline
921
void
write(FileStorage& fs,
const
Point_<_Tp>& pt )
927
template<
typename
_Tp>
static
inline
928
void
write(FileStorage& fs,
const
Point3_<_Tp>& pt )
935
template<
typename
_Tp>
static
inline
936
void
write(FileStorage& fs,
const
Size_<_Tp>& sz )
939
write(fs, sz.height);
942
template<
typename
_Tp>
static
inline
943
void
write(FileStorage& fs,
const
Complex<_Tp>& c )
949
template<
typename
_Tp>
static
inline
950
void
write(FileStorage& fs,
const
Rect_<_Tp>& r )
958
template<
typename
_Tp,
int
cn>
static
inline
959
void
write(FileStorage& fs,
const
Vec<_Tp, cn>& v )
961
for(
int
i = 0; i < cn; i++)
965
template<
typename
_Tp,
int
m,
int
n>
static
inline
966
void
write(FileStorage& fs,
const
Matx<_Tp, m, n>& x )
971
template<
typename
_Tp>
static
inline
972
void
write(FileStorage& fs,
const
Scalar_<_Tp>& s )
981
void
write(FileStorage& fs,
const
Range& r )
987
template<
typename
_Tp>
static
inline
988
void
write( FileStorage& fs,
const
std::vector<_Tp>& vec )
990
cv::internal::VecWriterProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> w(&fs);
994
template<
typename
_Tp>
static
inline
995
void
write(FileStorage& fs,
const
String& name,
const
Point_<_Tp>& pt )
1001
template<
typename
_Tp>
static
inline
1002
void
write(FileStorage& fs,
const
String& name,
const
Point3_<_Tp>& pt )
1008
template<
typename
_Tp>
static
inline
1009
void
write(FileStorage& fs,
const
String& name,
const
Size_<_Tp>& sz )
1015
template<
typename
_Tp>
static
inline
1016
void
write(FileStorage& fs,
const
String& name,
const
Complex<_Tp>& c )
1022
template<
typename
_Tp>
static
inline
1023
void
write(FileStorage& fs,
const
String& name,
const
Rect_<_Tp>& r )
1029
template<
typename
_Tp,
int
cn>
static
inline
1030
void
write(FileStorage& fs,
const
String& name,
const
Vec<_Tp, cn>& v )
1036
template<
typename
_Tp,
int
m,
int
n>
static
inline
1037
void
write(FileStorage& fs,
const
String& name,
const
Matx<_Tp, m, n>& x )
1039
write(fs, name, Mat(x));
1042
template<
typename
_Tp>
static
inline
1043
void
write(FileStorage& fs,
const
String& name,
const
Scalar_<_Tp>& s )
1050
void
write(FileStorage& fs,
const
String& name,
const
Range& r )
1057
void
write(FileStorage& fs,
const
String& name,
const
KeyPoint& kpt)
1060
write(fs, kpt.pt.x);
1061
write(fs, kpt.pt.y);
1062
write(fs, kpt.size);
1063
write(fs, kpt.angle);
1064
write(fs, kpt.response);
1065
write(fs, kpt.octave);
1066
write(fs, kpt.class_id);
1070
void
write(FileStorage& fs,
const
String& name,
const
DMatch& m)
1073
write(fs, m.queryIdx);
1074
write(fs, m.trainIdx);
1075
write(fs, m.imgIdx);
1076
write(fs, m.distance);
1079template<typename _Tp, typename std::enable_if< std::is_enum<_Tp>::value >::type* =
nullptr>
1080
static
inline
void
write( FileStorage& fs,
const
String& name,
const
_Tp& val )
1082
write(fs, name,
static_cast<
int
>(val));
1085
template<
typename
_Tp>
static
inline
1086
void
write( FileStorage& fs,
const
String& name,
const
std::vector<_Tp>& vec )
1092
template<
typename
_Tp>
static
inline
1093
void
write( FileStorage& fs,
const
String& name,
const
std::vector< std::vector<_Tp> >& vec )
1095
cv::internal::WriteStructContext ws(fs, name,
FileNode::SEQ);
1096
for(
size_t
i = 0; i < vec.size(); i++)
1103
#ifdef CV__LEGACY_PERSISTENCE
1106
static
inline
void
write(FileStorage& fs,
const
KeyPoint& kpt) { write(fs, String(), kpt); }
1107
static
inline
void
write(FileStorage& fs,
const
DMatch& m) { write(fs, String(), m); }
1108
static
inline
void
write(FileStorage& fs,
const
std::vector<KeyPoint>& vec)
1110
cv::internal::VecWriterProxy<KeyPoint, 0> w(&fs);
1113
static
inline
void
write(FileStorage& fs,
const
std::vector<DMatch>& vec)
1115
cv::internal::VecWriterProxy<DMatch, 0> w(&fs);
1127
void
read(
const
FileNode& node,
bool& value,
bool
default_value)
1130
read(node, temp, (
int)default_value);
1135
void
read(
const
FileNode& node, uchar& value, uchar default_value)
1138
read(node, temp, (
int)default_value);
1139
value = saturate_cast<uchar>(temp);
1143
void
read(
const
FileNode& node, schar& value, schar default_value)
1146
read(node, temp, (
int)default_value);
1147
value = saturate_cast<schar>(temp);
1151
void
read(
const
FileNode& node, ushort& value, ushort default_value)
1154
read(node, temp, (
int)default_value);
1155
value = saturate_cast<ushort>(temp);
1159
void
read(
const
FileNode& node,
short& value,
short
default_value)
1162
read(node, temp, (
int)default_value);
1163
value = saturate_cast<short>(temp);
1166
template<
typename
_Tp>
static
inline
1167
void
read( FileNodeIterator& it, std::vector<_Tp>& vec,
size_t
maxCount = (
size_t)INT_MAX )
1169
cv::internal::VecReaderProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> r(&it);
1173template<typename _Tp, typename std::enable_if< std::is_enum<_Tp>::value >::type* =
nullptr>
1174
static
inline
void
read(
const
FileNode& node, _Tp& value,
const
_Tp& default_value =
static_cast<_Tp
>(0))
1177
read(node, temp,
static_cast<
int
>(default_value));
1178
value =
static_cast<_Tp
>(temp);
1181
template<
typename
_Tp>
static
inline
1182
void
read(
const
FileNode& node, std::vector<_Tp>& vec,
const
std::vector<_Tp>& default_value = std::vector<_Tp>() )
1185
vec = default_value;
1188
FileNodeIterator it = node.begin();
1194
void
read(
const
FileNode& node, std::vector<KeyPoint>& vec,
const
std::vector<KeyPoint>& default_value )
1197
vec = default_value;
1203
void
read(
const
FileNode& node, std::vector<DMatch>& vec,
const
std::vector<DMatch>& default_value )
1206
vec = default_value;
1218
template<
typename
_Tp>
static
inline
1223
if( fs.state == FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP )
1224
CV_Error( Error::StsError,
"No element name has been given"
);
1225
write( fs, fs.elname, value );
1226
if( fs.state & FileStorage::INSIDE_MAP )
1227
fs.state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP;
1236
return
(fs << String(str));
1244
return
(fs << String(value));
1254
template<
typename
_Tp>
static
inline
1257
read( *it, value, _Tp());
1263
template<
typename
_Tp>
static
inline
1266
cv::internal::VecReaderProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> r(&it);
1267
r(vec, (
size_t)INT_MAX);
1278
template<
typename
_Tp>
static
inline
1281
read( n, value, _Tp());
1286
template<
typename
_Tp>
static
inline
1287
void
operator >> (
const
FileNode& n, std::vector<_Tp>& vec)
1303
#ifdef CV__LEGACY_PERSISTENCE
1305
void
operator >> (
const
FileNode& n, std::vector<KeyPoint>& vec)
1310
void
operator >> (
const
FileNode& n, std::vector<DMatch>& vec)
1341
bool
operator < (
const
FileNodeIterator& it1,
const
FileNodeIterator& it2)
1343
return
it1.remaining() > it2.remaining();
Class for matching keypoint descriptors
Definition:
core/types.hpp:809
CV_PROP_RW int queryIdx
query descriptor index
Definition:
core/types.hpp:815
CV_PROP_RW int imgIdx
train image index
Definition:
core/types.hpp:817
CV_PROP_RW int trainIdx
train descriptor index
Definition:
core/types.hpp:816
File Storage Node class.
Definition:
persistence.hpp:482
FileNodeIterator begin() const
returns iterator pointing to the first node element
operator std::string() const
returns the node content as text string
Definition:
persistence.hpp:581
void setValue(int type, const void *value, int len=-1)
FileNode(const FileNode &node)
FileNode operator[](const String &nodename) const
Returns element of a mapping node or a sequence node.
FileNodeIterator end() const
returns iterator pointing to the element following the last node element
@ FLOW
compact representation of a sequence or mapping. Used only by YAML writer
Definition:
persistence.hpp:497
@ SEQ
sequence
Definition:
persistence.hpp:493
CV_WRAP_AS(getNode) FileNode operator[](const char *nodename) const
void readRaw(const String &fmt, void *vec, size_t len) const
Reads node elements to the buffer with the specified format.
CV_WRAP_AS(at) FileNode operator[](int i) const
FileNode(const FileStorage *fs, size_t blockIdx, size_t ofs)
used to iterate through sequences and mappings.
Definition:
persistence.hpp:634
FileNodeIterator(const FileNodeIterator &it)
size_t remaining() const
returns the number of remaining (not read yet) elements
FileNodeIterator & readRaw(const String &fmt, void *vec, size_t len=(size_t) INT_MAX)
Reads node elements to the buffer with the specified format.
FileNodeIterator()
The constructors.
FileNodeIterator(const FileNode &node, bool seekEnd)
XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or readi...
Definition:
persistence.hpp:304
void writeRaw(const String &fmt, const void *vec, size_t len)
Writes multiple numbers.
virtual ~FileStorage()
the destructor. calls release()
Mode
file storage mode
Definition:
persistence.hpp:308
CV_WRAP_AS(getNode) FileNode operator[](const char *nodename) const
static String getDefaultObjectName(const String &filename)
Returns the normalized object name for the specified name of a file.
virtual CV_WRAP bool isOpened() const
Checks whether the file is opened.
FileNode operator[](const String &nodename) const
Returns the specified element of the top-level mapping.
Data structure for salient point detectors.
Definition:
core/types.hpp:712
CV_PROP_RW float angle
Definition:
core/types.hpp:773
CV_PROP_RW Point2f pt
coordinates of the keypoints
Definition:
core/types.hpp:771
CV_PROP_RW int octave
octave (pyramid layer) from which the keypoint has been extracted
Definition:
core/types.hpp:777
CV_PROP_RW float response
the response by which the most strong keypoints have been selected. Can be used for the further sorti...
Definition:
core/types.hpp:776
CV_PROP_RW int class_id
object class (if the keypoints need to be clustered by an object they belong to)
Definition:
core/types.hpp:778
CV_PROP_RW float size
diameter of the meaningful keypoint neighborhood
Definition:
core/types.hpp:772
n-dimensional dense array class
Definition:
mat.hpp:802
Template class for 2D points specified by its coordinates x and y.
Definition:
core/types.hpp:158
_Tp y
y coordinate of the point
Definition:
core/types.hpp:187
_Tp x
x coordinate of the point
Definition:
core/types.hpp:186
The class SparseMat represents multi-dimensional sparse numerical arrays.
Definition:
mat.hpp:2704
CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element minimum of two arrays or an array and a scalar.
#define CV_Error(code, msg)
Call the error handler.
Definition:
base.hpp:320
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails
Definition:
base.hpp:342
"black box" representation of the file storage associated with a file on disk.
Definition:
aruco.hpp:75
Definition:
cvstd_wrapper.hpp:74