diff --git a/src/examples/test_matcher.cpp b/src/examples/test_matcher.cpp index 041c8b35186fc2b1ebe7439d3a2aa1aca5b22aa5..b8a58066fe135d7cb63ff236ea99a6253e79c55d 100644 --- a/src/examples/test_matcher.cpp +++ b/src/examples/test_matcher.cpp @@ -155,7 +155,7 @@ int main(void) mat_ptr->match(descriptors_old,descriptors,des_ptr->getSize(),matches); // filter - mat_ptr->filterByDistance(10, 0.25, kpts_old, kpts, matches, frame_cur.rows, frame_cur.cols, best_matches, kpts_matched_curf, kpts_matched_prevf); + mat_ptr->filterByDistance(10, 0.25, kpts_old, kpts, matches, frame_cur.rows, frame_cur.cols, kpts_matched_prevf, kpts_matched_curf, best_matches); } else { @@ -164,7 +164,7 @@ int main(void) mat_ptr->match(descriptors_old,descriptors,des_ptr->getSize(),matches); //filter - mat_ptr->filterByDistance(10, 0.25, kpts_old, kpts, matches, frame_cur.rows, frame_cur.cols, best_matches, kpts_matched_curf, kpts_matched_prevf); + mat_ptr->filterByDistance(10, 0.25, kpts_old, kpts, matches, frame_cur.rows, frame_cur.cols, kpts_matched_prevf, kpts_matched_curf, best_matches); } // Draw diff --git a/src/matchers/matcher_base.cpp b/src/matchers/matcher_base.cpp index 698a4256042e010d49f170d812da48af5bbff98b..2affc12462088c6a09725e52928787f5f6f17129 100644 --- a/src/matchers/matcher_base.cpp +++ b/src/matchers/matcher_base.cpp @@ -16,7 +16,10 @@ MatcherBase::~MatcherBase(void) { } -Scalar MatcherBase::match(const cv::Mat& _desc1, const cv::Mat& _desc2, const int& _desc_size_bytes, cv::DMatch& _match) +Scalar MatcherBase::match(const cv::Mat& _desc1, + const cv::Mat& _desc2, + const int& _desc_size_bytes, + cv::DMatch& _match) { std::vector<Scalar> normalized_scores; if (params_base_ptr_->match_type == MATCH) @@ -35,7 +38,11 @@ Scalar MatcherBase::match(const cv::Mat& _desc1, const cv::Mat& _desc2, const in return normalized_scores[0]; } -std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1, const cv::Mat& _desc2, const int& desc_size_bytes, DMatchVector& matches, cv::InputArray _mask) +std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1, + const cv::Mat& _desc2, + const int& desc_size_bytes, + DMatchVector& matches, + cv::InputArray _mask) { std::vector<Scalar> normalized_scores; @@ -63,7 +70,11 @@ std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1, const cv::Mat& _de return normalized_scores; } -std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1, const cv::Mat& _desc2, const int& desc_size_bytes, std::vector< DMatchVector >& matches, cv::InputArray _mask) +std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1, + const cv::Mat& _desc2, + const int& desc_size_bytes, + std::vector< DMatchVector >& matches, + cv::InputArray _mask) { std::vector<Scalar> normalized_scores; @@ -114,29 +125,36 @@ std::vector<Scalar> MatcherBase::match(const cv::Mat& _desc1, const cv::Mat& _de return normalized_scores; } -void MatcherBase::filterByDistance(const int& _max_pixel_dist, const float& _img_size_percentage, const KeyPointVector& _kpts1,const KeyPointVector& _kpts2, const DMatchVector& _dirty, const int& _img_width, const int& _img_height, DMatchVector& _filtered_matches, KeyPointVector& _filtered_kpts, KeyPointVector& _filtered_kpts_prev) +void MatcherBase::filterByDistance(const int& _max_pixel_dist, + const float& _img_size_percentage, + const KeyPointVector& _kps1, + const KeyPointVector& _kps2, + const DMatchVector& _dirty, + const int& _img_width, + const int& _img_height, + KeyPointVector& _inlier_kps1, + KeyPointVector& _inlier_kps2, + DMatchVector& _inlier_matches) { if (params_base_ptr_->match_type == MATCH) { double tresholdDist = _img_size_percentage * sqrt(double(_img_height*_img_height + _img_width*_img_width)); - _filtered_matches.reserve(_dirty.size()); + _inlier_matches.reserve(_dirty.size()); for (size_t ii = 0; ii < _dirty.size(); ++ii) { - cv::Point2f from = _kpts1[_dirty[ii].queryIdx].pt; - cv::Point2f to = _kpts2[_dirty[ii].trainIdx].pt; + cv::Point2f p1 = _kps1[_dirty[ii].queryIdx].pt; + cv::Point2f p2 = _kps2[_dirty[ii].trainIdx].pt; //calculate local distance for each possible match - double dist = std::sqrt((from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y)); + double dist = std::sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); //save as best match if local distance is in specified area - if (dist < tresholdDist && std::abs(from.y-to.y)<_max_pixel_dist && std::abs(from.x-to.x)<_max_pixel_dist) + if (dist < tresholdDist && std::abs(p1.y-p2.y)<_max_pixel_dist && std::abs(p1.x-p2.x)<_max_pixel_dist) { - _filtered_matches.push_back(_dirty[ii]); - cv::KeyPoint kpt = cv::KeyPoint(to,1); - cv::KeyPoint kpt_prev = cv::KeyPoint(from,1); - _filtered_kpts.push_back(kpt); - _filtered_kpts_prev.push_back(kpt_prev); + _inlier_matches.push_back(_dirty[ii]); + _inlier_kps1.push_back(cv::KeyPoint(p1,1)); + _inlier_kps2.push_back(cv::KeyPoint(p2,1)); } } } @@ -144,32 +162,39 @@ void MatcherBase::filterByDistance(const int& _max_pixel_dist, const float& _img std::cerr << "[" << name_ << "]: Wrong input type in filterByDistance method." << std::endl; } -void MatcherBase::filterByDistance(const int& _max_pixel_dist, const float& _img_size_percentage, const KeyPointVector& _kpts1,const KeyPointVector& _kpts2, const std::vector< DMatchVector >& _dirty, const int& _img_width, const int& _img_height, DMatchVector& _filtered_matches, KeyPointVector& _filtered_kpts, KeyPointVector& _filtered_kpts_prev) +void MatcherBase::filterByDistance(const int& _max_pixel_dist, + const float& _img_size_percentage, + const KeyPointVector& _kps1, + const KeyPointVector& _kps2, + const std::vector< DMatchVector >& _dirty, + const int& _img_width, + const int& _img_height, + KeyPointVector& _inlier_kps1, + KeyPointVector& _inlier_kps2, + DMatchVector& _inlier_matches) { if (params_base_ptr_->match_type == KNNMATCH || params_base_ptr_->match_type == RADIUSMATCH) { double tresholdDist = _img_size_percentage * sqrt(double(_img_height*_img_height + _img_width*_img_width)); - _filtered_matches.reserve(_dirty.size()); + _inlier_matches.reserve(_dirty.size()); for (size_t ii = 0; ii < _dirty.size(); ++ii) { for (unsigned int jj = 0; jj < _dirty[ii].size(); jj++) { - cv::Point2f from = _kpts1[_dirty[ii][jj].queryIdx].pt; - cv::Point2f to = _kpts2[_dirty[ii][jj].trainIdx].pt; + cv::Point2f p1 = _kps1[_dirty[ii][jj].queryIdx].pt; + cv::Point2f p2 = _kps2[_dirty[ii][jj].trainIdx].pt; //calculate local distance for each possible match - double dist = std::sqrt((from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y)); + double dist = std::sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); //save as best match if local distance is in specified area and on same height - if (dist < tresholdDist && std::abs(from.y-to.y)<_max_pixel_dist && std::abs(from.x-to.x)<_max_pixel_dist) + if (dist < tresholdDist && std::abs(p1.y-p2.y)<_max_pixel_dist && std::abs(p1.x-p2.x)<_max_pixel_dist) { - _filtered_matches.push_back(_dirty[ii][jj]); - cv::KeyPoint kpt = cv::KeyPoint(to,1); - cv::KeyPoint kpt_prev = cv::KeyPoint(from,1); - _filtered_kpts.push_back(kpt); - _filtered_kpts_prev.push_back(kpt_prev); - jj = _dirty[ii].size(); + _inlier_matches.push_back(_dirty[ii][jj]); + _inlier_kps2.push_back(cv::KeyPoint(p2,1)); + _inlier_kps1.push_back(cv::KeyPoint(p1,1)); + jj = _dirty[ii].size(); // Keep best match from NN } } } @@ -178,11 +203,11 @@ void MatcherBase::filterByDistance(const int& _max_pixel_dist, const float& _img std::cerr << "[" << name_ << "]: Wrong input type in filterByDistance method." << std::endl; } -void MatcherBase::filterByDistance(const KeyPointVector& _kpts1, - const KeyPointVector& _kpts2, +void MatcherBase::filterByDistance(const KeyPointVector& _kps1, + const KeyPointVector& _kps2, const DMatchVector& _raw_matches, - KeyPointVector& _inlier_kpts1, - KeyPointVector& _inlier_kpts2, + KeyPointVector& _inlier_kps1, + KeyPointVector& _inlier_kps2, DMatchVector& _inlier_matches) { if (params_base_ptr_->match_type == MATCH) @@ -190,8 +215,8 @@ void MatcherBase::filterByDistance(const KeyPointVector& _kpts1, _inlier_matches.reserve(_raw_matches.size()); for (size_t ii = 0; ii < _raw_matches.size(); ++ii) { - cv::Point2f p1 = _kpts1[_raw_matches[ii].queryIdx].pt; - cv::Point2f p2 = _kpts2[_raw_matches[ii].trainIdx].pt; + cv::Point2f p1 = _kps1[_raw_matches[ii].queryIdx].pt; + cv::Point2f p2 = _kps2[_raw_matches[ii].trainIdx].pt; //calculate local distance for each possible match Scalar dist = std::sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); @@ -200,8 +225,8 @@ void MatcherBase::filterByDistance(const KeyPointVector& _kpts1, if (dist < params_base_ptr_->max_match_euclidean_dist) { _inlier_matches.push_back(_raw_matches[ii]); - _inlier_kpts1.push_back(cv::KeyPoint(p1,1)); - _inlier_kpts2.push_back(cv::KeyPoint(p2,1)); + _inlier_kps1.push_back(cv::KeyPoint(p1,1)); + _inlier_kps2.push_back(cv::KeyPoint(p2,1)); } } } @@ -209,11 +234,11 @@ void MatcherBase::filterByDistance(const KeyPointVector& _kpts1, std::cerr << "[" << name_ << "]: Wrong input type in filterByDistance method." << std::endl; } -void MatcherBase::filterByDistance(const KeyPointVector& _kpts1, - const KeyPointVector& _kpts2, +void MatcherBase::filterByDistance(const KeyPointVector& _kps1, + const KeyPointVector& _kps2, const std::vector< DMatchVector >& _raw_matches, - KeyPointVector& _inlier_kpts1, - KeyPointVector& _inlier_kpts2, + KeyPointVector& _inlier_kps1, + KeyPointVector& _inlier_kps2, DMatchVector& _inlier_matches) { if (params_base_ptr_->match_type == KNNMATCH || params_base_ptr_->match_type == RADIUSMATCH) @@ -223,8 +248,8 @@ void MatcherBase::filterByDistance(const KeyPointVector& _kpts1, { for (unsigned int jj = 0; jj < _raw_matches[ii].size(); jj++) { - cv::Point2f p1 = _kpts1[_raw_matches[ii][jj].queryIdx].pt; - cv::Point2f p2 = _kpts2[_raw_matches[ii][jj].trainIdx].pt; + cv::Point2f p1 = _kps1[_raw_matches[ii][jj].queryIdx].pt; + cv::Point2f p2 = _kps2[_raw_matches[ii][jj].trainIdx].pt; //calculate local distance for each possible match double dist = std::sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); @@ -233,8 +258,8 @@ void MatcherBase::filterByDistance(const KeyPointVector& _kpts1, if (dist < params_base_ptr_->max_match_euclidean_dist) { _inlier_matches.push_back(_raw_matches[ii][jj]); - _inlier_kpts1.push_back(cv::KeyPoint(p2,1)); - _inlier_kpts2.push_back(cv::KeyPoint(p1,1)); + _inlier_kps1.push_back(cv::KeyPoint(p2,1)); + _inlier_kps2.push_back(cv::KeyPoint(p1,1)); break; } } diff --git a/src/matchers/matcher_base.h b/src/matchers/matcher_base.h index dd906e4596ad79170e2aef385a7cc2643b9d18d3..966c147ba2159ec11b977dd72d37c62a47327052 100644 --- a/src/matchers/matcher_base.h +++ b/src/matchers/matcher_base.h @@ -90,49 +90,50 @@ class MatcherBase : public VUBase, public std::enable_shared_from_this<MatcherBa const int& desc_size_bytes, cv::DMatch& _match); - // Identify good matches using RANSAC - void ransacTest(const KeyPointVector& _raw_kps1, - const KeyPointVector& _raw_kps2, - const DMatchVector& _raw_matches, - KeyPointVector& _inlier_kps1, - KeyPointVector& _inlier_kps2, - DMatchVector& _inlier_matches); - void filterByDistance(const int& _max_pixel_dist, const float& _percentage, - const KeyPointVector& _kpts1, - const KeyPointVector& _kpts2, + const KeyPointVector& _kps1, + const KeyPointVector& _kps2, const DMatchVector& _dirty, const int& _img_width, const int& _img_height, - DMatchVector& _filtered_matches, - KeyPointVector& _filtered_kpts, - KeyPointVector& _filtered_kpts_prev); + KeyPointVector& _inlier_kps1, + KeyPointVector& _inlier_kps2, + DMatchVector& _inlier_matches); + void filterByDistance(const int& _max_pixel_dist, const float& _percentage, - const KeyPointVector& _kpts1, - const KeyPointVector& _kpts2, + const KeyPointVector& _kps1, + const KeyPointVector& _kps2, const std::vector< DMatchVector >& _dirty, const int& _img_width, const int& _img_height, - DMatchVector& _filtered_matches, - KeyPointVector& _filtered_kpts, - KeyPointVector& _filtered_kpts_prev); + KeyPointVector& _inlier_kps1, + KeyPointVector& _inlier_kps2, + DMatchVector& _inlier_matches); - void filterByDistance(const KeyPointVector& _kpts1, - const KeyPointVector& _kpts2, + void filterByDistance(const KeyPointVector& _kps1, + const KeyPointVector& _kps2, const DMatchVector& _raw_matches, - KeyPointVector& _inlier_kpts1, - KeyPointVector& _inlier_kpts2, + KeyPointVector& _inlier_kps1, + KeyPointVector& _inlier_kps2, DMatchVector& _inlier_matches); - void filterByDistance(const KeyPointVector& _kpts1, - const KeyPointVector& _kpts2, + void filterByDistance(const KeyPointVector& _kps1, + const KeyPointVector& _kps2, const std::vector< DMatchVector >& _raw_matches, - KeyPointVector& _inlier_kpts1, - KeyPointVector& _inlier_kpts2, + KeyPointVector& _inlier_kps1, + KeyPointVector& _inlier_kps2, DMatchVector& _inlier_matches); + // Identify good matches using RANSAC + void ransacTest(const KeyPointVector& _raw_kps1, + const KeyPointVector& _raw_kps2, + const DMatchVector& _raw_matches, + KeyPointVector& _inlier_kps1, + KeyPointVector& _inlier_kps2, + DMatchVector& _inlier_matches); + // Factory method static MatcherBasePtr create(const std::string& _unique_name, const ParamsBasePtr _params); diff --git a/src/test/gtest_matchers.cpp b/src/test/gtest_matchers.cpp index 7155e3a2f23f1d5541e18e2b0575478bbb8db0dc..4bda4f81feb3b056f15d856a0c2595762ed0da7e 100644 --- a/src/test/gtest_matchers.cpp +++ b/src/test/gtest_matchers.cpp @@ -68,7 +68,7 @@ TEST(Matchers, MATCH) DMatchVector best_matches; KeyPointVector kpts_matched_img2; KeyPointVector kpts_matched_img1; - mat_ptr->filterByDistance(10, 0.25, kpts1, kpts2, matches, image1.rows, image1.cols, best_matches, kpts_matched_img2, kpts_matched_img1); + mat_ptr->filterByDistance(10, 0.25, kpts1, kpts2, matches, image1.rows, image1.cols, kpts_matched_img1, kpts_matched_img2, best_matches); ASSERT_TRUE(best_matches.size()-35>0); } @@ -121,7 +121,7 @@ TEST(Matchers, KNNMATCH) DMatchVector best_matches; KeyPointVector kpts_matched_img2; KeyPointVector kpts_matched_img1; - mat_ptr->filterByDistance(10, 0.25, kpts1, kpts2, matches_vec, image1.rows, image1.cols, best_matches, kpts_matched_img2, kpts_matched_img1); + mat_ptr->filterByDistance(10, 0.25, kpts1, kpts2, matches_vec, image1.rows, image1.cols, kpts_matched_img1, kpts_matched_img2, best_matches); ASSERT_TRUE(best_matches.size()-45>0); } @@ -176,7 +176,7 @@ TEST(Matchers, RADIUSMATCH) // DMatchVector best_matches; // KeyPointVector kpts_matched_img2; // KeyPointVector kpts_matched_img1; -// mat_ptr->filterByDistance(10, 0.25, kpts1, kpts2, matches_vec, image1.rows, image1.cols, best_matches, kpts_matched_img2, kpts_matched_img1); +// mat_ptr->filterByDistance(10, 0.25, kpts1, kpts2, matches_vec, image1.rows, image1.cols, kpts_matched_img1, kpts_matched_img2, best_matches); // ASSERT_EQ(best_matches.size(),48); }