ZBLOG

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

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

#include <vector>
#include <iostream>
#include <algorithm>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d.hpp>  // 注意:特征提取头文件发生了改变
#include <opencv2/highgui/highgui.hpp>
#include <chrono>

using namespace std;
using namespace cv;

int main(int argc, char **argv) {
    if (argc != 3) {
        cout << "usage: feature_extraction img1 img2" << endl;
        return 1;
    }
  
    //-- 读取图像
    Mat img_1 = imread("D:\\slambook2-master\\ch7\\1.png", IMREAD_COLOR);
    Mat img_2 = imread("D:\\slambook2-master\\ch7\\2.png", IMREAD_COLOR);
    assert(img_1.data != nullptr && img_2.data != nullptr);

    //-- 初始化
    vector<KeyPoint> keypoints_1, keypoints_2;
    Mat descriptors_1, descriptors_2;
    Ptr<FeatureDetector> detector = ORB::create();
    Ptr<DescriptorExtractor> descriptor = ORB::create();
    Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");

    //-- 第一步:检测 Oriented FAST 角点位置
    chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
    
#if CV_MAJOR_VERSION < 4   // 如果使用的是 OpenCV 3版本及以前的版本
    detector->detect(img_1, keypoints_1);
    detector->detect(img_2, keypoints_2);
#else   // 使用 OpenCV 4版本及更高版本
    detector->detectAndCompute(img_1, noArray(), keypoints_1, descriptors_1);
    detector->detectAndCompute(img_2, noArray(), keypoints_2, descriptors_2);
#endif
    
    //-- 第二步:根据角点位置计算 BRIEF 描述子
#if CV_MAJOR_VERSION < 4   // 如果使用的是 OpenCV 3版本及以前的版本
    descriptor->compute(img_1, keypoints_1, descriptors_1);
    descriptor->compute(img_2, keypoints_2, descriptors_2);
#else   // 使用 OpenCV 4版本及更高版本
    descriptor->compute(img_1, keypoints_1, descriptors_1);
    descriptor->compute(img_2, keypoints_2, descriptors_2);
#endif
    
    chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
    chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
    cout << "extract ORB cost = " << time_used.count() << " seconds. " << endl;

    Mat outimg1;
#if CV_MAJOR_VERSION < 4   // 如果使用的是 OpenCV 3版本及以前的版本
      drawKeypoints(img_1, keypoints_1, outimg1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
#else   // 使用 OpenCV 4版本及更高版本
      drawKeypoints(img_1, keypoints_1, outimg1);
#endif

      imshow("ORB features", outimg1);

    //-- 第三步:对两幅图像中的BRIEF描述子进行匹配,使用 Hamming 距离
    vector<DMatch> matches;
    
#if CV_MAJOR_VERSION < 4   // 如果使用的是 OpenCV 3版本及以前的版本
    matcher->match(descriptors_1, descriptors_2, matches);
#else   // 使用 OpenCV 4版本及更高版本
    matcher->match(descriptors_1, descriptors_2, matches, noArray());
#endif
    
    t2 = chrono::steady_clock::now();
    time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
    cout << "match ORB cost = " << time_used.count() << " seconds. " << endl;

    //-- 第四步:匹配点对筛选
    auto min_max = minmax_element(matches.begin(), matches.end(), [](const DMatch &m1, const DMatch &m2) { return m1.distance < m2.distance; });
    double min_dist = min_max.first->distance;
    double max_dist = min_max.second->distance;

    printf("-- Max dist : %f \n", max_dist);
    printf("-- Min dist : %f \n", min_dist);

    std::vector<DMatch> good_matches;
    
#if CV_MAJOR_VERSION < 4   // 如果使用的是 OpenCV 3版本及以前的版本
      for (int i = 0; i < descriptors_1.rows; i++) {
          if (matches[i].distance <= max(2 * min_dist, 30.0)) {
              good_matches.push_back(matches[i]);
          }
      }
#else   // 使用 OpenCV 4版本及更高版本
      double threshold = max(2 * min_dist, 30.0);
      for (const auto &match : matches) {
          if (match.distance <= threshold) {
              good_matches.push_back(match);
          }
      }
#endif

      //-- 第五步:绘制匹配结果
      Mat img_match;
      Mat img_goodmatch;

#if CV_MAJOR_VERSION < 4   // 如果使用的是 OpenCV 3版本及以前的版本
    drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_match);
    drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches, img_goodmatch);
#else   // 使用 OpenCV 4版本及更高版本
    drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_match);
    drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches,
                img_goodmatch,cv::Scalar::all(-1),cv::Scalar::all(-1),
                std::vector<char>(),cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
#endif

      imshow("all matches", img_match);
      imshow("good matches", img_goodmatch);

      waitKey(0);

      return 0;
}

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


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?