Skip to content

Commit 8ef23d6

Browse files
committed
Merge pull request opencv#8308 from sovrasov:fs_dmatch_kpts_update
2 parents 24efb02 + 931b32d commit 8ef23d6

File tree

2 files changed

+128
-24
lines changed

2 files changed

+128
-24
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,20 @@ void write(FileStorage& fs, const String& name, const Range& r )
10551055
write(fs, r);
10561056
}
10571057

1058+
static inline
1059+
void write(FileStorage& fs, const String& name, const KeyPoint& r )
1060+
{
1061+
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1062+
write(fs, r);
1063+
}
1064+
1065+
static inline
1066+
void write(FileStorage& fs, const String& name, const DMatch& r )
1067+
{
1068+
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
1069+
write(fs, r);
1070+
}
1071+
10581072
template<typename _Tp> static inline
10591073
void write( FileStorage& fs, const String& name, const std::vector<_Tp>& vec )
10601074
{
@@ -1245,6 +1259,14 @@ void operator >> (const FileNode& n, std::vector<KeyPoint>& vec)
12451259
{
12461260
read(n, vec);
12471261
}
1262+
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+
12481270
/** @brief Reads DMatch from a file storage.
12491271
*/
12501272
//It needs special handling because it contains two types of fields, int & float.
@@ -1254,6 +1276,13 @@ void operator >> (const FileNode& n, std::vector<DMatch>& vec)
12541276
read(n, vec);
12551277
}
12561278

1279+
static inline
1280+
void operator >> (const FileNode& n, DMatch& m)
1281+
{
1282+
FileNodeIterator it = n.begin();
1283+
it >> m.queryIdx >> m.trainIdx >> m.imgIdx >> m.distance;
1284+
}
1285+
12571286
//! @} FileNode
12581287

12591288
//! @relates cv::FileNodeIterator

modules/core/test/test_io.cpp

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ TEST(Core_InputOutput, filestorage_yaml_advanvced_type_heading)
10141014
ASSERT_EQ(cv::norm(inputMatrix, actualMatrix, NORM_INF), 0.);
10151015
}
10161016

1017-
TEST(Core_InputOutput, filestorage_keypoints_io)
1017+
TEST(Core_InputOutput, filestorage_keypoints_vec_vec_io)
10181018
{
10191019
vector<vector<KeyPoint> > kptsVec;
10201020
vector<KeyPoint> kpts;
@@ -1051,36 +1051,111 @@ TEST(Core_InputOutput, filestorage_keypoints_io)
10511051
}
10521052
}
10531053

1054-
TEST(Core_InputOutput, filestorage_dmatch_io)
1054+
TEST(Core_InputOutput, FileStorage_DMatch)
10551055
{
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);
1056+
cv::FileStorage fs("dmatch.yml", cv::FileStorage::WRITE | cv::FileStorage::MEMORY);
10641057

1065-
FileStorage writer("", FileStorage::WRITE + FileStorage::MEMORY + FileStorage::FORMAT_XML);
1066-
writer << "dmatches" << matchesVec;
1067-
String content = writer.releaseAndGetString();
1058+
cv::DMatch d(1, 2, 3, -1.5f);
10681059

1069-
FileStorage reader(content, FileStorage::READ + FileStorage::MEMORY);
1070-
vector<vector<DMatch> > readKptsVec;
1071-
reader["dmatches"] >> readKptsVec;
1060+
EXPECT_NO_THROW(fs << "d" << d);
1061+
cv::String fs_result = fs.releaseAndGetString();
1062+
EXPECT_STREQ(fs_result.c_str(), "%YAML:1.0\n---\nd: [ 1, 2, 3, -1.5000000000000000e+00 ]\n");
1063+
1064+
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
1065+
1066+
cv::DMatch d_read;
1067+
ASSERT_NO_THROW(fs_read["d"] >> d_read);
1068+
1069+
EXPECT_EQ(d.queryIdx, d_read.queryIdx);
1070+
EXPECT_EQ(d.trainIdx, d_read.trainIdx);
1071+
EXPECT_EQ(d.imgIdx, d_read.imgIdx);
1072+
EXPECT_EQ(d.distance, d_read.distance);
1073+
}
10721074

1073-
ASSERT_EQ(matchesVec.size(), readKptsVec.size());
1075+
TEST(Core_InputOutput, FileStorage_DMatch_vector)
1076+
{
1077+
cv::FileStorage fs("dmatch.yml", cv::FileStorage::WRITE | cv::FileStorage::MEMORY);
1078+
1079+
cv::DMatch d1(1, 2, 3, -1.5f);
1080+
cv::DMatch d2(2, 3, 4, 1.5f);
1081+
cv::DMatch d3(3, 2, 1, 0.5f);
1082+
std::vector<cv::DMatch> dv;
1083+
dv.push_back(d1);
1084+
dv.push_back(d2);
1085+
dv.push_back(d3);
1086+
1087+
EXPECT_NO_THROW(fs << "dv" << dv);
1088+
cv::String fs_result = fs.releaseAndGetString();
1089+
EXPECT_STREQ(fs_result.c_str(),
1090+
"%YAML:1.0\n"
1091+
"---\n"
1092+
"dv: [ 1, 2, 3, -1.5000000000000000e+00, 2, 3, 4, 1.5000000000000000e+00,\n"
1093+
" 3, 2, 1, 5.0000000000000000e-01 ]\n"
1094+
);
1095+
1096+
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
1097+
1098+
std::vector<cv::DMatch> dv_read;
1099+
ASSERT_NO_THROW(fs_read["dv"] >> dv_read);
1100+
1101+
ASSERT_EQ(dv.size(), dv_read.size());
1102+
for (size_t i = 0; i < dv.size(); i++)
1103+
{
1104+
EXPECT_EQ(dv[i].queryIdx, dv_read[i].queryIdx);
1105+
EXPECT_EQ(dv[i].trainIdx, dv_read[i].trainIdx);
1106+
EXPECT_EQ(dv[i].imgIdx, dv_read[i].imgIdx);
1107+
EXPECT_EQ(dv[i].distance, dv_read[i].distance);
1108+
}
1109+
}
10741110

1075-
for(size_t i = 0; i < matchesVec.size(); i++)
1111+
TEST(Core_InputOutput, FileStorage_DMatch_vector_vector)
1112+
{
1113+
cv::FileStorage fs("dmatch.yml", cv::FileStorage::WRITE | cv::FileStorage::MEMORY);
1114+
1115+
cv::DMatch d1(1, 2, 3, -1.5f);
1116+
cv::DMatch d2(2, 3, 4, 1.5f);
1117+
cv::DMatch d3(3, 2, 1, 0.5f);
1118+
std::vector<cv::DMatch> dv1;
1119+
dv1.push_back(d1);
1120+
dv1.push_back(d2);
1121+
dv1.push_back(d3);
1122+
1123+
std::vector<cv::DMatch> dv2;
1124+
dv2.push_back(d3);
1125+
dv2.push_back(d1);
1126+
1127+
std::vector< std::vector<cv::DMatch> > dvv;
1128+
dvv.push_back(dv1);
1129+
dvv.push_back(dv2);
1130+
1131+
EXPECT_NO_THROW(fs << "dvv" << dvv);
1132+
cv::String fs_result = fs.releaseAndGetString();
1133+
EXPECT_STREQ(fs_result.c_str(),
1134+
"%YAML:1.0\n"
1135+
"---\n"
1136+
"dvv:\n"
1137+
" - [ 1, 2, 3, -1.5000000000000000e+00, 2, 3, 4, 1.5000000000000000e+00,\n"
1138+
" 3, 2, 1, 5.0000000000000000e-01 ]\n"
1139+
" - [ 3, 2, 1, 5.0000000000000000e-01, 1, 2, 3, -1.5000000000000000e+00 ]\n"
1140+
);
1141+
1142+
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
1143+
1144+
std::vector< std::vector<cv::DMatch> > dvv_read;
1145+
ASSERT_NO_THROW(fs_read["dvv"] >> dvv_read);
1146+
1147+
ASSERT_EQ(dvv.size(), dvv_read.size());
1148+
for (size_t j = 0; j < dvv.size(); j++)
10761149
{
1077-
ASSERT_EQ(matchesVec[i].size(), readKptsVec[i].size());
1078-
for(size_t j = 0; j < matchesVec[i].size(); j++)
1150+
const std::vector<cv::DMatch>& dv = dvv[j];
1151+
const std::vector<cv::DMatch>& dv_read = dvv_read[j];
1152+
ASSERT_EQ(dvv.size(), dvv_read.size());
1153+
for (size_t i = 0; i < dv.size(); i++)
10791154
{
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);
1155+
EXPECT_EQ(dv[i].queryIdx, dv_read[i].queryIdx);
1156+
EXPECT_EQ(dv[i].trainIdx, dv_read[i].trainIdx);
1157+
EXPECT_EQ(dv[i].imgIdx, dv_read[i].imgIdx);
1158+
EXPECT_EQ(dv[i].distance, dv_read[i].distance);
10841159
}
10851160
}
10861161
}

0 commit comments

Comments
 (0)