opencv3中使用FLANN进行特征点匹配

来源:互联网 发布:自学网络安全知乎 编辑:程序博客网 时间:2024/05/22 10:53
#include<opencv2/opencv.hpp>#include<iostream>#include<vector>using namespace cv;using namespace std;int main(){Mat srcImage1 = imread("mofang1.jpg");Mat srcImage2 = imread("mofang2.jpg");imshow("【原图1】", srcImage1);imshow("【原图2】", srcImage2);//首先对两幅图像进行特征点的检测//先准备参数vector<KeyPoint> keyPoint1;vector<KeyPoint> keyPoint2;SURF surf;surf.detect(srcImage1, keyPoint1);surf.detect(srcImage2, keyPoint2);//利用得到的特征点计算特征描述子//目的:对得到的每个特征点进行特征描述,整合到Mat类型的矩阵中(计算结果是Mat类型的)//该得到的结果矩阵的行数就是特征点的个数,因为是对每个点进行描述,所以每行都会有一个描述的字子向量,共同构成Mat矩阵Mat descriImage1, descriImage2;surf.compute(srcImage1, keyPoint1, descriImage1);surf.compute(srcImage2, keyPoint2, descriImage2);//正式开始在两幅图像中进行匹配//先得到一个匹配向量FlannBasedMatcher FLMatcher;vector<DMatch> g_vMatches;//g_vMatches就是得到的匹配向量FLMatcher.match(descriImage1, descriImage2, g_vMatches);//用找最大最小值的方式找到 两幅图像中匹配的点的距离的最大值和最小值//这里的 keyPoint1.size() 和 descriImage1.rows是一样的值,因为descriImage1的行数就是检测到的特征点的个数double minDistance = g_vMatches[0].distance, maxDistance = g_vMatches[0].distance;for (size_t i = 0; i < keyPoint1.size(); i++){double currDistance = g_vMatches[i].distance;if (currDistance < minDistance)minDistance = currDistance;if (currDistance > maxDistance)maxDistance = currDistance;}//定义一个新的变量,用来存储 通过距离检测后  通过阀值的点vector<DMatch> newMatches;for (size_t i = 0; i < keyPoint1.size(); i++){if (g_vMatches[i].distance < 1.46 * minDistance)newMatches.push_back(g_vMatches[i]);}//用绘制函数对匹配向量进行绘制Mat dstImage;drawMatches(srcImage1, keyPoint1, srcImage2, keyPoint2, newMatches, dstImage, Scalar(theRNG().uniform(0, 255), theRNG().uniform(0, 255), theRNG().uniform(0, 255)), Scalar(theRNG().uniform(0, 255), theRNG().uniform(0, 255), theRNG().uniform(0, 255)), Mat(), 2);imshow("【特征提取后的图像】", dstImage);waitKey(0);/*注意:以上运算如果是将图像转换为灰度图再进行计算,会提高运算速率(运算时用灰度图,绘制时用BGR图)*/return 0;}

0 0