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"
297class CV_EXPORTS FileNode;
298class 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]));
742template<
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]));
749template<
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]));
755template<
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]));
761template<
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]));
768template<
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]);
774template<
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);
785template<
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]));
792static 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;
890template<
typename _Tp>
static inline
891void write(FileStorage& fs,
const _Tp& value)
893 write(fs, String(), value);
897void write( FileStorage& fs,
const int& value )
899 writeScalar(fs, value);
903void write( FileStorage& fs,
const float& value )
905 writeScalar(fs, value);
909void write( FileStorage& fs,
const double& value )
911 writeScalar(fs, value);
915void write( FileStorage& fs,
const String& value )
917 writeScalar(fs, value);
920template<
typename _Tp>
static inline
921void write(FileStorage& fs,
const Point_<_Tp>& pt )
927template<
typename _Tp>
static inline
928void write(FileStorage& fs,
const Point3_<_Tp>& pt )
935template<
typename _Tp>
static inline
936void write(FileStorage& fs,
const Size_<_Tp>& sz )
939 write(fs, sz.height);
942template<
typename _Tp>
static inline
943void write(FileStorage& fs,
const Complex<_Tp>& c )
949template<
typename _Tp>
static inline
950void write(FileStorage& fs,
const Rect_<_Tp>& r )
958template<
typename _Tp,
int cn>
static inline
959void write(FileStorage& fs,
const Vec<_Tp, cn>& v )
961 for(
int i = 0; i < cn; i++)
965template<
typename _Tp,
int m,
int n>
static inline
966void write(FileStorage& fs,
const Matx<_Tp, m, n>& x )
971template<
typename _Tp>
static inline
972void write(FileStorage& fs,
const Scalar_<_Tp>& s )
981void write(FileStorage& fs,
const Range& r )
987template<
typename _Tp>
static inline
988void write( FileStorage& fs,
const std::vector<_Tp>& vec )
990 cv::internal::VecWriterProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> w(&fs);
994template<
typename _Tp>
static inline
995void write(FileStorage& fs,
const String& name,
const Point_<_Tp>& pt )
1001template<
typename _Tp>
static inline
1002void write(FileStorage& fs,
const String& name,
const Point3_<_Tp>& pt )
1008template<
typename _Tp>
static inline
1009void write(FileStorage& fs,
const String& name,
const Size_<_Tp>& sz )
1015template<
typename _Tp>
static inline
1016void write(FileStorage& fs,
const String& name,
const Complex<_Tp>& c )
1022template<
typename _Tp>
static inline
1023void write(FileStorage& fs,
const String& name,
const Rect_<_Tp>& r )
1029template<
typename _Tp,
int cn>
static inline
1030void write(FileStorage& fs,
const String& name,
const Vec<_Tp, cn>& v )
1036template<
typename _Tp,
int m,
int n>
static inline
1037void write(FileStorage& fs,
const String& name,
const Matx<_Tp, m, n>& x )
1039 write(fs, name, Mat(x));
1042template<
typename _Tp>
static inline
1043void write(FileStorage& fs,
const String& name,
const Scalar_<_Tp>& s )
1050void write(FileStorage& fs,
const String& name,
const Range& r )
1057void 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);
1070void 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>
1080static inline void write( FileStorage& fs,
const String& name,
const _Tp& val )
1082 write(fs, name,
static_cast<int>(val));
1085template<
typename _Tp>
static inline
1086void write( FileStorage& fs,
const String& name,
const std::vector<_Tp>& vec )
1092template<
typename _Tp>
static inline
1093void 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
1106static inline void write(FileStorage& fs,
const KeyPoint& kpt) { write(fs, String(), kpt); }
1107static inline void write(FileStorage& fs,
const DMatch& m) { write(fs, String(), m); }
1108static inline void write(FileStorage& fs,
const std::vector<KeyPoint>& vec)
1110 cv::internal::VecWriterProxy<KeyPoint, 0> w(&fs);
1113static inline void write(FileStorage& fs,
const std::vector<DMatch>& vec)
1115 cv::internal::VecWriterProxy<DMatch, 0> w(&fs);
1127void read(
const FileNode& node,
bool& value,
bool default_value)
1130 read(node, temp, (
int)default_value);
1135void read(
const FileNode& node, uchar& value, uchar default_value)
1138 read(node, temp, (
int)default_value);
1139 value = saturate_cast<uchar>(temp);
1143void read(
const FileNode& node, schar& value, schar default_value)
1146 read(node, temp, (
int)default_value);
1147 value = saturate_cast<schar>(temp);
1151void read(
const FileNode& node, ushort& value, ushort default_value)
1154 read(node, temp, (
int)default_value);
1155 value = saturate_cast<ushort>(temp);
1159void read(
const FileNode& node,
short& value,
short default_value)
1162 read(node, temp, (
int)default_value);
1163 value = saturate_cast<short>(temp);
1166template<
typename _Tp>
static inline
1167void 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>
1174static 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);
1181template<
typename _Tp>
static inline
1182void 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();
1194void read(
const FileNode& node, std::vector<KeyPoint>& vec,
const std::vector<KeyPoint>& default_value )
1197 vec = default_value;
1203void read(
const FileNode& node, std::vector<DMatch>& vec,
const std::vector<DMatch>& default_value )
1206 vec = default_value;
1218template<
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));
1254template<
typename _Tp>
static inline
1257 read( *it, value, _Tp());
1263template<
typename _Tp>
static inline
1266 cv::internal::VecReaderProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> r(&it);
1267 r(vec, (
size_t)INT_MAX);
1278template<
typename _Tp>
static inline
1281 read( n, value, _Tp());
1286template<
typename _Tp>
static inline
1287void operator >> (
const FileNode& n, std::vector<_Tp>& vec)
1303#ifdef CV__LEGACY_PERSISTENCE
1305void operator >> (
const FileNode& n, std::vector<KeyPoint>& vec)
1310void operator >> (
const FileNode& n, std::vector<DMatch>& vec)
1341bool 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