OpenCV描述符匹配子通用接口
struct DMatch 关键点描述符匹配结果存储结构。匹配结果包括查询描述符的索引,训练描述符的索引,训练图像的索引和描述符之间的距离.
struct DMatch { DMatch() : queryIdx(-1), trainIdx(-1), imgIdx(-1), distance(std::numeric_limits<float>::max()) {} DMatch( int _queryIdx, int _trainIdx, float _distance ) : queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(-1), distance(_distance) {} DMatch( int _queryIdx, int _trainIdx, int _imgIdx, float _distance ) : queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(_imgIdx), distance(_distance) {} int queryIdx; // query descriptor index int trainIdx; // train descriptor index int imgIdx; // train image index float distance; // less is better bool operator<( const DMatch &m ) const; };
class DescriptorMatcher { public: virtual ~DescriptorMatcher(); virtual void add( const vector<Mat>& descriptors ); const vector<Mat>& getTrainDescriptors() const; virtual void clear(); bool empty() const; virtual bool isMaskSupported() const = 0; virtual void train(); /* * Group of methods to match descriptors from an image pair. */ void match( const Mat& queryDescriptors, const Mat& trainDescriptors, vector<DMatch>& matches, const Mat& mask=Mat() ) const; void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, vector<vector<DMatch> >& matches, int k, const Mat& mask=Mat(), bool compactResult=false ) const; void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, vector<vector<DMatch> >& matches, float maxDistance, const Mat& mask=Mat(), bool compactResult=false ) const; /* * Group of methods to match descriptors from one image to an image set. */ void match( const Mat& queryDescriptors, vector<DMatch>& matches, const vector<Mat>& masks=vector<Mat>() ); void knnMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int k, const vector<Mat>& masks=vector<Mat>(), bool compactResult=false ); void radiusMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance, const vector<Mat>& masks=vector<Mat>(), bool compactResult=false ); virtual void read( const FileNode& ); virtual void write( FileStorage& ) const; virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0; static Ptr<DescriptorMatcher> create( const string& descriptorMatcherType ); protected: vector<Mat> trainDescCollection; ... };DescriptorMatcher::add 向训练描述符集中添加描述符。如果trainDescCollection不是空的,新的描述符将添加到已存在的描述符集中。
DescriptorMatcher::getTrainDescriptors 返回训练描述符集trainDescCollection的引用
DescriptorMatcher::clear 清空训练描述符集
DescriptorMatcher::empty 训练描述符集为空时返回true
DescriptorMatcher::isMaskSupported 描述符匹配子是否支持匹配
DescriptorMatcher::train 训练一个描述符匹配子
DescriptorMatcher::match 为查询集中的每一个描述符找到最匹配的结果
DescriptorMatcher::knnMatch 为查询集中的每一个描述符找到最匹配的k个结果
DescriptorMatcher::radiusMatch 为每一个查询描述符,找到不大于指定距离的描述符
DescriptorMatcher;:clone 拷贝匹配子
DescriptorMatcher::create 创建一个给定默认参数的类型的描述符匹配子
class BFMatcher : public DescriptorMatcher Brute-force描述符匹配子。该匹配子一一在一个集合中查找与另一个集合中的每一个描述符最为接近的描述符,该匹配子支持掩膜匹配。
class FlannBasedMatcher : public DescriptroMatcher Flann-based 描述符匹配子。该匹配子在训练描述符集中训练出flann::Index_,并调用它的nearest search method 找到最佳匹配。所以,该匹配子在匹配一个较大的训练集时应该比brute-force更快。该匹配子不支持掩膜匹配。
lass FlannBasedMatcher : public DescriptorMatcher { public: FlannBasedMatcher( const Ptr<flann::IndexParams>& indexParams=new flann::KDTreeIndexParams(), const Ptr<flann::SearchParams>& searchParams=new flann::SearchParams() ); virtual void add( const vector<Mat>& descriptors ); virtual void clear(); virtual void train(); virtual bool isMaskSupported() const; virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const; protected: ... };
参考:
1、OpenCV Documentationfeature2d 2D Features Framework:http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_descriptor_matchers.html?highlight=flannbasedmatcher#flannbasedmatcher