Commit 931b32d1 authored by Vladislav Sovrasov's avatar Vladislav Sovrasov

core: add single DMatch/Keypoint I/O

parent 89ce2dc4
......@@ -1055,6 +1055,20 @@ void write(FileStorage& fs, const String& name, const Range& r )
write(fs, r);
}
static inline
void write(FileStorage& fs, const String& name, const KeyPoint& r )
{
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
write(fs, r);
}
static inline
void write(FileStorage& fs, const String& name, const DMatch& r )
{
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ+FileNode::FLOW);
write(fs, r);
}
template<typename _Tp> static inline
void write( FileStorage& fs, const String& name, const std::vector<_Tp>& vec )
{
......@@ -1245,6 +1259,14 @@ void operator >> (const FileNode& n, std::vector<KeyPoint>& vec)
{
read(n, vec);
}
static inline
void operator >> (const FileNode& n, KeyPoint& kpt)
{
FileNodeIterator it = n.begin();
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.
......@@ -1254,6 +1276,13 @@ void operator >> (const FileNode& n, std::vector<DMatch>& vec)
read(n, vec);
}
static inline
void operator >> (const FileNode& n, DMatch& m)
{
FileNodeIterator it = n.begin();
it >> m.queryIdx >> m.trainIdx >> m.imgIdx >> m.distance;
}
//! @} FileNode
//! @relates cv::FileNodeIterator
......
......@@ -1014,7 +1014,7 @@ TEST(Core_InputOutput, filestorage_yaml_advanvced_type_heading)
ASSERT_EQ(cv::norm(inputMatrix, actualMatrix, NORM_INF), 0.);
}
TEST(Core_InputOutput, filestorage_keypoints_io)
TEST(Core_InputOutput, filestorage_keypoints_vec_vec_io)
{
vector<vector<KeyPoint> > kptsVec;
vector<KeyPoint> kpts;
......@@ -1051,41 +1051,6 @@ TEST(Core_InputOutput, filestorage_keypoints_io)
}
}
TEST(Core_InputOutput, filestorage_dmatch_io)
{
vector<vector<DMatch> > matchesVec;
vector<DMatch> matches;
matches.push_back(DMatch(1, 0, 10, 11.5f));
matches.push_back(DMatch(2, 1, 11, 21.5f));
matchesVec.push_back(matches);
matches.clear();
matches.push_back(DMatch(22, 10, 1, 1.5f));
matchesVec.push_back(matches);
FileStorage writer("", FileStorage::WRITE + FileStorage::MEMORY + FileStorage::FORMAT_XML);
writer << "dmatches" << matchesVec;
String content = writer.releaseAndGetString();
FileStorage reader(content, FileStorage::READ + FileStorage::MEMORY);
vector<vector<DMatch> > readKptsVec;
reader["dmatches"] >> readKptsVec;
ASSERT_EQ(matchesVec.size(), readKptsVec.size());
for(size_t i = 0; i < matchesVec.size(); i++)
{
ASSERT_EQ(matchesVec[i].size(), readKptsVec[i].size());
for(size_t j = 0; j < matchesVec[i].size(); j++)
{
ASSERT_FLOAT_EQ(matchesVec[i][j].distance, readKptsVec[i][j].distance);
ASSERT_EQ(matchesVec[i][j].imgIdx, readKptsVec[i][j].imgIdx);
ASSERT_EQ(matchesVec[i][j].queryIdx, readKptsVec[i][j].queryIdx);
ASSERT_EQ(matchesVec[i][j].trainIdx, readKptsVec[i][j].trainIdx);
}
}
}
#if 0
TEST(Core_InputOutput, FileStorage_DMatch)
{
cv::FileStorage fs("dmatch.yml", cv::FileStorage::WRITE | cv::FileStorage::MEMORY);
......@@ -1106,7 +1071,6 @@ TEST(Core_InputOutput, FileStorage_DMatch)
EXPECT_EQ(d.imgIdx, d_read.imgIdx);
EXPECT_EQ(d.distance, d_read.distance);
}
#endif
TEST(Core_InputOutput, FileStorage_DMatch_vector)
{
......@@ -1125,15 +1089,8 @@ TEST(Core_InputOutput, FileStorage_DMatch_vector)
EXPECT_STREQ(fs_result.c_str(),
"%YAML:1.0\n"
"---\n"
#if 0
"dv:\n"
" - [ 1, 2, 3, -1.5000000000000000e+00 ]\n"
" - [ 2, 3, 4, 1.5000000000000000e+00 ]\n"
" - [ 3, 2, 1, 5.0000000000000000e-01 ]\n"
#else
"dv: [ 1, 2, 3, -1.5000000000000000e+00, 2, 3, 4, 1.5000000000000000e+00,\n"
" 3, 2, 1, 5.0000000000000000e-01 ]\n"
#endif
);
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
......@@ -1177,19 +1134,9 @@ TEST(Core_InputOutput, FileStorage_DMatch_vector_vector)
"%YAML:1.0\n"
"---\n"
"dvv:\n"
#if 0
" -\n"
" - [ 1, 2, 3, -1.5000000000000000e+00 ]\n"
" - [ 2, 3, 4, 1.5000000000000000e+00 ]\n"
" - [ 3, 2, 1, 5.0000000000000000e-01 ]\n"
" -\n"
" - [ 3, 2, 1, 5.0000000000000000e-01 ]\n"
" - [ 1, 2, 3, -1.5000000000000000e+00 ]\n"
#else
" - [ 1, 2, 3, -1.5000000000000000e+00, 2, 3, 4, 1.5000000000000000e+00,\n"
" 3, 2, 1, 5.0000000000000000e-01 ]\n"
" - [ 3, 2, 1, 5.0000000000000000e-01, 1, 2, 3, -1.5000000000000000e+00 ]\n"
#endif
);
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment