当前位置:   article > 正文

Surf特征点检测与匹配代码分析_特征匹配surf代码

特征匹配surf代码
  1. #include <opencv2\core\core.hpp>  
  2. #include <opencv2\highgui\highgui.hpp>  
  3. #include <opencv2\nonfree\features2d.hpp>  
  4. #include <opencv2\nonfree\nonfree.hpp>  
  5. #include <opencv2\calib3d\calib3d.hpp>  
  6. #include <iostream>  
  7.   
  8. using namespace std;  
  9. using namespace cv;  
  10.   
  11. vector<DMatch> ransac(vector<KeyPoint> queryKeyPoint,vector<KeyPoint> trainKeyPoint,vector<DMatch> matches);  
  12.   
  13.   
  14. int main(int argc,char* argv[])  
  15. {  
  16.     //定义图像  
  17.     Mat queryImage,trainImage;  
  18.       
  19.     //读取图像  
  20.     queryImage = imread("3.jpg",IMREAD_COLOR);  
  21.     trainImage = imread("4.jpg",IMREAD_COLOR);  
  22.     //判断是否读取图像  
  23.     if(queryImage.empty() || trainImage.empty())  
  24.     {  
  25.         cerr<<"Failure in loading image"<<endl;  
  26.         return -1;  
  27.     }  
  28.   
  29.     //定义Surf特征检测器  
  30.     SurfFeatureDetector surfDetector;  
  31.   
  32.     //定义特征点  
  33.     vector<KeyPoint> queryKeyPoint,trainKeyPoint;  
  34.   
  35.     //检测surf特征  
  36.     surfDetector.detect(queryImage,queryKeyPoint);  
  37.     surfDetector.detect(trainImage,trainKeyPoint);  
  38.   
  39.     //查看检测到的特征点的数目  
  40.     cout<<"Number of queryKeyPoint:"<<queryKeyPoint.size()<<endl;  
  41.     cout<<"Number of trainKeyPoint:"<<trainKeyPoint.size()<<endl;  
  42.   
  43.     //定义绘制surf特征点的图像  
  44.     Mat queryImageKeyPoint,trainImageKeyPoint;  
  45.   
  46.     //绘制surf特征点  
  47.     drawKeypoints(queryImage,queryKeyPoint,queryImageKeyPoint,Scalar(0,0,255),  
  48.         /*DrawMatchesFlags::DRAW_RICH_KEYPOINTS*/DrawMatchesFlags::DEFAULT);  
  49.     drawKeypoints(trainImage,trainKeyPoint,trainImageKeyPoint,Scalar(0,0,255),  
  50.         /*DrawMatchesFlags::DRAW_RICH_KEYPOINTS*/DrawMatchesFlags::DEFAULT);  
  51.   
  52.     //定义显示窗口,可不要  
  53.     namedWindow("queryImageKeyPoint",CV_WINDOW_AUTOSIZE);  
  54.     namedWindow("trainImageKeyPoint",CV_WINDOW_AUTOSIZE);  
  55.   
  56.     //显示surf特征点  
  57.     imshow("queryImageKeyPoint",queryImageKeyPoint);  
  58.     imshow("trainImageKeyPoint",trainImageKeyPoint);  
  59.   
  60.     //定义surf特征描述子  
  61.     Mat queryDescriptor,trainDescriptor;  
  62.   
  63.     //定义Surf特征描述子提取器  
  64.     SurfDescriptorExtractor surfExtractor;  
  65.   
  66.     //提取特征描述子  
  67.     surfExtractor.compute(queryImage,queryKeyPoint,queryDescriptor);  
  68.     surfExtractor.compute(trainImage,trainKeyPoint,trainDescriptor);  
  69.       
  70.     //定义描述子匹配对  
  71.     vector<DMatch> bfMatches;  
  72.     vector<DMatch> flannMatches;  
  73.   
  74.     //Brute Match,穷举法求最近邻  
  75.     BFMatcher bfMatcher;  
  76.     bfMatcher.match(queryDescriptor,trainDescriptor,bfMatches);  
  77.   
  78.     //FlannBasedMatcher,最近邻近似匹配  
  79.     FlannBasedMatcher flannMatcher;  
  80.     flannMatcher.match(queryDescriptor,trainDescriptor,flannMatches);  
  81.   
  82.   
  83.     //对匹配点进行RANSAC过滤  
  84.     vector<DMatch> ransac_BFMatches,ransac_FlannMatches;  
  85.   
  86.     ransac_BFMatches=ransac(queryKeyPoint,trainKeyPoint,bfMatches);  
  87.     ransac_FlannMatches=ransac(queryKeyPoint,trainKeyPoint,flannMatches);  
  88.   
  89.     //定义匹配图像  
  90.     Mat bfMatchImage,flannMatchImage;  
  91.   
  92.     //绘制匹配图像  
  93.     //drawMatches(queryImage,queryKeyPoint,trainImage,trainKeyPoint,bfMatches,bfMatchImage);  
  94.     //drawMatches(queryImage,queryKeyPoint,trainImage,trainKeyPoint,flannMatches,flannMatchImage);  
  95.     drawMatches(queryImage,queryKeyPoint,trainImage,trainKeyPoint,ransac_BFMatches,bfMatchImage);  
  96.     drawMatches(queryImage,queryKeyPoint,trainImage,trainKeyPoint,ransac_FlannMatches,flannMatchImage);  
  97.       
  98.     //显示匹配结果  
  99.     imshow("BFMatch",bfMatchImage);  
  100.     imshow("FlannMatch",flannMatchImage);  
  101.   
  102.   
  103.     waitKey(0);  
  104.     return 0;  
  105. }  
  106.   
  107. vector<DMatch> ransac(vector<KeyPoint> queryKeyPoint,vector<KeyPoint> trainKeyPoint,vector<DMatch> matches)  
  108. {  
  109.     cout<<"ransac前:"<<matches.size()<<endl;  
  110.     vector<Point2f> queryPoints(matches.size()),trainPoints(matches.size());  
  111.     for(int i=0;i<matches.size();i++)  
  112.     {  
  113.         queryPoints[i] = queryKeyPoint[matches[i].queryIdx].pt;   
  114.         trainPoints[i] = trainKeyPoint[matches[i].trainIdx].pt;   
  115.     }  
  116.   
  117.     Mat H;  
  118.     vector<unsigned char> inlierMask(matches.size());  
  119.     vector<DMatch> ransac_matches;  
  120.     H = findHomography(queryPoints,trainPoints,CV_RANSAC,3,inlierMask);  
  121.   
  122.     for(int i=0;i<inlierMask.size();i++)  
  123.     {  
  124.         if(inlierMask[i])  
  125.         {  
  126.             ransac_matches.push_back(matches[i]);  
  127.         }  
  128.     }  
  129.     cout<<"ransac后:"<<ransac_matches.size()<<endl;  
  130.     return ransac_matches;  
  131. }  

SURF原理:http://blog.csdn.net/quincuntial/article/details/50461274


转载自:http://blog.csdn.net/quincuntial/article/details/50132089


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/356064
推荐阅读
相关标签
  

闽ICP备14008679号