Skip to content

Commit ddfe688

Browse files
committed
Merge pull request opencv#8299 from sovrasov:fs_fix_kpts_dmatch_output
2 parents bc7f6fc + c321d02 commit ddfe688

File tree

3 files changed

+132
-13
lines changed

3 files changed

+132
-13
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,13 +944,54 @@ void write(FileStorage& fs, const Scalar_<_Tp>& s )
944944
write(fs, s.val[3]);
945945
}
946946

947+
static inline
948+
void write(FileStorage& fs, const KeyPoint& kpt )
949+
{
950+
write(fs, kpt.pt.x);
951+
write(fs, kpt.pt.y);
952+
write(fs, kpt.size);
953+
write(fs, kpt.angle);
954+
write(fs, kpt.response);
955+
write(fs, kpt.octave);
956+
write(fs, kpt.class_id);
957+
}
958+
959+
static inline
960+
void write(FileStorage& fs, const DMatch& m )
961+
{
962+
write(fs, m.queryIdx);
963+
write(fs, m.trainIdx);
964+
write(fs, m.imgIdx);
965+
write(fs, m.distance);
966+
}
967+
947968
static inline
948969
void write(FileStorage& fs, const Range& r )
949970
{
950971
write(fs, r.start);
951972
write(fs, r.end);
952973
}
953974

975+
static inline
976+
void write( FileStorage& fs, const std::vector<KeyPoint>& vec )
977+
{
978+
size_t npoints = vec.size();
979+
for(size_t i = 0; i < npoints; i++ )
980+
{
981+
write(fs, vec[i]);
982+
}
983+
}
984+
985+
static inline
986+
void write( FileStorage& fs, const std::vector<DMatch>& vec )
987+
{
988+
size_t npoints = vec.size();
989+
for(size_t i = 0; i < npoints; i++ )
990+
{
991+
write(fs, vec[i]);
992+
}
993+
}
994+
954995
template<typename _Tp> static inline
955996
void write( FileStorage& fs, const std::vector<_Tp>& vec )
956997
{
@@ -1096,6 +1137,24 @@ void read( const FileNode& node, std::vector<_Tp>& vec, const std::vector<_Tp>&
10961137
}
10971138
}
10981139

1140+
static inline
1141+
void read( const FileNode& node, std::vector<KeyPoint>& vec, const std::vector<KeyPoint>& default_value )
1142+
{
1143+
if(!node.node)
1144+
vec = default_value;
1145+
else
1146+
read(node, vec);
1147+
}
1148+
1149+
static inline
1150+
void read( const FileNode& node, std::vector<DMatch>& vec, const std::vector<DMatch>& default_value )
1151+
{
1152+
if(!node.node)
1153+
vec = default_value;
1154+
else
1155+
read(node, vec);
1156+
}
1157+
10991158
//! @} FileNode
11001159

11011160
//! @relates cv::FileStorage

modules/core/src/persistence.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7283,14 +7283,7 @@ void write(FileStorage& fs, const String& objname, const std::vector<KeyPoint>&
72837283
int i, npoints = (int)keypoints.size();
72847284
for( i = 0; i < npoints; i++ )
72857285
{
7286-
const KeyPoint& kpt = keypoints[i];
7287-
cv::write(fs, kpt.pt.x);
7288-
cv::write(fs, kpt.pt.y);
7289-
cv::write(fs, kpt.size);
7290-
cv::write(fs, kpt.angle);
7291-
cv::write(fs, kpt.response);
7292-
cv::write(fs, kpt.octave);
7293-
cv::write(fs, kpt.class_id);
7286+
write(fs, keypoints[i]);
72947287
}
72957288
}
72967289

@@ -7315,11 +7308,7 @@ void write(FileStorage& fs, const String& objname, const std::vector<DMatch>& ma
73157308
int i, n = (int)matches.size();
73167309
for( i = 0; i < n; i++ )
73177310
{
7318-
const DMatch& m = matches[i];
7319-
cv::write(fs, m.queryIdx);
7320-
cv::write(fs, m.trainIdx);
7321-
cv::write(fs, m.imgIdx);
7322-
cv::write(fs, m.distance);
7311+
write(fs, matches[i]);
73237312
}
73247313
}
73257314

modules/core/test/test_io.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,3 +1013,74 @@ TEST(Core_InputOutput, filestorage_yaml_advanvced_type_heading)
10131013

10141014
ASSERT_EQ(cv::norm(inputMatrix, actualMatrix, NORM_INF), 0.);
10151015
}
1016+
1017+
TEST(Core_InputOutput, filestorage_keypoints_io)
1018+
{
1019+
vector<vector<KeyPoint> > kptsVec;
1020+
vector<KeyPoint> kpts;
1021+
kpts.push_back(KeyPoint(0, 0, 1.1f));
1022+
kpts.push_back(KeyPoint(1, 1, 1.1f));
1023+
kptsVec.push_back(kpts);
1024+
kpts.clear();
1025+
kpts.push_back(KeyPoint(0, 0, 1.1f, 10.1f, 34.5f, 10, 11));
1026+
kptsVec.push_back(kpts);
1027+
1028+
FileStorage writer("", FileStorage::WRITE + FileStorage::MEMORY + FileStorage::FORMAT_XML);
1029+
writer << "keypoints" << kptsVec;
1030+
String content = writer.releaseAndGetString();
1031+
1032+
FileStorage reader(content, FileStorage::READ + FileStorage::MEMORY);
1033+
vector<vector<KeyPoint> > readKptsVec;
1034+
reader["keypoints"] >> readKptsVec;
1035+
1036+
ASSERT_EQ(kptsVec.size(), readKptsVec.size());
1037+
1038+
for(size_t i = 0; i < kptsVec.size(); i++)
1039+
{
1040+
ASSERT_EQ(kptsVec[i].size(), readKptsVec[i].size());
1041+
for(size_t j = 0; j < kptsVec[i].size(); j++)
1042+
{
1043+
ASSERT_FLOAT_EQ(kptsVec[i][j].pt.x, readKptsVec[i][j].pt.x);
1044+
ASSERT_FLOAT_EQ(kptsVec[i][j].pt.y, readKptsVec[i][j].pt.y);
1045+
ASSERT_FLOAT_EQ(kptsVec[i][j].angle, readKptsVec[i][j].angle);
1046+
ASSERT_FLOAT_EQ(kptsVec[i][j].size, readKptsVec[i][j].size);
1047+
ASSERT_FLOAT_EQ(kptsVec[i][j].response, readKptsVec[i][j].response);
1048+
ASSERT_EQ(kptsVec[i][j].octave, readKptsVec[i][j].octave);
1049+
ASSERT_EQ(kptsVec[i][j].class_id, readKptsVec[i][j].class_id);
1050+
}
1051+
}
1052+
}
1053+
1054+
TEST(Core_InputOutput, filestorage_dmatch_io)
1055+
{
1056+
vector<vector<DMatch> > matchesVec;
1057+
vector<DMatch> matches;
1058+
matches.push_back(DMatch(1, 0, 10, 11.5f));
1059+
matches.push_back(DMatch(2, 1, 11, 21.5f));
1060+
matchesVec.push_back(matches);
1061+
matches.clear();
1062+
matches.push_back(DMatch(22, 10, 1, 1.5f));
1063+
matchesVec.push_back(matches);
1064+
1065+
FileStorage writer("", FileStorage::WRITE + FileStorage::MEMORY + FileStorage::FORMAT_XML);
1066+
writer << "dmatches" << matchesVec;
1067+
String content = writer.releaseAndGetString();
1068+
1069+
FileStorage reader(content, FileStorage::READ + FileStorage::MEMORY);
1070+
vector<vector<DMatch> > readKptsVec;
1071+
reader["dmatches"] >> readKptsVec;
1072+
1073+
ASSERT_EQ(matchesVec.size(), readKptsVec.size());
1074+
1075+
for(size_t i = 0; i < matchesVec.size(); i++)
1076+
{
1077+
ASSERT_EQ(matchesVec[i].size(), readKptsVec[i].size());
1078+
for(size_t j = 0; j < matchesVec[i].size(); j++)
1079+
{
1080+
ASSERT_FLOAT_EQ(matchesVec[i][j].distance, readKptsVec[i][j].distance);
1081+
ASSERT_EQ(matchesVec[i][j].imgIdx, readKptsVec[i][j].imgIdx);
1082+
ASSERT_EQ(matchesVec[i][j].queryIdx, readKptsVec[i][j].queryIdx);
1083+
ASSERT_EQ(matchesVec[i][j].trainIdx, readKptsVec[i][j].trainIdx);
1084+
}
1085+
}
1086+
}

0 commit comments

Comments
 (0)