快速最近邻逼近搜索函数库(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); }
《opencv学习笔记》-- 特征点匹配
最新推荐文章于 2024-09-02 09:11:16 发布