OpenCV中feature2D学习——FAST特征点检测与SIFT/SURF/BRIEF特征提取与匹配

以你之姓@ 2022-08-07 10:42 399阅读 0赞

在前面的文章《OpenCV中feature2D学习——FAST特征点检测》中讲了利用FAST算子进行特征点检测,这里尝试使用FAST算子来进行特征点检测,并结合SIFT/SURF/BRIEF算子进行特征点提取和匹配。

I、结合SIFT算子进行特征点提取和匹配

由于数据类型的不同,SIFT和SURF算子只能采用FlannBasedMatcher或者BruteForceMatcher来进行匹配(参考OpenCV中feature2D学习——BFMatcher和FlannBasedMatcher)。

  1. /**
  2. * @概述:采用FAST算子检测特征点,采用SIFT算子对特征点进行特征提取,并使用BruteForce匹配法进行特征点的匹配
  3. * @类和函数:FastFeatureDetector + SiftDescriptorExtractor + BruteForceMatcher
  4. * @author:holybin
  5. */
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include "opencv2/core/core.hpp"
  9. #include "opencv2/nonfree/features2d.hpp" //SurfFeatureDetector实际在该头文件中
  10. #include "opencv2/legacy/legacy.hpp" //BruteForceMatcher实际在该头文件中
  11. //#include "opencv2/features2d/features2d.hpp" //FlannBasedMatcher实际在该头文件中
  12. #include "opencv2/highgui/highgui.hpp"
  13. using namespace cv;
  14. using namespace std;
  15. int main( int argc, char** argv )
  16. {
  17. Mat src_1 = imread("cat3d120.jpg");
  18. Mat src_2 = imread("cat0.jpg");
  19. if( !src_1.data || !src_2.data )
  20. {
  21. cout<< " --(!) Error reading images "<<endl;
  22. return -1;
  23. }
  24. //-- Step 1: 使用FAST算子检测特征点
  25. FastFeatureDetector fast(20);
  26. vector<KeyPoint> keypoints_1, keypoints_2;
  27. fast.detect( src_1, keypoints_1 ); //FAST(src_1, keypoints_1, 20);
  28. fast.detect( src_2, keypoints_2 ); //FAST(src_2, keypoints_2, 20);
  29. cout<<"img1--number of keypoints: "<<keypoints_1.size()<<endl;
  30. cout<<"img2--number of keypoints: "<<keypoints_2.size()<<endl;
  31. //-- Step 2: 使用SIFT算子提取特征(计算特征向量)
  32. SiftDescriptorExtractor extractor; //SurfDescriptorExtractor extractor;
  33. Mat descriptors_1, descriptors_2;
  34. extractor.compute( src_1, keypoints_1, descriptors_1 );
  35. extractor.compute( src_2, keypoints_2, descriptors_2 );
  36. //-- Step 3: 使用BruteForce法进行暴力匹配
  37. BruteForceMatcher< L2<float> > matcher; //FlannBasedMatcher matcher;
  38. vector< DMatch > matches;
  39. matcher.match( descriptors_1, descriptors_2, matches );
  40. cout<<"number of matches: "<<matches.size()<<endl;
  41. //-- 显示匹配结果
  42. Mat matchImg;
  43. drawMatches( src_1, keypoints_1, src_2, keypoints_2, matches, matchImg,
  44. Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
  45. imshow("matching result", matchImg );
  46. imwrite("match_result.png", matchImg);
  47. waitKey(0);
  48. return 0;
  49. }

运行结果如下:

SouthEast

SouthEast 1

II、结合BRIEF算子进行特征点提取和匹配

BRIEF算子只能采用BruteForceMatcher来进行匹配(参考OpenCV中feature2D学习——BFMatcher和FlannBasedMatcher)。

  1. /**
  2. * @概述:采用FAST算子检测特征点,采用BRIEF算子对特征点进行特征提取,并使用BruteForce匹配法进行特征点的匹配
  3. * @类和函数:FastFeatureDetector + BriefDescriptorExtractor + BruteForceMatcher
  4. * @author:holybin
  5. */
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include "opencv2/core/core.hpp"
  9. #include "opencv2/nonfree/features2d.hpp" //SurfFeatureDetector实际在该头文件中
  10. #include "opencv2/legacy/legacy.hpp" //BruteForceMatcher实际在该头文件中
  11. //#include "opencv2/features2d/features2d.hpp" //FlannBasedMatcher实际在该头文件中
  12. #include "opencv2/highgui/highgui.hpp"
  13. using namespace cv;
  14. using namespace std;
  15. int main( int argc, char** argv )
  16. {
  17. Mat src_1 = imread("cat3d120.jpg");
  18. Mat src_2 = imread("cat0.jpg");
  19. if( !src_1.data || !src_2.data )
  20. {
  21. cout<< " --(!) Error reading images "<<endl;
  22. return -1;
  23. }
  24. //-- Step 1: 使用FAST算子检测特征点
  25. FastFeatureDetector fast(20);
  26. vector<KeyPoint> keypoints_1, keypoints_2;
  27. fast.detect( src_1, keypoints_1 ); //FAST(src_1, keypoints_1, 20);
  28. fast.detect( src_2, keypoints_2 ); //FAST(src_2, keypoints_2, 20);
  29. cout<<"img1--number of keypoints: "<<keypoints_1.size()<<endl;
  30. cout<<"img2--number of keypoints: "<<keypoints_2.size()<<endl;
  31. //-- Step 2: 使用BRIEF算子提取特征(计算特征向量)
  32. BriefDescriptorExtractor extractor;
  33. Mat descriptors_1, descriptors_2;
  34. extractor.compute( src_1, keypoints_1, descriptors_1 );
  35. extractor.compute( src_2, keypoints_2, descriptors_2 );
  36. //-- Step 3: 使用BruteForce法进行暴力匹配
  37. BruteForceMatcher< L2<float> > matcher; //FlannBasedMatcher matcher;
  38. vector< DMatch > matches;
  39. matcher.match( descriptors_1, descriptors_2, matches );
  40. cout<<"number of matches: "<<matches.size()<<endl;
  41. //-- 显示匹配结果
  42. Mat matchImg;
  43. drawMatches( src_1, keypoints_1, src_2, keypoints_2, matches, matchImg,
  44. Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
  45. imshow("matching result", matchImg );
  46. imwrite("match_result.png", matchImg);
  47. waitKey(0);
  48. return 0;
  49. }

运行结果如下:

SouthEast 2

SouthEast 3

发表评论

表情:
评论列表 (有 0 条评论,399人围观)

还没有评论,来说两句吧...

相关阅读