提交 0154b18a 编写于 作者: I Ilya Lysenkov

Speeded up BruteForceMatcher using matrix multiplication

上级 2f4d3965
......@@ -1872,6 +1872,9 @@ void BruteForceMatcher<Distance>::matchImpl( const Mat& descriptors_1, const Mat
}
}
template<>
void BruteForceMatcher<L2<float> >::matchImpl( const Mat& descriptors_1, const Mat& descriptors_2,
const Mat& mask, vector<int>& matches ) const;
CV_EXPORTS DescriptorMatcher* createDescriptorMatcher( const string& descriptorMatcherType );
......
......@@ -41,6 +41,10 @@
#include "precomp.hpp"
#ifdef HAVE_EIGEN2
#include <Eigen/Array>
#endif
//#define _KDTREE
using namespace std;
......@@ -267,6 +271,47 @@ DescriptorMatcher* createDescriptorMatcher( const string& descriptorMatcherType
}
template<>
void BruteForceMatcher<L2<float> >::matchImpl( const Mat& descriptors_1, const Mat& descriptors_2,
const Mat& mask, vector<int>& 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<descriptors_1.rows;i++ )
{
Mat distances = (-2)*descriptors_1.row(i)*desc_2t;
distances += norms;
Point minLoc;
minMaxLoc ( distances, 0, 0, &minLoc );
matches.push_back( minLoc.x );
}
#else
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> desc1;
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> desc2;
cv2eigen( descriptors_1, desc1);
cv2eigen( descriptors_2, desc2 );
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> norms = desc2.rowwise().squaredNorm().transpose();
for( int i=0;i<descriptors_1.rows;i++ )
{
//TODO: it doesn't work in Debug due to assert in lazyAssign
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> distances = (-2)*desc1.row(i)*desc2.transpose();
distances += norms;
int idx;
distances.minCoeff(&idx);
matches.push_back( idx );
}
#endif
}
/****************************************************************************************\
* GenericDescriptorMatch *
\****************************************************************************************/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册