ZBLOG

#include #include #include #include #include #include #include #include using namespace std; u...

以下是将代码转换为适用于OpenCV 4版本的代码:

  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <opencv2/opencv.hpp>
  5. #include <opencv2/core/core.hpp>
  6. #include <opencv2/features2d.hpp> // 注意:特征提取头文件发生了改变
  7. #include <opencv2/highgui/highgui.hpp>
  8. #include <chrono>
  9. using namespace std;
  10. using namespace cv;
  11. int main(int argc, char **argv) {
  12. if (argc != 3) {
  13. cout << "usage: feature_extraction img1 img2" << endl;
  14. return 1;
  15. }
  16. //-- 读取图像
  17. Mat img_1 = imread("D:\\slambook2-master\\ch7\\1.png", IMREAD_COLOR);
  18. Mat img_2 = imread("D:\\slambook2-master\\ch7\\2.png", IMREAD_COLOR);
  19. assert(img_1.data != nullptr && img_2.data != nullptr);
  20. //-- 初始化
  21. vector<KeyPoint> keypoints_1, keypoints_2;
  22. Mat descriptors_1, descriptors_2;
  23. Ptr<FeatureDetector> detector = ORB::create();
  24. Ptr<DescriptorExtractor> descriptor = ORB::create();
  25. Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
  26. //-- 第一步:检测 Oriented FAST 角点位置
  27. chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  28. #if CV_MAJOR_VERSION < 4 // 如果使用的是 OpenCV 3版本及以前的版本
  29. detector->detect(img_1, keypoints_1);
  30. detector->detect(img_2, keypoints_2);
  31. #else // 使用 OpenCV 4版本及更高版本
  32. detector->detectAndCompute(img_1, noArray(), keypoints_1, descriptors_1);
  33. detector->detectAndCompute(img_2, noArray(), keypoints_2, descriptors_2);
  34. #endif
  35. //-- 第二步:根据角点位置计算 BRIEF 描述子
  36. #if CV_MAJOR_VERSION < 4 // 如果使用的是 OpenCV 3版本及以前的版本
  37. descriptor->compute(img_1, keypoints_1, descriptors_1);
  38. descriptor->compute(img_2, keypoints_2, descriptors_2);
  39. #else // 使用 OpenCV 4版本及更高版本
  40. descriptor->compute(img_1, keypoints_1, descriptors_1);
  41. descriptor->compute(img_2, keypoints_2, descriptors_2);
  42. #endif
  43. chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
  44. chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  45. cout << "extract ORB cost = " << time_used.count() << " seconds. " << endl;
  46. Mat outimg1;
  47. #if CV_MAJOR_VERSION < 4 // 如果使用的是 OpenCV 3版本及以前的版本
  48. drawKeypoints(img_1, keypoints_1, outimg1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
  49. #else // 使用 OpenCV 4版本及更高版本
  50. drawKeypoints(img_1, keypoints_1, outimg1);
  51. #endif
  52. imshow("ORB features", outimg1);
  53. //-- 第三步:对两幅图像中的BRIEF描述子进行匹配,使用 Hamming 距离
  54. vector<DMatch> matches;
  55. #if CV_MAJOR_VERSION < 4 // 如果使用的是 OpenCV 3版本及以前的版本
  56. matcher->match(descriptors_1, descriptors_2, matches);
  57. #else // 使用 OpenCV 4版本及更高版本
  58. matcher->match(descriptors_1, descriptors_2, matches, noArray());
  59. #endif
  60. t2 = chrono::steady_clock::now();
  61. time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  62. cout << "match ORB cost = " << time_used.count() << " seconds. " << endl;
  63. //-- 第四步:匹配点对筛选
  64. auto min_max = minmax_element(matches.begin(), matches.end(), [](const DMatch &m1, const DMatch &m2) { return m1.distance < m2.distance; });
  65. double min_dist = min_max.first->distance;
  66. double max_dist = min_max.second->distance;
  67. printf("-- Max dist : %f \n", max_dist);
  68. printf("-- Min dist : %f \n", min_dist);
  69. std::vector<DMatch> good_matches;
  70. #if CV_MAJOR_VERSION < 4 // 如果使用的是 OpenCV 3版本及以前的版本
  71. for (int i = 0; i < descriptors_1.rows; i++) {
  72. if (matches[i].distance <= max(2 * min_dist, 30.0)) {
  73. good_matches.push_back(matches[i]);
  74. }
  75. }
  76. #else // 使用 OpenCV 4版本及更高版本
  77. double threshold = max(2 * min_dist, 30.0);
  78. for (const auto &match : matches) {
  79. if (match.distance <= threshold) {
  80. good_matches.push_back(match);
  81. }
  82. }
  83. #endif
  84. //-- 第五步:绘制匹配结果
  85. Mat img_match;
  86. Mat img_goodmatch;
  87. #if CV_MAJOR_VERSION < 4 // 如果使用的是 OpenCV 3版本及以前的版本
  88. drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_match);
  89. drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches, img_goodmatch);
  90. #else // 使用 OpenCV 4版本及更高版本
  91. drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_match);
  92. drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches,
  93. img_goodmatch,cv::Scalar::all(-1),cv::Scalar::all(-1),
  94. std::vector<char>(),cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
  95. #endif
  96. imshow("all matches", img_match);
  97. imshow("good matches", img_goodmatch);
  98. waitKey(0);
  99. return 0;
  100. }

请注意,代码中标记为OpenCV版本3和4的部分表示对应OpenCV库不同版本的兼容处理。根据您使用的具体OpenCV版本进行相应的修改即可。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=15041

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?