1. 程式人生 > >OpenCV中feature2D學習——BFMatcher和FlannBasedMatcher

OpenCV中feature2D學習——BFMatcher和FlannBasedMatcher

Brute Force匹配和FLANN匹配是opencv二維特徵點匹配常見的兩種辦法,分別對應BFMatcher(BruteForceMatcher)和FlannBasedMatcher。BFMatcher的建構函式如下:

C++:BFMatcher::BFMatcher(int normType=NORM_L2, bool crossCheck=false )
Parameters:
  • normType – One of NORM_L1NORM_L2NORM_HAMMINGNORM_HAMMING2L1 and L2 norms are preferable choices for SIFT and SURF descriptors, NORM_HAMMING
     should be used with ORB, BRISK and BRIEF, NORM_HAMMING2 should be used with ORB when WTA_K==3 or 4 (see ORB::ORB constructor description).
  • crossCheck – If it is false, this is will be default BFMatcher behaviour when it finds the k nearest neighbors for each query descriptor. If crossCheck==true, then the knnMatch()
     method with k=1 will only return pairs (i,j) such that for i-th query descriptor the j-th descriptor in the matcher’s collection is the nearest and vice versa, i.e. the BFMatcher will only return consistent pairs. Such technique usually produces best results with minimal number of outliers when there are enough matches. This is alternative to the ratio test, used by D. Lowe in SIFT paper.
FlannBasedMatcher的建構函式如下:
class 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:
    ...
};

二者的區別在於BFMatcher總是嘗試所有可能的匹配,從而使得它總能夠找到最佳匹配,這也是Brute Force(暴力法)的原始含義。而FlannBasedMatcher中FLANN的含義是Fast Library forApproximate Nearest Neighbors,從字面意思可知它是一種近似法,演算法更快但是找到的是最近鄰近似匹配,所以當我們需要找到一個相對好的匹配但是不需要最佳匹配的時候往往使用FlannBasedMatcher。當然也可以通過調整FlannBasedMatcher的引數來提高匹配的精度或者提高演算法速度,但是相應地演算法速度或者演算法精度會受到影響。

此外,使用特徵提取過程得到的特徵描述符(descriptor)資料型別有的是float型別的,比如說SurfDescriptorExtractor,

SiftDescriptorExtractor,有的是uchar型別的,比如說有ORB,BriefDescriptorExtractor。對應float型別的匹配方式有:FlannBasedMatcher,BruteForce<L2<float>>,BruteForce<SL2<float>>,BruteForce<L1<float>>。對應uchar型別的匹配方式有:BruteForce<Hammin>,BruteForce<HammingLUT>。所以ORB和BRIEF特徵描述子只能使用BruteForce匹配法。