OpenCV_局部图像特征的提取与匹配_源代码

转自http://blog.csdn.net/icvpr/article/details/8491369

OpenCV的feature2d module中提供了从局部图像特征(Local image feature)的检测、特征向量(feature vector)的提取,到特征匹配的实现。其中的局部图像特征包括了常用的几种局部图像特征检测与描述算子,如FAST、SURF、SIFT、以及ORB。对于高维特征向量之间的匹配,OpenCV主要有两种方式:1)BruteForce穷举法;2)FLANN近似K近邻算法(包含了多种高维特征向量匹配的算法,例如随机森林等)。


feature2d module: http://docs.opencv.org/modules/features2d/doc/features2d.html

OpenCV FLANN: http://docs.opencv.org/modules/flann/doc/flann.html

FLANN: http://www.cs.ubc.ca/~mariusm/index.php/FLANN/FLANN



下面的这段代码实现了基于OpenCV的局部图像特征检测、特征向量提取、以及高维特征向量的匹配功能。


版本:OpenCV2.4.2 



LocalFeature.h

[cpp]  view plain copy
  1. //  局部图像特征提取与匹配  
  2. //      Author:  www.icvpr.com  
  3. //  Blog  :  http://blog.csdn.net/icvpr  
  4.       
  5. #ifndef _FEATURE_H_   
  6. #define _FEATURE_H_  
  7.   
  8. #include <iostream>  
  9. #include <vector>  
  10. #include <string>  
  11.   
  12. #include <opencv2/opencv.hpp>  
  13.   
  14. using namespace cv;  
  15. using namespace std;  
  16.   
  17. class Feature  
  18. {  
  19. public:  
  20.     Feature();  
  21.     ~Feature();  
  22.   
  23.     Feature(const string& detectType, const string& extractType, const string& matchType);  
  24.   
  25. public:  
  26.       
  27.     void detectKeypoints(const Mat& image, vector<KeyPoint>& keypoints);   // 检测特征点  
  28.     void extractDescriptors(const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptor);   // 提取特征向量  
  29.     void bestMatch(const Mat& queryDescriptor, Mat& trainDescriptor, vector<DMatch>& matches);  // 最近邻匹配  
  30.     void knnMatch(const Mat& queryDescriptor, Mat& trainDescriptor, vector<vector<DMatch>>& matches, int k);   // K近邻匹配  
  31.   
  32.     void saveKeypoints(const Mat& image, const vector<KeyPoint>& keypoints, const string& saveFileName = "");  // 保存特征点  
  33.     void saveMatches(const Mat& queryImage,  
  34.              const vector<KeyPoint>& queryKeypoints,  
  35.              const Mat& trainImage,  
  36.              const vector<KeyPoint>& trainKeypoints,  
  37.              const vector<DMatch>& matches,  
  38.              const string& saveFileName = "");   // 保存匹配结果到图片中  
  39.   
  40. private:  
  41.     Ptr<FeatureDetector> m_detector;  
  42.     Ptr<DescriptorExtractor> m_extractor;  
  43.     Ptr<DescriptorMatcher> m_matcher;  
  44.   
  45.     string m_detectType;  
  46.     string m_extractType;  
  47.     string m_matchType;  
  48.   
  49. };  
  50.   
  51.   
  52. #endif  


LocalFeature.cpp

[cpp]  view plain copy
  1. //  局部图像特征提取与匹配    
  2. //  Author:  www.icvpr.com  
  3. //  Blog  :  http://blog.csdn.net/icvpr  
  4.   
  5. #include "LocalFeature.h"  
  6.   
  7. Feature::Feature()  
  8. {  
  9.     m_detectType = "SIFT";  
  10.     m_extractType = "SIFT";  
  11.     m_matchType = "FruteForce";  
  12.     initModule_nonfree();   
  13. }  
  14.   
  15. Feature::~Feature()  
  16. {  
  17.   
  18. }  
  19.   
  20.   
  21. Feature::Feature(const string& detectType, const string& extractType, const string& matchType)  
  22. {  
  23.     assert(!detectType.empty());  
  24.     assert(!extractType.empty());  
  25.     assert(!matchType.empty());  
  26.   
  27.     m_detectType = detectType;  
  28.     m_extractType = extractType;  
  29.     m_matchType = matchType;  
  30.     initModule_nonfree();   
  31. }  
  32.   
  33.   
  34. void Feature::detectKeypoints(const Mat& image, std::vector<KeyPoint>& keypoints)   
  35. {  
  36.     assert(image.type() == CV_8UC1);  
  37.     assert(!m_detectType.empty());  
  38.   
  39.     keypoints.clear();  
  40.     m_detector = FeatureDetector::create(m_detectType);  
  41.     m_detector->detect(image, keypoints);  
  42.   
  43. }  
  44.   
  45.   
  46.   
  47. void Feature::extractDescriptors(const Mat& image, std::vector<KeyPoint>& keypoints, Mat& descriptor)  
  48. {  
  49.     assert(image.type() == CV_8UC1);  
  50.     assert(!m_extractType.empty());  
  51.   
  52.     m_extractor = DescriptorExtractor::create(m_extractType);  
  53.     m_extractor->compute(image, keypoints, descriptor);  
  54.   
  55. }  
  56.   
  57.   
  58. void Feature::bestMatch(const Mat& queryDescriptor, Mat& trainDescriptor, std::vector<DMatch>& matches)   
  59. {  
  60.     assert(!queryDescriptor.empty());  
  61.     assert(!trainDescriptor.empty());  
  62.     assert(!m_matchType.empty());  
  63.   
  64.     matches.clear();  
  65.   
  66.     m_matcher = DescriptorMatcher::create(m_matchType);  
  67.     m_matcher->add(std::vector<Mat>(1, trainDescriptor));  
  68.     m_matcher->train();  
  69.     m_matcher->match(queryDescriptor, matches);  
  70.   
  71. }  
  72.   
  73.   
  74. void Feature::knnMatch(const Mat& queryDescriptor, Mat& trainDescriptor, std::vector<std::vector<DMatch>>& matches, int k)  
  75. {  
  76.     assert(k > 0);  
  77.     assert(!queryDescriptor.empty());  
  78.     assert(!trainDescriptor.empty());  
  79.     assert(!m_matchType.empty());  
  80.   
  81.     matches.clear();  
  82.   
  83.     m_matcher = DescriptorMatcher::create(m_matchType);  
  84.     m_matcher->add(std::vector<Mat>(1, trainDescriptor));  
  85.     m_matcher->train();  
  86.     m_matcher->knnMatch(queryDescriptor, matches, k);  
  87.   
  88. }  
  89.   
  90.   
  91.   
  92. void Feature::saveKeypoints(const Mat& image, const vector<KeyPoint>& keypoints, const string& saveFileName)  
  93. {  
  94.     assert(!saveFileName.empty());  
  95.   
  96.     Mat outImage;  
  97.     cv::drawKeypoints(image, keypoints, outImage, Scalar(255,255,0), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );  
  98.   
  99.     //  
  100.     string saveKeypointsImgName = saveFileName + "_" + m_detectType + ".jpg";  
  101.     imwrite(saveKeypointsImgName, outImage);  
  102.   
  103. }  
  104.   
  105.   
  106.   
  107. void Feature::saveMatches(const Mat& queryImage,  
  108.                             const vector<KeyPoint>& queryKeypoints,  
  109.                             const Mat& trainImage,  
  110.                             const vector<KeyPoint>& trainKeypoints,  
  111.                             const vector<DMatch>& matches,  
  112.                             const string& saveFileName)  
  113. {  
  114.     assert(!saveFileName.empty());  
  115.   
  116.     Mat outImage;  
  117.     cv::drawMatches(queryImage, queryKeypoints, trainImage, trainKeypoints, matches, outImage,   
  118.                 Scalar(255, 0, 0), Scalar(0, 255, 255), vector<char>(),  DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);  
  119.   
  120.     //  
  121.     string saveMatchImgName = saveFileName + "_" + m_detectType + "_" + m_extractType + "_" + m_matchType + ".jpg";  
  122.     imwrite(saveMatchImgName, outImage);  
  123. }  





 

测试代码, main.cpp

[cpp]  view plain copy
  1. //  局部图像特征提取与匹配    
  2. //  Author:  www.icvpr.com    
  3. //  Blog  : http://blog.csdn.net/icvpr      
  4.     
  5. #include     
  6. #include     
  7. #include <opencv2/opencv.hpp>    
  8.     
  9. using namespace cv;    
  10. using namespace std;    
  11.     
  12. #include "LocalFeature.h"    
  13.     
  14. int main(int argc, char** argv)    
  15. {    
  16.     if (argc != 6)    
  17.     {    
  18.         cout << "wrong usage!" << endl;    
  19.         cout << "usage: .exe FAST SIFT BruteForce queryImage trainImage" << endl;    
  20.         return -1;    
  21.     }    
  22.     
  23.     string detectorType = argv[1];    
  24.     string extractorType = argv[2];    
  25.     string matchType = argv[3];    
  26.     string queryImagePath = argv[4];    
  27.     string trainImagePath = argv[5];    
  28.         
  29.     Mat queryImage = imread(queryImagePath, CV_LOAD_IMAGE_GRAYSCALE);    
  30.     if (queryImage.empty())    
  31.     {    
  32.         cout<<"read failed"<< endl;    
  33.         return -1;    
  34.     }    
  35.         
  36.     Mat trainImage = imread(trainImagePath, CV_LOAD_IMAGE_GRAYSCALE);    
  37.     if (trainImage.empty())    
  38.     {    
  39.         cout<<"read failed"<< endl;    
  40.         return -1;    
  41.     }    
  42.         
  43.     Feature feature(detectorType, extractorType, matchType);    
  44.         
  45.     vector queryKeypoints, trainKeypoints;    
  46.     feature.detectKeypoints(queryImage, queryKeypoints);    
  47.     feature.detectKeypoints(trainImage, trainKeypoints);    
  48.         
  49.     Mat queryDescriptor, trainDescriptor;    
  50.         
  51.     feature.extractDescriptors(queryImage, queryKeypoints, queryDescriptor);    
  52.     feature.extractDescriptors(trainImage, trainKeypoints, trainDescriptor);    
  53.         
  54.     vector matches;    
  55.     feature.bestMatch(queryDescriptor, trainDescriptor, matches);    
  56.         
  57.     vector<vector> knnmatches;    
  58.     feature.knnMatch(queryDescriptor, trainDescriptor, knnmatches, 2);    
  59.         
  60.     Mat outImage;    
  61.     feature.saveMatches(queryImage, queryKeypoints, trainImage, trainKeypoints, matches, "../");    
  62.         
  63.     return 0;    
  64. }    





下面是对不同的局部图像特征检测算子的实验对比结果:

(说明:这里只是简单地对各个局部图像特征检测算子进行了对比,实际应用中需要考虑不同检测算子的特点,以及所应用的场景来选择。)



1. FAST+SIFT+FLANN (即局部图像特征检测算子+特征向量描述算子+高维特征向量匹配方法)



2. HARRIS+SIFT+FLANN


3. SURF+SIFT+FLANN


4. MSER+SIFT+FLANN


5. STAR+SIFT+FLANN


6.SIFT+SIFT+FLANN


7. ORB+ORB+FLANN


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值