《opencv学习笔记》-- 特征点匹配

快速最近邻逼近搜索函数库(Fast Library for ApproximateNearest Neighbors,FLANN)。
 

找到最佳匹配:DescriptorMatcher::match方法
 

void DescriptorMatcher::match(const Mat& queryDescriptors,  
                              const Mat& trainDescriptors,  
                              vector<DMatch>& matches,       
                              const Mat& mask=Mat())  

参数1,查询描述符集

参数2,训练描述符集

参数3,得到的匹配。若查询描述符有在掩膜中被标记出来,则没有匹配添加到描述符中。

             则匹配量可能会比查询描述符数量少

参数4,指定输入查询和训练描述符允许匹配的掩膜

void DescriptorMatcher::match( const Mat& queryDescriptors,
                               vector<DMatch>& matches,
                               const vector<Mat>& masks = vector<Mat>())

参数1,查询描述符集

参数2,得到的匹配。若查询描述符有在掩膜中被标记出来,则没有匹配添加到描述符中。

             则匹配量可能会比查询描述符数量少

参数3,一组掩膜,每个masks [i]从第i个图像trainDesccollection[i]指定输入查询和训练描述

             符允许匹配的掩膜

#include "opencv2/core/core.hpp " 
#include "opencv2/features2d/features2d.hpp" 
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/1egacy/legacy.hpp"

Mat img_1 = imread("1.jpg", 1);
Mat img_2 = imread("2.jpg", 1);

//利用SURF检测器检测的关键点
int minHessian = 300;
SURF detector(minHessian);
std::vector<KeyPoint> keypoints_1, keypoints_2;
detector.detect(img_1, keypoints_1);
detector.detect(img_2, keypoints_2);

//计算描述符(特征向量)
SURF extractor;
Mat descriptors_1, descriptors_2;
extractor.compute(img_1, keypoints_1, descriptors_1);
extractor.compute(img_2, keypoints_2, descriptors_2);

//采用FLANN算法匹配描述符向量
FlannBasedMatcher matcher;
std::vector<DMatch> matches;
matcher.match (descriptors_1, descriptors_2, matches);
double max_dist = 0 ; 
double min_dist = 100;

//【快速计算关键点之间的最大和最小距离
for(int i = 0; i < descriptors_1.rows; i++) {
    double dist = matches[i].distance;
    if( dist < min_dist ) 
        min_dist = dist;
    if( dist > max_dist ) 
        max_dist = dist;
}

//存下符合条件的匹配结果(即其距离小于2* min_dist的),使用radiusMatch同样可行
std::vector<DMatch> good_matches;
for(int i = 0; i < descriptors_1.rows; i++) {
    if(matches [i].distance < 2*min_dist) { 
        good_matches.push_back(matches[i]); 
    }
}

//绘制出符合条件的匹配点
Mat img_matches;
drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches, 
            img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(),  
            DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS) ;

//输出相关匹配点信息
for ( int i = 0;  i < good_matches.size(); i++ ) { 
  printf("符合条件的匹配点[%d]特征点1: %d-- 特征点2: %d \n", i, 	
        good_matches[i].queryIdx, good_matches[i].trainIdx); 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值