Skip to content

Commit 0451629

Browse files
committed
core(persistence): resolve DMatch/KeyPoint problem
1 parent 86b55b3 commit 0451629

File tree

3 files changed

+331
-82
lines changed

3 files changed

+331
-82
lines changed

modules/core/include/opencv2/core/persistence.hpp

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
#ifndef OPENCV_CORE_PERSISTENCE_HPP
4545
#define OPENCV_CORE_PERSISTENCE_HPP
4646

47+
#ifndef CV_DOXYGEN
48+
/// Define to support persistence legacy formats
49+
#define CV__LEGACY_PERSISTENCE
50+
#endif
51+
4752
#ifndef __cplusplus
4853
# error persistence.hpp header must be compiled as C++
4954
#endif
@@ -700,8 +705,10 @@ CV_EXPORTS void write( FileStorage& fs, const String& name, double value );
700705
CV_EXPORTS void write( FileStorage& fs, const String& name, const String& value );
701706
CV_EXPORTS void write( FileStorage& fs, const String& name, const Mat& value );
702707
CV_EXPORTS void write( FileStorage& fs, const String& name, const SparseMat& value );
708+
#ifdef CV__LEGACY_PERSISTENCE
703709
CV_EXPORTS void write( FileStorage& fs, const String& name, const std::vector<KeyPoint>& value);
704710
CV_EXPORTS void write( FileStorage& fs, const String& name, const std::vector<DMatch>& value);
711+
#endif
705712

706713
CV_EXPORTS void writeScalar( FileStorage& fs, int value );
707714
CV_EXPORTS void writeScalar( FileStorage& fs, float value );
@@ -720,8 +727,12 @@ CV_EXPORTS void read(const FileNode& node, String& value, const String& default_
720727
CV_EXPORTS void read(const FileNode& node, std::string& value, const std::string& default_value);
721728
CV_EXPORTS void read(const FileNode& node, Mat& mat, const Mat& default_mat = Mat() );
722729
CV_EXPORTS void read(const FileNode& node, SparseMat& mat, const SparseMat& default_mat = SparseMat() );
730+
#ifdef CV__LEGACY_PERSISTENCE
723731
CV_EXPORTS void read(const FileNode& node, std::vector<KeyPoint>& keypoints);
724732
CV_EXPORTS void read(const FileNode& node, std::vector<DMatch>& matches);
733+
#endif
734+
CV_EXPORTS void read(const FileNode& node, KeyPoint& value, const KeyPoint& default_value);
735+
CV_EXPORTS void read(const FileNode& node, DMatch& value, const DMatch& default_value);
725736

726737
template<typename _Tp> static inline void read(const FileNode& node, Point_<_Tp>& value, const Point_<_Tp>& default_value)
727738
{
@@ -948,54 +959,13 @@ void write(FileStorage& fs, const Scalar_<_Tp>& s )
948959
write(fs, s.val[3]);
949960
}
950961

951-
static inline
952-
void write(FileStorage& fs, const KeyPoint& kpt )
953-
{
954-
write(fs, kpt.pt.x);
955-
write(fs, kpt.pt.y);
956-
write(fs, kpt.size);
957-
write(fs, kpt.angle);
958-
write(fs, kpt.response);
959-
write(fs, kpt.octave);
960-
write(fs, kpt.class_id);
961-
}
962-
963-
static inline
964-
void write(FileStorage& fs, const DMatch& m )
965-
{
966-
write(fs, m.queryIdx);
967-
write(fs, m.trainIdx);
968-
write(fs, m.imgIdx);
969-
write(fs, m.distance);
970-
}
971-
972962
static inline
973963
void write(FileStorage& fs, const Range& r )
974964
{
975965
write(fs, r.start);
976966
write(fs, r.end);
977967
}
978968

979-
static inline
980-
void write( FileStorage& fs, const std::vector<KeyPoint>& vec )
981-
{
982-
size_t npoints = vec.size();
983-
for(size_t i = 0; i < npoints; i++ )
984-
{
985-
write(fs, vec[i]);
986-
}
987-
}
988-
989-
static inline
990-
void write( FileStorage& fs, const std::vector<DMatch>& vec )
991-
{
992-
size_t npoints = vec.size();
993-
for(size_t i = 0; i < npoints; i++ )
994-
{
995-
write(fs, vec[i]);
996-
}
997-
}
998-
999969
template<typename _Tp> static inline
1000970
void write( FileStorage& fs, const std::vector<_Tp>& vec )
1001971
{
@@ -1060,17 +1030,26 @@ void write(FileStorage& fs, const String& name, const Range& r )
10601030
}
10611031

10621032
static inline
1063-
void write(FileStorage& fs, const String& name, const KeyPoint& r )
1033+
void write(FileStorage& fs, const String& name, const KeyPoint& kpt)
10641034
{
10651035
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1066-
write(fs, r);
1036+
write(fs, kpt.pt.x);
1037+
write(fs, kpt.pt.y);
1038+
write(fs, kpt.size);
1039+
write(fs, kpt.angle);
1040+
write(fs, kpt.response);
1041+
write(fs, kpt.octave);
1042+
write(fs, kpt.class_id);
10671043
}
10681044

10691045
static inline
1070-
void write(FileStorage& fs, const String& name, const DMatch& r )
1046+
void write(FileStorage& fs, const String& name, const DMatch& m)
10711047
{
10721048
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1073-
write(fs, r);
1049+
write(fs, m.queryIdx);
1050+
write(fs, m.trainIdx);
1051+
write(fs, m.imgIdx);
1052+
write(fs, m.distance);
10741053
}
10751054

10761055
template<typename _Tp> static inline
@@ -1091,6 +1070,24 @@ void write( FileStorage& fs, const String& name, const std::vector< std::vector<
10911070
}
10921071
}
10931072

1073+
#ifdef CV__LEGACY_PERSISTENCE
1074+
// This code is not needed anymore, but it is preserved here to keep source compatibility
1075+
// Implementation is similar to templates instantiations
1076+
static inline void write(FileStorage& fs, const KeyPoint& kpt) { write(fs, String(), kpt); }
1077+
static inline void write(FileStorage& fs, const DMatch& m) { write(fs, String(), m); }
1078+
static inline void write(FileStorage& fs, const std::vector<KeyPoint>& vec)
1079+
{
1080+
cv::internal::VecWriterProxy<KeyPoint, 0> w(&fs);
1081+
w(vec);
1082+
}
1083+
static inline void write(FileStorage& fs, const std::vector<DMatch>& vec)
1084+
{
1085+
cv::internal::VecWriterProxy<DMatch, 0> w(&fs);
1086+
w(vec);
1087+
1088+
}
1089+
#endif
1090+
10941091
//! @} FileStorage
10951092

10961093
//! @relates cv::FileNode
@@ -1258,28 +1255,29 @@ void operator >> (const FileNode& n, std::vector<_Tp>& vec)
12581255
/** @brief Reads KeyPoint from a file storage.
12591256
*/
12601257
//It needs special handling because it contains two types of fields, int & float.
1261-
static inline
1262-
void operator >> (const FileNode& n, std::vector<KeyPoint>& vec)
1263-
{
1264-
read(n, vec);
1265-
}
1266-
12671258
static inline
12681259
void operator >> (const FileNode& n, KeyPoint& kpt)
12691260
{
12701261
FileNodeIterator it = n.begin();
12711262
it >> kpt.pt.x >> kpt.pt.y >> kpt.size >> kpt.angle >> kpt.response >> kpt.octave >> kpt.class_id;
12721263
}
12731264

1274-
/** @brief Reads DMatch from a file storage.
1275-
*/
1276-
//It needs special handling because it contains two types of fields, int & float.
1265+
#ifdef CV__LEGACY_PERSISTENCE
1266+
static inline
1267+
void operator >> (const FileNode& n, std::vector<KeyPoint>& vec)
1268+
{
1269+
read(n, vec);
1270+
}
12771271
static inline
12781272
void operator >> (const FileNode& n, std::vector<DMatch>& vec)
12791273
{
12801274
read(n, vec);
12811275
}
1276+
#endif
12821277

1278+
/** @brief Reads DMatch from a file storage.
1279+
*/
1280+
//It needs special handling because it contains two types of fields, int & float.
12831281
static inline
12841282
void operator >> (const FileNode& n, DMatch& m)
12851283
{

modules/core/src/persistence.cpp

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7333,21 +7333,45 @@ void read( const FileNode& node, SparseMat& mat, const SparseMat& default_mat )
73337333
m->copyToSparseMat(mat);
73347334
}
73357335

7336-
void write(FileStorage& fs, const String& objname, const std::vector<KeyPoint>& keypoints)
7336+
CV_EXPORTS void read(const FileNode& node, KeyPoint& value, const KeyPoint& default_value)
73377337
{
7338-
cv::internal::WriteStructContext ws(fs, objname, CV_NODE_SEQ + CV_NODE_FLOW);
7338+
if( node.empty() )
7339+
{
7340+
value = default_value;
7341+
return;
7342+
}
7343+
node >> value;
7344+
}
73397345

7340-
int i, npoints = (int)keypoints.size();
7341-
for( i = 0; i < npoints; i++ )
7346+
CV_EXPORTS void read(const FileNode& node, DMatch& value, const DMatch& default_value)
7347+
{
7348+
if( node.empty() )
73427349
{
7343-
write(fs, keypoints[i]);
7350+
value = default_value;
7351+
return;
73447352
}
7353+
node >> value;
73457354
}
73467355

7356+
#ifdef CV__LEGACY_PERSISTENCE
7357+
void write( FileStorage& fs, const String& name, const std::vector<KeyPoint>& vec)
7358+
{
7359+
// from template implementation
7360+
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ);
7361+
write(fs, vec);
7362+
}
73477363

73487364
void read(const FileNode& node, std::vector<KeyPoint>& keypoints)
73497365
{
7350-
keypoints.resize(0);
7366+
FileNode first_node = *(node.begin());
7367+
if (first_node.isSeq())
7368+
{
7369+
// modern scheme
7370+
FileNodeIterator it = node.begin();
7371+
it >> keypoints;
7372+
return;
7373+
}
7374+
keypoints.clear();
73517375
FileNodeIterator it = node.begin(), it_end = node.end();
73527376
for( ; it != it_end; )
73537377
{
@@ -7357,21 +7381,24 @@ void read(const FileNode& node, std::vector<KeyPoint>& keypoints)
73577381
}
73587382
}
73597383

7360-
7361-
void write(FileStorage& fs, const String& objname, const std::vector<DMatch>& matches)
7384+
void write( FileStorage& fs, const String& name, const std::vector<DMatch>& vec)
73627385
{
7363-
cv::internal::WriteStructContext ws(fs, objname, CV_NODE_SEQ + CV_NODE_FLOW);
7364-
7365-
int i, n = (int)matches.size();
7366-
for( i = 0; i < n; i++ )
7367-
{
7368-
write(fs, matches[i]);
7369-
}
7386+
// from template implementation
7387+
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ);
7388+
write(fs, vec);
73707389
}
73717390

73727391
void read(const FileNode& node, std::vector<DMatch>& matches)
73737392
{
7374-
matches.resize(0);
7393+
FileNode first_node = *(node.begin());
7394+
if (first_node.isSeq())
7395+
{
7396+
// modern scheme
7397+
FileNodeIterator it = node.begin();
7398+
it >> matches;
7399+
return;
7400+
}
7401+
matches.clear();
73757402
FileNodeIterator it = node.begin(), it_end = node.end();
73767403
for( ; it != it_end; )
73777404
{
@@ -7380,7 +7407,7 @@ void read(const FileNode& node, std::vector<DMatch>& matches)
73807407
matches.push_back(m);
73817408
}
73827409
}
7383-
7410+
#endif
73847411

73857412
int FileNode::type() const { return !node ? NONE : (node->tag & TYPE_MASK); }
73867413
bool FileNode::isNamed() const { return !node ? false : (node->tag & NAMED) != 0; }

0 commit comments

Comments
 (0)