OPENCV中BFMatcher(BruteForceMatcher)和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匹配法。

特征匹配算法是一种计算图像相似度的方法,其包括SURF、SIFT和ORB等算法。这些算法都是基于图像的特征点提取来计算相似度的。在Python,你可以使用OpenCV库来实现这些特征匹配算法。 以SURF算法为例,你可以使用OpenCV的`cv2.xfeatures2d.SURF_create()`函数来创建一个SURF对象,并使用`detectAndCompute()`函数来检测特征点并计算特征描述子。然后,你可以使用`BruteForceMatcher`或者`FlannBasedMatcher`等匹配器来进行特征点匹配。最后,根据匹配结果计算相似度得分。以下是一个示例代码: ```python import cv2 # 读取模板图像和测试图像 template_img = cv2.imread('template.jpg', 0) test_img = cv2.imread('test.jpg', 0) # 创建SURF对象 surf = cv2.xfeatures2d.SURF_create() # 检测特征点并计算特征描述子 kp1, des1 = surf.detectAndCompute(template_img, None) kp2, des2 = surf.detectAndCompute(test_img, None) # 创建匹配器 matcher = cv2.BFMatcher() # 对特征描述子进行匹配 matches = matcher.match(des1, des2) # 计算相似度得分 similarity_score = sum([match.distance for match in matches]) / len(matches) # 输出相似度得分 print("相似度得分:", similarity_score) ``` 这段代码,首先读取模板图像和测试图像。然后,创建SURF对象并使用`detectAndCompute()`函数提取特征点和计算特征描述子。接下来,使用`BFMatcher`匹配器匹配特征描述子,并计算相似度得分。最后,输出相似度得分。 除了SURF算法,你也可以使用SIFT和ORB算法来计算图像相似度。它们的使用方法类似,只需要将代码的SURF相关函数替换为对应的SIFT或ORB函数即可。 请注意,这些特征匹配算法对于图像的旋转、缩放、平移、仿射变换和光照强度等都是鲁棒的。但是,在进行相似度计算时,模板图像和测试图像的尺寸应保持一致。 参考文献: 引用 引用 引用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值