From 97640847c587d32d2cf1c12305e86b42b44fdfff Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Thu, 11 Oct 2012 18:25:45 +0400 Subject: [PATCH] Add methods to sort keypoints and corresponding descriptors --- modules/features2d/perf/perf_orb.cpp | 3 ++ modules/ts/include/opencv2/ts/ts_perf.hpp | 18 +++++++++ modules/ts/src/ts_perf.cpp | 49 +++++++++++++++++++++-- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/modules/features2d/perf/perf_orb.cpp b/modules/features2d/perf/perf_orb.cpp index cf7b46b648..3a8b423f10 100644 --- a/modules/features2d/perf/perf_orb.cpp +++ b/modules/features2d/perf/perf_orb.cpp @@ -27,6 +27,7 @@ PERF_TEST_P(orb, detect, testing::Values(ORB_IMAGES)) TEST_CYCLE() detector(frame, mask, points); + sort(points.begin(), points.end(), comparators::KeypointGreater()); SANITY_CHECK_KEYPOINTS(points); } @@ -44,6 +45,7 @@ PERF_TEST_P(orb, extract, testing::Values(ORB_IMAGES)) ORB detector(1500, 1.3f, 5); vector points; detector(frame, mask, points); + sort(points.begin(), points.end(), comparators::KeypointGreater()); Mat descriptors; @@ -69,6 +71,7 @@ PERF_TEST_P(orb, full, testing::Values(ORB_IMAGES)) TEST_CYCLE() detector(frame, mask, points, descriptors, false); + perf::sort(points, descriptors); SANITY_CHECK_KEYPOINTS(points); SANITY_CHECK(descriptors); } diff --git a/modules/ts/include/opencv2/ts/ts_perf.hpp b/modules/ts/include/opencv2/ts/ts_perf.hpp index 04aefb6f61..e5023150fb 100644 --- a/modules/ts/include/opencv2/ts/ts_perf.hpp +++ b/modules/ts/include/opencv2/ts/ts_perf.hpp @@ -510,7 +510,25 @@ struct CV_EXPORTS RectLess_ typedef RectLess_ RectLess; +struct CV_EXPORTS KeypointGreater +{ + bool operator()(const cv::KeyPoint& kp1, const cv::KeyPoint& kp2) const + { + if(kp1.response > kp2.response) return true; + if(kp1.response < kp2.response) return false; + if(kp1.size > kp2.size) return true; + if(kp1.size < kp2.size) return false; + if(kp1.octave > kp2.octave) return true; + if(kp1.octave < kp2.octave) return false; + if(kp1.pt.y < kp2.pt.y) return false; + if(kp1.pt.y > kp2.pt.y) return true; + return kp1.pt.x < kp2.pt.x; + } +}; + } //namespace comparators + +void CV_EXPORTS sort(std::vector& pts, cv::InputOutputArray descriptors); } //namespace perf #endif //__OPENCV_TS_PERF_HPP__ diff --git a/modules/ts/src/ts_perf.cpp b/modules/ts/src/ts_perf.cpp index 6e14422cde..117cf0860e 100644 --- a/modules/ts/src/ts_perf.cpp +++ b/modules/ts/src/ts_perf.cpp @@ -1246,6 +1246,51 @@ TestBase::_declareHelper::_declareHelper(TestBase* t) : test(t) { } +/*****************************************************************************************\ +* miscellaneous +\*****************************************************************************************/ + +namespace { +struct KeypointComparator +{ + std::vector& pts_; + comparators::KeypointGreater cmp; + + KeypointComparator(std::vector& pts) : pts_(pts), cmp() {} + + bool operator()(int idx1, int idx2) const + { + return cmp(pts_[idx1], pts_[idx2]); + } +}; +}//namespace + +void perf::sort(std::vector& pts, cv::InputOutputArray descriptors) +{ + cv::Mat desc = descriptors.getMat(); + + CV_Assert(pts.size() == (size_t)desc.rows); + cv::AutoBuffer idxs(desc.rows); + + for (int i = 0; i < desc.rows; ++i) + idxs[i] = i; + + std::sort((int*)idxs, (int*)idxs + desc.rows, KeypointComparator(pts)); + + std::vector spts(pts.size()); + cv::Mat sdesc(desc.size(), desc.type()); + + for(int j = 0; j < desc.rows; ++j) + { + spts[j] = pts[idxs[j]]; + cv::Mat row = sdesc.row(j); + desc.row(idxs[j]).copyTo(row); + } + + spts.swap(pts); + sdesc.copyTo(desc); +} + /*****************************************************************************************\ * ::perf::GpuPerf \*****************************************************************************************/ @@ -1293,7 +1338,3 @@ void PrintTo(const Size& sz, ::std::ostream* os) } // namespace cv - -/*****************************************************************************************\ -* ::cv::PrintTo -\*****************************************************************************************/