From 931b32d1022dbb999ede6143307720910177d2c3 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Fri, 3 Mar 2017 13:58:55 +0300 Subject: [PATCH] core: add single DMatch/Keypoint I/O --- .../core/include/opencv2/core/persistence.hpp | 29 ++++++++++ modules/core/test/test_io.cpp | 55 +------------------ 2 files changed, 30 insertions(+), 54 deletions(-) diff --git a/modules/core/include/opencv2/core/persistence.hpp b/modules/core/include/opencv2/core/persistence.hpp index 61d1d27fa0..3ead8af14b 100644 --- a/modules/core/include/opencv2/core/persistence.hpp +++ b/modules/core/include/opencv2/core/persistence.hpp @@ -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 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& 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& 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 diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 59d260d10e..33af4c8b76 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -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 > kptsVec; vector kpts; @@ -1051,41 +1051,6 @@ TEST(Core_InputOutput, filestorage_keypoints_io) } } -TEST(Core_InputOutput, filestorage_dmatch_io) -{ - vector > matchesVec; - vector 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 > 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);