OpenCV 4.5.3(日本語機械翻訳)
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;
298 class CV_EXPORTS FileNodeIterator;
299
303 class CV_EXPORTS_W FileStorage
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
336 CV_WRAP FileStorage();
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
381 CV_WRAP virtual String releaseAndGetString();
382
386 CV_WRAP FileNode getFirstTopLevelNode() const;
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
449 CV_WRAP void endWriteStruct();
450
455 static String getDefaultObjectName(const String& filename);
456
460 CV_WRAP int getFormat() const;
461
462 int state;
463 std::string elname;
464
465 class Impl;
466 Ptr<Impl> p;
467};
468
481 class CV_EXPORTS_W_SIMPLE FileNode
482{
483 public:
485 enum
486 {
487 NONE = 0,
488 INT = 1,
489 REAL = 2,
490 FLOAT = REAL,
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 };
508 CV_WRAP FileNode();
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
545 CV_WRAP std::vector<String> keys() const;
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;
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(); }
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;
615 CV_WRAP std::string string() 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
633 class CV_EXPORTS FileNodeIterator
634{
635 public:
642
651 FileNodeIterator(const FileNode& node, bool seekEnd);
652
657
658 FileNodeIterator& operator=(const FileNodeIterator& it);
659
661 FileNode operator *() const;
662
664 FileNodeIterator& operator ++ ();
666 FileNodeIterator operator ++ (int);
668 FileNodeIterator& operator += (int ofs);
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
701
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 );
707CV_EXPORTS void write( FileStorage& fs, const String& name, const SparseMat& 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);
711 #endif
712
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 );
717
719
722
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() );
728CV_EXPORTS void read(const FileNode& node, SparseMat& mat, const SparseMat& default_mat = SparseMat() );
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);
732 #endif
733CV_EXPORTS void read(const FileNode& node, KeyPoint& value, const KeyPoint& default_value);
734CV_EXPORTS void read(const FileNode& node, DMatch& value, const DMatch& default_value);
735
736 template<typename _Tp> static inline void read(const FileNode& node, Point_<_Tp>& value, const Point_<_Tp>& default_value)
737{
738 std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
739 value = temp.size() != 2 ? default_value : Point_<_Tp>(saturate_cast<_Tp>(temp[0]), saturate_cast<_Tp>(temp[1]));
740}
741
742 template<typename _Tp> static inline void read(const FileNode& node, Point3_<_Tp>& value, const Point3_<_Tp>& default_value)
743{
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]));
747}
748
749 template<typename _Tp> static inline void read(const FileNode& node, Size_<_Tp>& value, const Size_<_Tp>& default_value)
750{
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]));
753}
754
755 template<typename _Tp> static inline void read(const FileNode& node, Complex<_Tp>& value, const Complex<_Tp>& default_value)
756{
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]));
759}
760
761 template<typename _Tp> static inline void read(const FileNode& node, Rect_<_Tp>& value, const Rect_<_Tp>& default_value)
762{
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]));
766}
767
768 template<typename _Tp, int cn> static inline void read(const FileNode& node, Vec<_Tp, cn>& value, const Vec<_Tp, cn>& default_value)
769{
770 std::vector<_Tp> temp; FileNodeIterator it = node.begin(); it >> temp;
771 value = temp.size() != cn ? default_value : Vec<_Tp, cn>(&temp[0]);
772}
773
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>())
775{
776 Mat temp;
777 read(node, temp); // read as a Mat class
778
779 if (temp.empty())
780 value = default_matx;
781 else
782 value = Matx<_Tp, m, n>(temp);
783}
784
785 template<typename _Tp> static inline void read(const FileNode& node, Scalar_<_Tp>& value, const Scalar_<_Tp>& default_value)
786{
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]));
790}
791
792 static inline void read(const FileNode& node, Range& value, const Range& default_value)
793{
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;
797}
798
800
804 CV_EXPORTS FileStorage& operator << (FileStorage& fs, const String& str);
805
807
808 namespace internal
809{
810 class CV_EXPORTS WriteStructContext
811 {
812 public:
813 WriteStructContext(FileStorage& _fs, const String& name, int flags, const String& typeName = String());
814 ~WriteStructContext();
815 private:
816 FileStorage* fs;
817 };
818
819 template<typename _Tp, int numflag> class VecWriterProxy
820 {
821 public:
822 VecWriterProxy( FileStorage* _fs ) : fs(_fs) {}
823 void operator()(const std::vector<_Tp>& vec) const
824 {
825 size_t count = vec.size();
826 for (size_t i = 0; i < count; i++)
827 write(*fs, vec[i]);
828 }
829 private:
830 FileStorage* fs;
831 };
832
833 template<typename _Tp> class VecWriterProxy<_Tp, 1>
834 {
835 public:
836 VecWriterProxy( FileStorage* _fs ) : fs(_fs) {}
837 void operator()(const std::vector<_Tp>& vec) const
838 {
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));
842 }
843 private:
844 FileStorage* fs;
845 };
846
847 template<typename _Tp, int numflag> class VecReaderProxy
848 {
849 public:
850 VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
851 void operator()(std::vector<_Tp>& vec, size_t count) const
852 {
853 count = std::min(count, it->remaining());
854 vec.resize(count);
855 for (size_t i = 0; i < count; i++, ++(*it))
856 read(**it, vec[i], _Tp());
857 }
858 private:
859 FileNodeIterator* it;
860 };
861
862 template<typename _Tp> class VecReaderProxy<_Tp, 1>
863 {
864 public:
865 VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
866 void operator()(std::vector<_Tp>& vec, size_t count) const
867 {
868 size_t remaining = it->remaining();
869 size_t cn = DataType<_Tp>::channels;
870 int _fmt = traits::SafeFmt<_Tp>::fmt;
871 CV_Assert((_fmt >> 8) < 9);
872 char fmt[] = { (char)((_fmt >> 8)+'1'), (char)_fmt, '\0' };
873 CV_Assert((remaining % cn) == 0);
874 size_t remaining1 = remaining / cn;
875 count = count > remaining1 ? remaining1 : count;
876 vec.resize(count);
877 it->readRaw(fmt, !vec.empty() ? (uchar*)&vec[0] : 0, count*sizeof(_Tp));
878 }
879 private:
880 FileNodeIterator* it;
881 };
882
883} // internal
884
886
889
890 template<typename _Tp> static inline
891 void write(FileStorage& fs, const _Tp& value)
892{
893 write(fs, String(), value);
894}
895
896 template<> inline
897 void write( FileStorage& fs, const int& value )
898{
899 writeScalar(fs, value);
900}
901
902 template<> inline
903 void write( FileStorage& fs, const float& value )
904{
905 writeScalar(fs, value);
906}
907
908 template<> inline
909 void write( FileStorage& fs, const double& value )
910{
911 writeScalar(fs, value);
912}
913
914 template<> inline
915 void write( FileStorage& fs, const String& value )
916{
917 writeScalar(fs, value);
918}
919
920 template<typename _Tp> static inline
921 void write(FileStorage& fs, const Point_<_Tp>& pt )
922{
923 write(fs, pt.x);
924 write(fs, pt.y);
925}
926
927 template<typename _Tp> static inline
928 void write(FileStorage& fs, const Point3_<_Tp>& pt )
929{
930 write(fs, pt.x);
931 write(fs, pt.y);
932 write(fs, pt.z);
933}
934
935 template<typename _Tp> static inline
936 void write(FileStorage& fs, const Size_<_Tp>& sz )
937{
938 write(fs, sz.width);
939 write(fs, sz.height);
940}
941
942 template<typename _Tp> static inline
943 void write(FileStorage& fs, const Complex<_Tp>& c )
944{
945 write(fs, c.re);
946 write(fs, c.im);
947}
948
949 template<typename _Tp> static inline
950 void write(FileStorage& fs, const Rect_<_Tp>& r )
951{
952 write(fs, r.x);
953 write(fs, r.y);
954 write(fs, r.width);
955 write(fs, r.height);
956}
957
958 template<typename _Tp, int cn> static inline
959 void write(FileStorage& fs, const Vec<_Tp, cn>& v )
960{
961 for(int i = 0; i < cn; i++)
962 write(fs, v.val[i]);
963}
964
965 template<typename _Tp, int m, int n> static inline
966 void write(FileStorage& fs, const Matx<_Tp, m, n>& x )
967{
968 write(fs, Mat(x)); // write as a Mat class
969}
970
971 template<typename _Tp> static inline
972 void write(FileStorage& fs, const Scalar_<_Tp>& s )
973{
974 write(fs, s.val[0]);
975 write(fs, s.val[1]);
976 write(fs, s.val[2]);
977 write(fs, s.val[3]);
978}
979
980 static inline
981 void write(FileStorage& fs, const Range& r )
982{
983 write(fs, r.start);
984 write(fs, r.end);
985}
986
987 template<typename _Tp> static inline
988 void write( FileStorage& fs, const std::vector<_Tp>& vec )
989{
990 cv::internal::VecWriterProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> w(&fs);
991 w(vec);
992}
993
994 template<typename _Tp> static inline
995 void write(FileStorage& fs, const String& name, const Point_<_Tp>& pt )
996{
997 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
998 write(fs, pt);
999}
1000
1001 template<typename _Tp> static inline
1002 void write(FileStorage& fs, const String& name, const Point3_<_Tp>& pt )
1003{
1004 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1005 write(fs, pt);
1006}
1007
1008 template<typename _Tp> static inline
1009 void write(FileStorage& fs, const String& name, const Size_<_Tp>& sz )
1010{
1011 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1012 write(fs, sz);
1013}
1014
1015 template<typename _Tp> static inline
1016 void write(FileStorage& fs, const String& name, const Complex<_Tp>& c )
1017{
1018 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1019 write(fs, c);
1020}
1021
1022 template<typename _Tp> static inline
1023 void write(FileStorage& fs, const String& name, const Rect_<_Tp>& r )
1024{
1025 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1026 write(fs, r);
1027}
1028
1029 template<typename _Tp, int cn> static inline
1030 void write(FileStorage& fs, const String& name, const Vec<_Tp, cn>& v )
1031{
1032 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1033 write(fs, v);
1034}
1035
1036 template<typename _Tp, int m, int n> static inline
1037 void write(FileStorage& fs, const String& name, const Matx<_Tp, m, n>& x )
1038{
1039 write(fs, name, Mat(x)); // write as a Mat class
1040}
1041
1042 template<typename _Tp> static inline
1043 void write(FileStorage& fs, const String& name, const Scalar_<_Tp>& s )
1044{
1045 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1046 write(fs, s);
1047}
1048
1049 static inline
1050 void write(FileStorage& fs, const String& name, const Range& r )
1051{
1052 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1053 write(fs, r);
1054}
1055
1056 static inline
1057 void write(FileStorage& fs, const String& name, const KeyPoint& kpt)
1058{
1059 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
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);
1067}
1068
1069 static inline
1070 void write(FileStorage& fs, const String& name, const DMatch& m)
1071{
1072 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1073 write(fs, m.queryIdx);
1074 write(fs, m.trainIdx);
1075 write(fs, m.imgIdx);
1076 write(fs, m.distance);
1077}
1078
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 )
1081{
1082 write(fs, name, static_cast< int >(val));
1083}
1084
1085 template<typename _Tp> static inline
1086 void write( FileStorage& fs, const String& name, const std::vector<_Tp>& vec )
1087{
1088 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+(traits::SafeFmt<_Tp>::fmt != 0 ? FileNode::FLOW : 0));
1089 write(fs, vec);
1090}
1091
1092 template<typename _Tp> static inline
1093 void write( FileStorage& fs, const String& name, const std::vector< std::vector<_Tp> >& vec )
1094{
1095 cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ);
1096 for(size_t i = 0; i < vec.size(); i++)
1097 {
1098 cv::internal::WriteStructContext ws_(fs, name, FileNode::SEQ+(traits::SafeFmt<_Tp>::fmt != 0 ? FileNode::FLOW : 0));
1099 write(fs, vec[i]);
1100 }
1101}
1102
1103 #ifdef CV__LEGACY_PERSISTENCE
1104 // This code is not needed anymore, but it is preserved here to keep source compatibility
1105 // Implementation is similar to templates instantiations
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)
1109{
1110 cv::internal::VecWriterProxy<KeyPoint, 0> w(&fs);
1111 w(vec);
1112}
1113 static inline void write(FileStorage& fs, const std::vector<DMatch>& vec)
1114{
1115 cv::internal::VecWriterProxy<DMatch, 0> w(&fs);
1116 w(vec);
1117
1118}
1119 #endif
1120
1122
1125
1126 static inline
1127 void read(const FileNode& node, bool& value, bool default_value)
1128{
1129 int temp;
1130 read(node, temp, (int)default_value);
1131 value = temp != 0;
1132}
1133
1134 static inline
1135 void read(const FileNode& node, uchar& value, uchar default_value)
1136{
1137 int temp;
1138 read(node, temp, (int)default_value);
1139 value = saturate_cast<uchar>(temp);
1140}
1141
1142 static inline
1143 void read(const FileNode& node, schar& value, schar default_value)
1144{
1145 int temp;
1146 read(node, temp, (int)default_value);
1147 value = saturate_cast<schar>(temp);
1148}
1149
1150 static inline
1151 void read(const FileNode& node, ushort& value, ushort default_value)
1152{
1153 int temp;
1154 read(node, temp, (int)default_value);
1155 value = saturate_cast<ushort>(temp);
1156}
1157
1158 static inline
1159 void read(const FileNode& node, short& value, short default_value)
1160{
1161 int temp;
1162 read(node, temp, (int)default_value);
1163 value = saturate_cast<short>(temp);
1164}
1165
1166 template<typename _Tp> static inline
1167 void read( FileNodeIterator& it, std::vector<_Tp>& vec, size_t maxCount = (size_t)INT_MAX )
1168{
1169 cv::internal::VecReaderProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> r(&it);
1170 r(vec, maxCount);
1171}
1172
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))
1175{
1176 int temp;
1177 read(node, temp, static_cast< int >(default_value));
1178 value = static_cast<_Tp>(temp);
1179}
1180
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>() )
1183{
1184 if(node.empty())
1185 vec = default_value;
1186 else
1187 {
1188 FileNodeIterator it = node.begin();
1189 read( it, vec );
1190 }
1191}
1192
1193 static inline
1194 void read( const FileNode& node, std::vector<KeyPoint>& vec, const std::vector<KeyPoint>& default_value )
1195{
1196 if(node.empty())
1197 vec = default_value;
1198 else
1199 read(node, vec);
1200}
1201
1202 static inline
1203 void read( const FileNode& node, std::vector<DMatch>& vec, const std::vector<DMatch>& default_value )
1204{
1205 if(node.empty())
1206 vec = default_value;
1207 else
1208 read(node, vec);
1209}
1210
1212
1215
1218 template<typename _Tp> static inline
1219 FileStorage& operator << (FileStorage& fs, const _Tp& value)
1220{
1221 if( !fs.isOpened() )
1222 return fs;
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;
1228 return fs;
1229}
1230
1233 static inline
1234 FileStorage& operator << (FileStorage& fs, const char* str)
1235{
1236 return (fs << String(str));
1237}
1238
1241 static inline
1242 FileStorage& operator << (FileStorage& fs, char* value)
1243{
1244 return (fs << String(value));
1245}
1246
1248
1251
1254 template<typename _Tp> static inline
1255 FileNodeIterator& operator >> (FileNodeIterator& it, _Tp& value)
1256{
1257 read( *it, value, _Tp());
1258 return ++it;
1259}
1260
1263 template<typename _Tp> static inline
1264 FileNodeIterator& operator >> (FileNodeIterator& it, std::vector<_Tp>& vec)
1265{
1266 cv::internal::VecReaderProxy<_Tp, traits::SafeFmt<_Tp>::fmt != 0> r(&it);
1267 r(vec, (size_t)INT_MAX);
1268 return it;
1269}
1270
1272
1275
1278 template<typename _Tp> static inline
1279 void operator >> (const FileNode& n, _Tp& value)
1280{
1281 read( n, value, _Tp());
1282}
1283
1286 template<typename _Tp> static inline
1287 void operator >> (const FileNode& n, std::vector<_Tp>& vec)
1288{
1289 FileNodeIterator it = n.begin();
1290 it >> vec;
1291}
1292
1295 //It needs special handling because it contains two types of fields, int & float.
1296 static inline
1297 void operator >> (const FileNode& n, KeyPoint& kpt)
1298{
1299 FileNodeIterator it = n.begin();
1300 it >> kpt.pt.x >> kpt.pt.y >> kpt.size >> kpt.angle >> kpt.response >> kpt.octave >> kpt.class_id;
1301}
1302
1303 #ifdef CV__LEGACY_PERSISTENCE
1304 static inline
1305 void operator >> (const FileNode& n, std::vector<KeyPoint>& vec)
1306{
1307 read(n, vec);
1308}
1309 static inline
1310 void operator >> (const FileNode& n, std::vector<DMatch>& vec)
1311{
1312 read(n, vec);
1313}
1314 #endif
1315
1318 //It needs special handling because it contains two types of fields, int & float.
1319 static inline
1320 void operator >> (const FileNode& n, DMatch& m)
1321{
1322 FileNodeIterator it = n.begin();
1323 it >> m.queryIdx >> m.trainIdx >> m.imgIdx >> m.distance;
1324}
1325
1327
1330
1331CV_EXPORTS bool operator == (const FileNodeIterator& it1, const FileNodeIterator& it2);
1332CV_EXPORTS bool operator != (const FileNodeIterator& it1, const FileNodeIterator& it2);
1333
1334 static inline
1335ptrdiff_t operator - (const FileNodeIterator& it1, const FileNodeIterator& it2)
1336{
1337 return it2.remaining() - it1.remaining();
1338}
1339
1340 static inline
1341 bool operator < (const FileNodeIterator& it1, const FileNodeIterator& it2)
1342{
1343 return it1.remaining() > it2.remaining();
1344}
1345
1347
1348} // cv
1349
1350 #endif // OPENCV_CORE_PERSISTENCE_HPP
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
cv
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75
Definition: cvstd_wrapper.hpp:74