From 0154b18a3d32b906e2841ccb214efef5207b207b Mon Sep 17 00:00:00 2001 From: Ilya Lysenkov Date: Wed, 30 Jun 2010 14:37:42 +0000 Subject: [PATCH] Speeded up BruteForceMatcher using matrix multiplication --- .../include/opencv2/features2d/features2d.hpp | 3 ++ modules/features2d/src/descriptors.cpp | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/modules/features2d/include/opencv2/features2d/features2d.hpp b/modules/features2d/include/opencv2/features2d/features2d.hpp index 6835335400..354537d5e3 100644 --- a/modules/features2d/include/opencv2/features2d/features2d.hpp +++ b/modules/features2d/include/opencv2/features2d/features2d.hpp @@ -1872,6 +1872,9 @@ void BruteForceMatcher::matchImpl( const Mat& descriptors_1, const Mat } } +template<> +void BruteForceMatcher >::matchImpl( const Mat& descriptors_1, const Mat& descriptors_2, + const Mat& mask, vector& matches ) const; CV_EXPORTS DescriptorMatcher* createDescriptorMatcher( const string& descriptorMatcherType ); diff --git a/modules/features2d/src/descriptors.cpp b/modules/features2d/src/descriptors.cpp index 362351c025..619a158a60 100644 --- a/modules/features2d/src/descriptors.cpp +++ b/modules/features2d/src/descriptors.cpp @@ -41,6 +41,10 @@ #include "precomp.hpp" +#ifdef HAVE_EIGEN2 +#include +#endif + //#define _KDTREE using namespace std; @@ -267,6 +271,47 @@ DescriptorMatcher* createDescriptorMatcher( const string& descriptorMatcherType } +template<> +void BruteForceMatcher >::matchImpl( const Mat& descriptors_1, const Mat& descriptors_2, + const Mat& mask, vector& matches ) const +{ + matches.clear(); + matches.reserve( descriptors_1.rows ); +#if (defined _DEBUG || !defined HAVE_EIGEN2) + Mat norms; + cv::reduce( descriptors_2.mul( descriptors_2 ), norms, 1, 0); + norms = norms.t(); + Mat desc_2t = descriptors_2.t(); + for( int i=0;i desc1; + Eigen::Matrix desc2; + cv2eigen( descriptors_1, desc1); + cv2eigen( descriptors_2, desc2 ); + + Eigen::Matrix norms = desc2.rowwise().squaredNorm().transpose(); + for( int i=0;i distances = (-2)*desc1.row(i)*desc2.transpose(); + + distances += norms; + int idx; + distances.minCoeff(&idx); + matches.push_back( idx ); + } +#endif +} + + /****************************************************************************************\ * GenericDescriptorMatch * \****************************************************************************************/