EstervQrCode 1.1.1
Library for qr code manipulation
persistence.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
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.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
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.
27 //
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.
30 //
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.
41 //
42 //M*/
43 
44 #ifndef OPENCV_CORE_PERSISTENCE_HPP
45 #define OPENCV_CORE_PERSISTENCE_HPP
46 
47 #ifndef CV_DOXYGEN
49 #define CV__LEGACY_PERSISTENCE
50 #endif
51 
52 #ifndef __cplusplus
53 # error persistence.hpp header must be compiled as C++
54 #endif
55 
58 
99 
100 #include "opencv2/core/types.hpp"
101 #include "opencv2/core/mat.hpp"
102 
103 namespace cv {
104 
290 
296 
297 class CV_EXPORTS FileNode;
299 
304 {
305 public:
307  enum Mode
308  {
309  READ = 0,
310  WRITE = 1,
311  APPEND = 2,
312  MEMORY = 4,
314  FORMAT_MASK = (7<<3),
315  FORMAT_AUTO = 0,
316  FORMAT_XML = (1<<3),
317  FORMAT_YAML = (2<<3),
318  FORMAT_JSON = (3<<3),
319 
320  BASE64 = 64,
321  WRITE_BASE64 = BASE64 | WRITE,
322  };
323  enum State
324  {
325  UNDEFINED = 0,
326  VALUE_EXPECTED = 1,
327  NAME_EXPECTED = 2,
328  INSIDE_MAP = 4
329  };
330 
337 
341  CV_WRAP FileStorage(const String& filename, int flags, const String& encoding=String());
342 
344  virtual ~FileStorage();
345 
361  CV_WRAP virtual bool open(const String& filename, int flags, const String& encoding=String());
362 
368  CV_WRAP virtual bool isOpened() const;
369 
374  CV_WRAP virtual void release();
375 
382 
387 
393  CV_WRAP FileNode root(int streamidx=0) const;
394 
399  FileNode operator[](const String& nodename) const;
400 
402  CV_WRAP_AS(getNode) FileNode operator[](const char* nodename) const;
403 
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);
418 
427  void writeRaw( const String& fmt, const void* vec, size_t len );
428 
437  CV_WRAP void writeComment(const String& comment, bool append = false);
438 
445  CV_WRAP void startWriteStruct(const String& name, int flags, const String& typeName=String());
446 
450 
456 
460  CV_WRAP int getFormat() const;
461 
462  int state;
464 
465  class Impl;
467 };
468 
482 {
483 public:
485  enum
486  {
487  NONE = 0,
488  INT = 1,
489  REAL = 2,
491  STR = 3,
492  STRING = STR,
493  SEQ = 4,
494  MAP = 5,
495  TYPE_MASK = 7,
496 
497  FLOW = 8,
498  UNIFORM = 8,
500  EMPTY = 16,
501  NAMED = 32
502  };
509 
517  FileNode(const FileStorage* fs, size_t blockIdx, size_t ofs);
518 
522  FileNode(const FileNode& node);
523 
524  FileNode& operator=(const FileNode& node);
525 
530  FileNode operator[](const String& nodename) const;
531 
535  CV_WRAP_AS(getNode) FileNode operator[](const char* nodename) const;
536 
540  CV_WRAP_AS(at) FileNode operator[](int i) const;
541 
546 
550  CV_WRAP int type() const;
551 
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;
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(); }
582 
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);
588 
589  uchar* ptr();
590  const uchar* ptr() const;
591 
596 
605  void readRaw( const String& fmt, void* vec, size_t len ) const;
606 
610  void setValue( int type, const void* value, int len=-1 );
611 
613  CV_WRAP double real() const;
617  CV_WRAP Mat mat() const;
618 
619  //protected:
620  FileNode(FileStorage::Impl* fs, size_t blockIdx, size_t ofs);
621 
622  FileStorage::Impl* fs;
623  size_t blockIdx;
624  size_t ofs;
625 };
626 
627 
634 {
635 public:
642 
651  FileNodeIterator(const FileNode& node, bool seekEnd);
652 
657 
659 
662 
664  FileNodeIterator& operator ++ ();
666  FileNodeIterator operator ++ (int);
669 
678  FileNodeIterator& readRaw( const String& fmt, void* vec,
679  size_t len=(size_t)INT_MAX );
680 
682  size_t remaining() const;
683 
684  bool equalTo(const FileNodeIterator& it) const;
685 
686 protected:
687  FileStorage::Impl* fs;
688  size_t blockIdx;
689  size_t ofs;
690  size_t blockSize;
691  size_t nodeNElems;
692  size_t idx;
693 };
694 
696 
698 
699 CV_EXPORTS void write( FileStorage& fs, const String& name, int value );
700 CV_EXPORTS void write( FileStorage& fs, const String& name, float value );
701 CV_EXPORTS void write( FileStorage& fs, const String& name, double value );
702 CV_EXPORTS void write( FileStorage& fs, const String& name, const String& value );
703 CV_EXPORTS void write( FileStorage& fs, const String& name, const Mat& value );
704 CV_EXPORTS void write( FileStorage& fs, const String& name, const SparseMat& value );
705 #ifdef CV__LEGACY_PERSISTENCE
707 CV_EXPORTS void write( FileStorage& fs, const String& name, const std::vector<DMatch>& value);
708 #endif
709 
714 
715 CV_EXPORTS void read(const FileNode& node, int& value, int default_value);
716 CV_EXPORTS void read(const FileNode& node, float& value, float default_value);
717 CV_EXPORTS void read(const FileNode& node, double& value, double default_value);
718 CV_EXPORTS void read(const FileNode& node, std::string& value, const std::string& default_value);
719 CV_EXPORTS void read(const FileNode& node, Mat& mat, const Mat& default_mat = Mat() );
720 CV_EXPORTS void read(const FileNode& node, SparseMat& mat, const SparseMat& default_mat = SparseMat() );
721 #ifdef CV__LEGACY_PERSISTENCE
722 CV_EXPORTS void read(const FileNode& node, std::vector<KeyPoint>& keypoints);
723 CV_EXPORTS void read(const FileNode& node, std::vector<DMatch>& matches);
724 #endif
725 CV_EXPORTS void read(const FileNode& node, KeyPoint& value, const KeyPoint& default_value);
726 CV_EXPORTS void read(const FileNode& node, DMatch& value, const DMatch& default_value);
727 
728 template<typename _Tp> static inline void read(const FileNode& node, Point_<_Tp>& value, const Point_<_Tp>& default_value)
729 {
730  std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
731  value = temp.size() != 2 ? default_value : Point_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
732 }
733 
734 template<typename _Tp> static inline void read(const FileNode& node, Point3_<_Tp>& value, const Point3_<_Tp>& default_value)
735 {
736  std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
737  value = temp.size() != 3 ? default_value : Point3_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]),
738  saturate_cast<_Tp>(temp[2]));
739 }
740 
741 template<typename _Tp> static inline void read(const FileNode& node, Size_<_Tp>& value, const Size_<_Tp>& default_value)
742 {
743  std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
744  value = temp.size() != 2 ? default_value : Size_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
745 }
746 
747 template<typename _Tp> static inline void read(const FileNode& node, Complex<_Tp>& value, const Complex<_Tp>& default_value)
748 {
749  std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
750  value = temp.size() != 2 ? default_value : Complex<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
751 }
752 
753 template<typename _Tp> static inline void read(const FileNode& node, Rect_<_Tp>& value, const Rect_<_Tp>& default_value)
754 {
755  std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
756  value = temp.size() != 4 ? default_value : Rect_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]),
757  saturate_cast<_Tp>(temp[2]), saturate_cast<_Tp>(temp[3]));
758 }
759 
760 template<typename _Tp, int cn> static inline void read(const FileNode& node, Vec<_Tp, cn>& value, const Vec<_Tp, cn>& default_value)
761 {
762  std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
763  value = temp.size() != cn ? default_value : Vec<_Tp, cn>(&temp[0]);
764 }
765 
766 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>())
767 {
768  Mat temp;
769  read(node, temp); // read as a Mat class
770 
771  if (temp.empty())
772  value = default_matx;
773  else
775 }
776 
777 template<typename _Tp> static inline void read(const FileNode& node, Scalar_<_Tp>& value, const Scalar_<_Tp>& default_value)
778 {
779  std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
780  value = temp.size() != 4 ? default_value : Scalar_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]),
781  saturate_cast<_Tp>(temp[2]), saturate_cast<_Tp>(temp[3]));
782 }
783 
784 static inline void read(const FileNode& node, Range& value, const Range& default_value)
785 {
786  Point2i temp(value.start, value.end); const Point2i default_temp = Point2i(default_value.start, default_value.end);
787  read(node, temp, default_temp);
788  value.start = temp.x; value.end = temp.y;
789 }
790 
794 
796 
797 namespace internal
798 {
799  class CV_EXPORTS WriteStructContext
800  {
801  public:
802  WriteStructContext(FileStorage& _fs, const String& name, int flags, const String& typeName = String());
803  ~WriteStructContext();
804  private:
805  FileStorage* fs;
806  };
807 
808  template<typename _Tp, int numflag> class VecWriterProxy
809  {
810  public:
811  VecWriterProxy( FileStorage* _fs ) : fs(_fs) {}
812  void operator()(const std::vector<_Tp>& vec) const
813  {
814  size_t count = vec.size();
815  for (size_t i = 0; i < count; i++)
816  write(*fs, vec[i]);
817  }
818  private:
819  FileStorage* fs;
820  };
821 
822  template<typename _Tp> class VecWriterProxy<_Tp, 1>
823  {
824  public:
825  VecWriterProxy( FileStorage* _fs ) : fs(_fs) {}
826  void operator()(const std::vector<_Tp>& vec) const
827  {
828  int _fmt = traits::SafeFmt<_Tp>::fmt;
829  char fmt[] = { (char)((_fmt >> 8) + '1'), (char)_fmt, '\0' };
830  fs->writeRaw(fmt, !vec.empty() ? (uchar*)&vec[0] : 0, vec.size() * sizeof(_Tp));
831  }
832  private:
833  FileStorage* fs;
834  };
835 
836  template<typename _Tp, int numflag> class VecReaderProxy
837  {
838  public:
839  VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
840  void operator()(std::vector<_Tp>& vec, size_t count) const
841  {
842  count = std::min(count, it->remaining());
843  vec.resize(count);
844  for (size_t i = 0; i < count; i++, ++(*it))
845  read(**it, vec[i], _Tp());
846  }
847  private:
848  FileNodeIterator* it;
849  };
850 
851  template<typename _Tp> class VecReaderProxy<_Tp, 1>
852  {
853  public:
854  VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
855  void operator()(std::vector<_Tp>& vec, size_t count) const
856  {
857  size_t remaining = it->remaining();
858  size_t cn = DataType<_Tp>::channels;
859  int _fmt = traits::SafeFmt<_Tp>::fmt;
860  CV_Assert((_fmt >> 8) < 9);
861  char fmt[] = { (char)((_fmt >> 8)+'1'), (char)_fmt, '\0' };
862  CV_Assert((remaining % cn) == 0);
863  size_t remaining1 = remaining / cn;
864  count = count > remaining1 ? remaining1 : count;
865  vec.resize(count);
866  it->readRaw(fmt, !vec.empty() ? (uchar*)&vec[0] : 0, count*sizeof(_Tp));
867  }
868  private:
869  FileNodeIterator* it;
870  };
871 
872 } // internal
873 
875 
876 template<typename _Tp> static inline
877 void write(FileStorage& fs, const _Tp& value)
878 {
879  write(fs, String(), value);
880 }
881 
882 template<> inline
883 void write( FileStorage& fs, const int& value )
884 {
885  writeScalar(fs, value);
886 }
887 
888 template<> inline
889 void write( FileStorage& fs, const float& value )
890 {
891  writeScalar(fs, value);
892 }
893 
894 template<> inline
895 void write( FileStorage& fs, const double& value )
896 {
897  writeScalar(fs, value);
898 }
899 
900 template<> inline
901 void write( FileStorage& fs, const String& value )
902 {
903  writeScalar(fs, value);
904 }
905 
906 template<typename _Tp> static inline
907 void write(FileStorage& fs, const Point_<_Tp>& pt )
908 {
909  write(fs, pt.x);
910  write(fs, pt.y);
911 }
912 
913 template<typename _Tp> static inline
914 void write(FileStorage& fs, const Point3_<_Tp>& pt )
915 {
916  write(fs, pt.x);
917  write(fs, pt.y);
918  write(fs, pt.z);
919 }
920 
921 template<typename _Tp> static inline
922 void write(FileStorage& fs, const Size_<_Tp>& sz )
923 {
924  write(fs, sz.width);
925  write(fs, sz.height);
926 }
927 
928 template<typename _Tp> static inline
929 void write(FileStorage& fs, const Complex<_Tp>& c )
930 {
931  write(fs, c.re);
932  write(fs, c.im);
933 }
934 
935 template<typename _Tp> static inline
936 void write(FileStorage& fs, const Rect_<_Tp>& r )
937 {
938  write(fs, r.x);
939  write(fs, r.y);
940  write(fs, r.width);
941  write(fs, r.height);
942 }
943 
944 template<typename _Tp, int cn> static inline
945 void write(FileStorage& fs, const Vec<_Tp, cn>& v )
946 {
947  for(int i = 0; i < cn; i++)
948  write(fs, v.val[i]);
949 }
950 
951 template<typename _Tp, int m, int n> static inline
952 void write(FileStorage& fs, const Matx<_Tp, m, n>& x )
953 {
954  write(fs, Mat(x)); // write as a Mat class
955 }
956 
957 template<typename _Tp> static inline
958 void write(FileStorage& fs, const Scalar_<_Tp>& s )
959 {
960  write(fs, s.val[0]);
961  write(fs, s.val[1]);
962  write(fs, s.val[2]);
963  write(fs, s.val[3]);
964 }
965 
966 static inline
967 void write(FileStorage& fs, const Range& r )
968 {
969  write(fs, r.start);
970  write(fs, r.end);
971 }
972 
973 template<typename _Tp> static inline
974 void write( FileStorage& fs, const std::vector<_Tp>& vec )
975 {
976  cv::internal::VecWriterProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> w(&fs);
977  w(vec);
978 }
979 
980 template<typename _Tp> static inline
981 void write(FileStorage& fs, const String& name, const Point_<_Tp>& pt )
982 {
983  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
984  write(fs, pt);
985 }
986 
987 template<typename _Tp> static inline
988 void write(FileStorage& fs, const String& name, const Point3_<_Tp>& pt )
989 {
990  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
991  write(fs, pt);
992 }
993 
994 template<typename _Tp> static inline
995 void write(FileStorage& fs, const String& name, const Size_<_Tp>& sz )
996 {
997  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
998  write(fs, sz);
999 }
1000 
1001 template<typename _Tp> static inline
1002 void write(FileStorage& fs, const String& name, const Complex<_Tp>& c )
1003 {
1004  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1005  write(fs, c);
1006 }
1007 
1008 template<typename _Tp> static inline
1009 void write(FileStorage& fs, const String& name, const Rect_<_Tp>& r )
1010 {
1011  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1012  write(fs, r);
1013 }
1014 
1015 template<typename _Tp, int cn> static inline
1016 void write(FileStorage& fs, const String& name, const Vec<_Tp, cn>& v )
1017 {
1018  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1019  write(fs, v);
1020 }
1021 
1022 template<typename _Tp, int m, int n> static inline
1023 void write(FileStorage& fs, const String& name, const Matx<_Tp, m, n>& x )
1024 {
1025  write(fs, name, Mat(x)); // write as a Mat class
1026 }
1027 
1028 template<typename _Tp> static inline
1029 void write(FileStorage& fs, const String& name, const Scalar_<_Tp>& s )
1030 {
1031  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1032  write(fs, s);
1033 }
1034 
1035 static inline
1036 void write(FileStorage& fs, const String& name, const Range& r )
1037 {
1038  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1039  write(fs, r);
1040 }
1041 
1042 static inline
1043 void write(FileStorage& fs, const String& name, const KeyPoint& kpt)
1044 {
1045  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1046  write(fs, kpt.pt.x);
1047  write(fs, kpt.pt.y);
1048  write(fs, kpt.size);
1049  write(fs, kpt.angle);
1050  write(fs, kpt.response);
1051  write(fs, kpt.octave);
1052  write(fs, kpt.class_id);
1053 }
1054 
1055 static inline
1056 void write(FileStorage& fs, const String& name, const DMatch& m)
1057 {
1058  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1059  write(fs, m.queryIdx);
1060  write(fs, m.trainIdx);
1061  write(fs, m.imgIdx);
1062  write(fs, m.distance);
1063 }
1064 
1066 static inline void write( FileStorage& fs, const String& name, const _Tp& val )
1067 {
1068  write(fs, name, static_cast<int>(val));
1069 }
1070 
1071 template<typename _Tp> static inline
1072 void write( FileStorage& fs, const String& name, const std::vector<_Tp>& vec )
1073 {
1074  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+(traits::SafeFmt<_Tp>::fmt != 0 ? FileNode::FLOW : 0));
1075  write(fs, vec);
1076 }
1077 
1078 template<typename _Tp> static inline
1079 void write( FileStorage& fs, const String& name, const std::vector< std::vector<_Tp> >& vec )
1080 {
1081  cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ);
1082  for(size_t i = 0; i < vec.size(); i++)
1083  {
1084  cv::internal::WriteStructContext ws_(fs, name, FileNode::SEQ+(traits::SafeFmt<_Tp>::fmt != 0 ? FileNode::FLOW : 0));
1085  write(fs, vec[i]);
1086  }
1087 }
1088 
1089 #ifdef CV__LEGACY_PERSISTENCE
1090 // This code is not needed anymore, but it is preserved here to keep source compatibility
1091 // Implementation is similar to templates instantiations
1092 static inline void write(FileStorage& fs, const KeyPoint& kpt) { write(fs, String(), kpt); }
1093 static inline void write(FileStorage& fs, const DMatch& m) { write(fs, String(), m); }
1094 static inline void write(FileStorage& fs, const std::vector<KeyPoint>& vec)
1095 {
1096  cv::internal::VecWriterProxy<KeyPoint, 0> w(&fs);
1097  w(vec);
1098 }
1099 static inline void write(FileStorage& fs, const std::vector<DMatch>& vec)
1100 {
1101  cv::internal::VecWriterProxy<DMatch, 0> w(&fs);
1102  w(vec);
1103 
1104 }
1105 #endif
1106 
1107 
1108 static inline
1109 void read(const FileNode& node, bool& value, bool default_value)
1110 {
1111  int temp;
1112  read(node, temp, (int)default_value);
1113  value = temp != 0;
1114 }
1115 
1116 static inline
1117 void read(const FileNode& node, uchar& value, uchar default_value)
1118 {
1119  int temp;
1120  read(node, temp, (int)default_value);
1122 }
1123 
1124 static inline
1125 void read(const FileNode& node, schar& value, schar default_value)
1126 {
1127  int temp;
1128  read(node, temp, (int)default_value);
1130 }
1131 
1132 static inline
1133 void read(const FileNode& node, ushort& value, ushort default_value)
1134 {
1135  int temp;
1136  read(node, temp, (int)default_value);
1138 }
1139 
1140 static inline
1141 void read(const FileNode& node, short& value, short default_value)
1142 {
1143  int temp;
1144  read(node, temp, (int)default_value);
1146 }
1147 
1148 template<typename _Tp> static inline
1149 void read( FileNodeIterator& it, std::vector<_Tp>& vec, size_t maxCount = (size_t)INT_MAX )
1150 {
1151  cv::internal::VecReaderProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> r(&it);
1152  r(vec, maxCount);
1153 }
1154 
1156 static inline void read(const FileNode& node, _Tp& value, const _Tp& default_value = static_cast<_Tp>(0))
1157 {
1158  int temp;
1159  read(node, temp, static_cast<int>(default_value));
1160  value = static_cast<_Tp>(temp);
1161 }
1162 
1163 template<typename _Tp> static inline
1164 void read( const FileNode& node, std::vector<_Tp>& vec, const std::vector<_Tp>& default_value = std::vector<_Tp>() )
1165 {
1166  if(node.empty())
1167  vec = default_value;
1168  else
1169  {
1170  FileNodeIterator it = node.begin();
1171  read( it, vec );
1172  }
1173 }
1174 
1175 static inline
1176 void read( const FileNode& node, std::vector<KeyPoint>& vec, const std::vector<KeyPoint>& default_value )
1177 {
1178  if(node.empty())
1179  vec = default_value;
1180  else
1181  read(node, vec);
1182 }
1183 
1184 static inline
1185 void read( const FileNode& node, std::vector<DMatch>& vec, const std::vector<DMatch>& default_value )
1186 {
1187  if(node.empty())
1188  vec = default_value;
1189  else
1190  read(node, vec);
1191 }
1192 
1195 template<typename _Tp> static inline
1197 {
1198  if( !fs.isOpened() )
1199  return fs;
1201  CV_Error( Error::StsError, "No element name has been given" );
1202  write( fs, fs.elname, value );
1203  if( fs.state & FileStorage::INSIDE_MAP )
1205  return fs;
1206 }
1207 
1210 static inline
1211 FileStorage& operator << (FileStorage& fs, const char* str)
1212 {
1213  return (fs << String(str));
1214 }
1215 
1218 static inline
1220 {
1221  return (fs << String(value));
1222 }
1223 
1226 template<typename _Tp> static inline
1228 {
1229  read( *it, value, _Tp());
1230  return ++it;
1231 }
1232 
1235 template<typename _Tp> static inline
1237 {
1238  cv::internal::VecReaderProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> r(&it);
1239  r(vec, (size_t)INT_MAX);
1240  return it;
1241 }
1242 
1245 template<typename _Tp> static inline
1246 void operator >> (const FileNode& n, _Tp& value)
1247 {
1248  read( n, value, _Tp());
1249 }
1250 
1253 template<typename _Tp> static inline
1255 {
1256  FileNodeIterator it = n.begin();
1257  it >> vec;
1258 }
1259 
1262 //It needs special handling because it contains two types of fields, int & float.
1263 static inline
1264 void operator >> (const FileNode& n, KeyPoint& kpt)
1265 {
1266  FileNodeIterator it = n.begin();
1267  it >> kpt.pt.x >> kpt.pt.y >> kpt.size >> kpt.angle >> kpt.response >> kpt.octave >> kpt.class_id;
1268 }
1269 
1270 #ifdef CV__LEGACY_PERSISTENCE
1271 static inline
1273 {
1274  read(n, vec);
1275 }
1276 static inline
1278 {
1279  read(n, vec);
1280 }
1281 #endif
1282 
1285 //It needs special handling because it contains two types of fields, int & float.
1286 static inline
1287 void operator >> (const FileNode& n, DMatch& m)
1288 {
1289  FileNodeIterator it = n.begin();
1290  it >> m.queryIdx >> m.trainIdx >> m.imgIdx >> m.distance;
1291 }
1292 
1295 
1296 static inline
1297 ptrdiff_t operator - (const FileNodeIterator& it1, const FileNodeIterator& it2)
1298 {
1299  return it2.remaining() - it1.remaining();
1300 }
1301 
1302 static inline
1303 bool operator < (const FileNodeIterator& it1, const FileNodeIterator& it2)
1304 {
1305  return it1.remaining() > it2.remaining();
1306 }
1307 
1308 } // cv
1309 
1310 #endif // OPENCV_CORE_PERSISTENCE_HPP
A complex number class.
Definition: types.hpp:80
_Tp im
the real and the imaginary parts
Definition: types.hpp:92
_Tp re
Definition: types.hpp:92
Class for matching keypoint descriptors.
Definition: types.hpp:842
CV_PROP_RW int queryIdx
query descriptor index
Definition: types.hpp:848
CV_PROP_RW float distance
Definition: types.hpp:852
CV_PROP_RW int imgIdx
train image index
Definition: types.hpp:850
CV_PROP_RW int trainIdx
train descriptor index
Definition: types.hpp:849
used to iterate through sequences and mappings.
Definition: persistence.hpp:634
FileNodeIterator(const FileNodeIterator &it)
size_t ofs
Definition: persistence.hpp:689
size_t remaining() const
returns the number of remaining (not read yet) elements
bool equalTo(const FileNodeIterator &it) const
size_t nodeNElems
Definition: persistence.hpp:691
FileNodeIterator()
The constructors.
size_t blockSize
Definition: persistence.hpp:690
FileStorage::Impl * fs
Definition: persistence.hpp:687
size_t blockIdx
Definition: persistence.hpp:688
size_t idx
Definition: persistence.hpp:692
FileNodeIterator(const FileNode &node, bool seekEnd)
FileNodeIterator & operator=(const FileNodeIterator &it)
FileNodeIterator & readRaw(const String &fmt, void *vec, size_t len=(size_t) INT_MAX)
Reads node elements to the buffer with the specified format.
File Storage Node class.
Definition: persistence.hpp:482
CV_WRAP int type() const
Returns type of the node.
CV_WRAP double real() const
Simplified reading API to use with bindings.
static bool isSeq(int flags)
static bool isMap(int flags)
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
CV_WRAP FileNode()
The constructors.
CV_WRAP std::vector< String > keys() const
Returns keys of a mapping node.
CV_WRAP bool empty() const
returns true if the node is empty
void setValue(int type, const void *value, int len=-1)
CV_WRAP bool isInt() const
returns true if the node is an integer
CV_WRAP std::string name() const
returns the node name or an empty string if the node is nameless
FileNode(const FileNode &node)
CV_WRAP bool isMap() const
returns true if the node is a mapping
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
static bool isEmptyCollection(int flags)
FileNode(FileStorage::Impl *fs, size_t blockIdx, size_t ofs)
CV_WRAP std::string string() const
Simplified reading API to use with bindings.
CV_WRAP_AS(getNode) FileNode operator[](const char *nodename) const
size_t blockIdx
Definition: persistence.hpp:623
FileNode & operator=(const FileNode &node)
size_t ofs
Definition: persistence.hpp:624
CV_WRAP bool isString() const
returns true if the node is a text string
@ 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 Mat mat() const
Simplified reading API to use with bindings.
void readRaw(const String &fmt, void *vec, size_t len) const
Reads node elements to the buffer with the specified format.
uchar * ptr()
CV_WRAP bool isNone() const
returns true if the node is a "none" object
static bool isCollection(int flags)
static bool isFlow(int flags)
CV_WRAP_AS(at) FileNode operator[](int i) const
CV_WRAP bool isReal() const
returns true if the node is a floating-point number
FileStorage::Impl * fs
Definition: persistence.hpp:622
FileNode(const FileStorage *fs, size_t blockIdx, size_t ofs)
CV_WRAP size_t rawSize() const
returns raw size of the FileNode in bytes
const uchar * ptr() const
CV_WRAP bool isSeq() const
returns true if the node is a sequence
CV_WRAP bool isNamed() const
returns true if the node has a name
CV_WRAP size_t size() const
returns the number of elements in the node, if it is a sequence or mapping, or 1 otherwise.
XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or readi...
Definition: persistence.hpp:304
CV_WRAP FileStorage()
The constructors.
CV_WRAP void write(const String &name, int val)
Simplified writing API to use with bindings.
virtual CV_WRAP void release()
Closes the file and releases all the memory buffers.
CV_WRAP void write(const String &name, const Mat &val)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void writeRaw(const String &fmt, const void *vec, size_t len)
Writes multiple numbers.
CV_WRAP void write(const String &name, const std::vector< String > &val)
This is an overloaded member function, provided for convenience. It differs from the above function o...
virtual ~FileStorage()
the destructor. calls release()
CV_WRAP void write(const String &name, double val)
This is an overloaded member function, provided for convenience. It differs from the above function o...
CV_WRAP FileNode root(int streamidx=0) const
Returns the top-level mapping.
CV_WRAP FileNode getFirstTopLevelNode() const
Returns the first element of the top-level mapping.
CV_WRAP void startWriteStruct(const String &name, int flags, const String &typeName=String())
Starts to write a nested structure (sequence or a mapping).
Mode
file storage mode
Definition: persistence.hpp:308
CV_WRAP void endWriteStruct()
Finishes writing nested structure (should pair startWriteStruct())
CV_WRAP_AS(getNode) FileNode operator[](const char *nodename) const
CV_WRAP void writeComment(const String &comment, bool append=false)
Writes a comment.
int state
Definition: persistence.hpp:462
State
Definition: persistence.hpp:324
@ INSIDE_MAP
Definition: persistence.hpp:328
@ NAME_EXPECTED
Definition: persistence.hpp:327
virtual CV_WRAP bool open(const String &filename, int flags, const String &encoding=String())
Opens a file.
CV_WRAP int getFormat() const
Returns the current format.
CV_WRAP void write(const String &name, const String &val)
This is an overloaded member function, provided for convenience. It differs from the above function o...
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.
CV_WRAP FileStorage(const String &filename, int flags, const String &encoding=String())
Opens a file.
FileNode operator[](const String &nodename) const
Returns the specified element of the top-level mapping.
std::string elname
Definition: persistence.hpp:463
Ptr< Impl > p
Definition: persistence.hpp:465
virtual CV_WRAP String releaseAndGetString()
Closes the file and releases all the memory buffers.
Data structure for salient point detectors.
Definition: types.hpp:745
CV_PROP_RW float angle
Definition: types.hpp:806
CV_PROP_RW Point2f pt
coordinates of the keypoints
Definition: types.hpp:804
CV_PROP_RW int octave
octave (pyramid layer) from which the keypoint has been extracted
Definition: types.hpp:810
CV_PROP_RW float response
the response by which the most strong keypoints have been selected. Can be used for the further sorti...
Definition: types.hpp:809
CV_PROP_RW int class_id
object class (if the keypoints need to be clustered by an object they belong to)
Definition: types.hpp:811
CV_PROP_RW float size
diameter of the meaningful keypoint neighborhood
Definition: types.hpp:805
n-dimensional dense array class
Definition: mat.hpp:812
Template class for small matrices whose type and size are known at compilation time.
Definition: matx.hpp:100
_Tp val[m *n]
matrix elements
Definition: matx.hpp:218
Template class for 3D points specified by its coordinates x, y and z.
Definition: types.hpp:255
Template class for 2D points specified by its coordinates x and y.
Definition: types.hpp:163
_Tp y
y coordinate of the point
Definition: types.hpp:202
_Tp x
x coordinate of the point
Definition: types.hpp:201
Template class specifying a continuous subsequence (slice) of a sequence.
Definition: types.hpp:623
int end
Definition: types.hpp:631
int start
Definition: types.hpp:631
Template class for 2D rectangles.
Definition: types.hpp:444
Template class for a 4-element vector derived from Vec.
Definition: types.hpp:670
Template class for specifying the size of an image or rectangle.
Definition: types.hpp:335
_Tp height
the height
Definition: types.hpp:363
_Tp width
the width
Definition: types.hpp:362
The class SparseMat represents multi-dimensional sparse numerical arrays.
Definition: mat.hpp:2735
Template class for short numerical vectors, a partial case of Matx.
Definition: matx.hpp:369
T empty(T... args)
InputArrayOfArrays Size InputOutputArray InputOutputArray OutputArrayOfArrays OutputArrayOfArrays OutputArray OutputArray OutputArray int flags
Definition: calib3d.hpp:1617
Point_< int > Point2i
Definition: types.hpp:205
std::string String
Definition: cvstd.hpp:151
static bool operator!=(const Matx< _Tp, m, n > &a, const Matx< _Tp, m, n > &b)
static bool operator==(const Matx< _Tp, m, n > &a, const Matx< _Tp, m, n > &b)
int CvScalar value
Definition: core_c.h:720
int int channels
Definition: core_c.h:100
CvArr const CvMat * mat
Definition: core_c.h:1308
int int type
Definition: core_c.h:221
int count
Definition: core_c.h:1413
const char const char ** filename
Definition: core_c.h:2630
const CvArr CvArr * x
Definition: core_c.h:1195
signed char schar
Definition: interface.h:48
unsigned char uchar
Definition: interface.h:51
unsigned short ushort
Definition: interface.h:52
CV_INLINE v_reg< _Tp, n > & operator+=(v_reg< _Tp, n > &a, const v_reg< _Tp, n > &b)
#define CV_EXPORTS_W_SIMPLE
Definition: cvdef.h:473
#define CV_EXPORTS
Definition: cvdef.h:435
uchar saturate_cast< uchar >(schar v)
Definition: saturate.hpp:101
#define CV_Error(code, msg)
Call the error handler.
Definition: base.hpp:320
#define CV_EXPORTS_W
Definition: cvdef.h:472
short saturate_cast< short >(ushort v)
Definition: saturate.hpp:130
ushort saturate_cast< ushort >(schar v)
Definition: saturate.hpp:121
schar saturate_cast< schar >(uchar v)
Definition: saturate.hpp:111
#define CV_WRAP
Definition: cvdef.h:481
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition: base.hpp:342
class CV_EXPORTS FileNodeIterator
Definition: persistence.hpp:298
std::ostream & operator<<(std::ostream &, const DualQuat< _Tp > &)
CvPoint2D32f pt[4]
Definition: imgproc_c.h:571
CvRect r
Definition: imgproc_c.h:984
CvArr CvArr * temp
Definition: imgproc_c.h:329
T min(T... args)
@ StsError
unknown /unspecified error
Definition: base.hpp:71
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441
CV_EXPORTS void read(const FileNode &node, int &value, int default_value)
CV_EXPORTS void writeScalar(FileStorage &fs, int value)
static bool operator<(const FileNodeIterator &it1, const FileNodeIterator &it2)
Definition: persistence.hpp:1303
DualQuat< T > operator*(const T a, const DualQuat< T > &q)
Definition: dualquaternion.inl.hpp:274
DualQuat< T > operator-(const DualQuat< T > &q, const T a)
Definition: dualquaternion.inl.hpp:255
CV_EXPORTS void write(FileStorage &fs, const String &name, int value)
static FileNodeIterator & operator>>(FileNodeIterator &it, _Tp &value)
Reads data from a file storage.
Definition: persistence.hpp:1227
T resize(T... args)
T size(T... args)
float x
Definition: types_c.h:978
float y
Definition: types_c.h:979
int width
Definition: types_c.h:835
int y
Definition: types_c.h:834
int x
Definition: types_c.h:833
int height
Definition: types_c.h:836
Definition: traits.hpp:402