1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

core(persistence): resolve DMatch/KeyPoint problem

This commit is contained in:
Alexander Alekhin
2017-08-25 18:55:31 +03:00
parent 86b55b3923
commit 0451629e22
3 changed files with 332 additions and 83 deletions
@@ -44,6 +44,11 @@
#ifndef OPENCV_CORE_PERSISTENCE_HPP
#define OPENCV_CORE_PERSISTENCE_HPP
#ifndef CV_DOXYGEN
/// Define to support persistence legacy formats
#define CV__LEGACY_PERSISTENCE
#endif
#ifndef __cplusplus
# error persistence.hpp header must be compiled as C++
#endif
@@ -700,8 +705,10 @@ CV_EXPORTS void write( FileStorage& fs, const String& name, double value );
CV_EXPORTS void write( FileStorage& fs, const String& name, const String& value );
CV_EXPORTS void write( FileStorage& fs, const String& name, const Mat& value );
CV_EXPORTS void write( FileStorage& fs, const String& name, const SparseMat& value );
#ifdef CV__LEGACY_PERSISTENCE
CV_EXPORTS void write( FileStorage& fs, const String& name, const std::vector<KeyPoint>& value);
CV_EXPORTS void write( FileStorage& fs, const String& name, const std::vector<DMatch>& value);
#endif
CV_EXPORTS void writeScalar( FileStorage& fs, int value );
CV_EXPORTS void writeScalar( FileStorage& fs, float value );
@@ -720,8 +727,12 @@ CV_EXPORTS void read(const FileNode& node, String& value, const String& default_
CV_EXPORTS void read(const FileNode& node, std::string& value, const std::string& default_value);
CV_EXPORTS void read(const FileNode& node, Mat& mat, const Mat& default_mat = Mat() );
CV_EXPORTS void read(const FileNode& node, SparseMat& mat, const SparseMat& default_mat = SparseMat() );
#ifdef CV__LEGACY_PERSISTENCE
CV_EXPORTS void read(const FileNode& node, std::vector<KeyPoint>& keypoints);
CV_EXPORTS void read(const FileNode& node, std::vector<DMatch>& matches);
#endif
CV_EXPORTS void read(const FileNode& node, KeyPoint& value, const KeyPoint& default_value);
CV_EXPORTS void read(const FileNode& node, DMatch& value, const DMatch& default_value);
template<typename _Tp> static inline void read(const FileNode& node, Point_<_Tp>& value, const Point_<_Tp>& default_value)
{
@@ -948,27 +959,6 @@ void write(FileStorage& fs, const Scalar_<_Tp>& s )
write(fs, s.val[3]);
}
static inline
void write(FileStorage& fs, const KeyPoint& kpt )
{
write(fs, kpt.pt.x);
write(fs, kpt.pt.y);
write(fs, kpt.size);
write(fs, kpt.angle);
write(fs, kpt.response);
write(fs, kpt.octave);
write(fs, kpt.class_id);
}
static inline
void write(FileStorage& fs, const DMatch& m )
{
write(fs, m.queryIdx);
write(fs, m.trainIdx);
write(fs, m.imgIdx);
write(fs, m.distance);
}
static inline
void write(FileStorage& fs, const Range& r )
{
@@ -976,26 +966,6 @@ void write(FileStorage& fs, const Range& r )
write(fs, r.end);
}
static inline
void write( FileStorage& fs, const std::vector<KeyPoint>& vec )
{
size_t npoints = vec.size();
for(size_t i = 0; i < npoints; i++ )
{
write(fs, vec[i]);
}
}
static inline
void write( FileStorage& fs, const std::vector<DMatch>& vec )
{
size_t npoints = vec.size();
for(size_t i = 0; i < npoints; i++ )
{
write(fs, vec[i]);
}
}
template<typename _Tp> static inline
void write( FileStorage& fs, const std::vector<_Tp>& vec )
{
@@ -1060,17 +1030,26 @@ void write(FileStorage& fs, const String& name, const Range& r )
}
static inline
void write(FileStorage& fs, const String& name, const KeyPoint& r )
void write(FileStorage& fs, const String& name, const KeyPoint& kpt)
{
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
write(fs, r);
write(fs, kpt.pt.x);
write(fs, kpt.pt.y);
write(fs, kpt.size);
write(fs, kpt.angle);
write(fs, kpt.response);
write(fs, kpt.octave);
write(fs, kpt.class_id);
}
static inline
void write(FileStorage& fs, const String& name, const DMatch& r )
void write(FileStorage& fs, const String& name, const DMatch& m)
{
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
write(fs, r);
write(fs, m.queryIdx);
write(fs, m.trainIdx);
write(fs, m.imgIdx);
write(fs, m.distance);
}
template<typename _Tp> static inline
@@ -1091,6 +1070,24 @@ void write( FileStorage& fs, const String& name, const std::vector< std::vector<
}
}
#ifdef CV__LEGACY_PERSISTENCE
// This code is not needed anymore, but it is preserved here to keep source compatibility
// Implementation is similar to templates instantiations
static inline void write(FileStorage& fs, const KeyPoint& kpt) { write(fs, String(), kpt); }
static inline void write(FileStorage& fs, const DMatch& m) { write(fs, String(), m); }
static inline void write(FileStorage& fs, const std::vector<KeyPoint>& vec)
{
cv::internal::VecWriterProxy<KeyPoint, 0> w(&fs);
w(vec);
}
static inline void write(FileStorage& fs, const std::vector<DMatch>& vec)
{
cv::internal::VecWriterProxy<DMatch, 0> w(&fs);
w(vec);
}
#endif
//! @} FileStorage
//! @relates cv::FileNode
@@ -1258,12 +1255,6 @@ void operator >> (const FileNode& n, std::vector<_Tp>& vec)
/** @brief Reads KeyPoint from a file storage.
*/
//It needs special handling because it contains two types of fields, int & float.
static inline
void operator >> (const FileNode& n, std::vector<KeyPoint>& vec)
{
read(n, vec);
}
static inline
void operator >> (const FileNode& n, KeyPoint& kpt)
{
@@ -1271,15 +1262,22 @@ void operator >> (const FileNode& n, KeyPoint& kpt)
it >> kpt.pt.x >> kpt.pt.y >> kpt.size >> kpt.angle >> kpt.response >> kpt.octave >> kpt.class_id;
}
/** @brief Reads DMatch from a file storage.
*/
//It needs special handling because it contains two types of fields, int & float.
#ifdef CV__LEGACY_PERSISTENCE
static inline
void operator >> (const FileNode& n, std::vector<KeyPoint>& vec)
{
read(n, vec);
}
static inline
void operator >> (const FileNode& n, std::vector<DMatch>& vec)
{
read(n, vec);
}
#endif
/** @brief Reads DMatch from a file storage.
*/
//It needs special handling because it contains two types of fields, int & float.
static inline
void operator >> (const FileNode& n, DMatch& m)
{