orb 純背景物體識別

#include <chrono>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
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(argv[1], CV_LOAD_IMAGE_COLOR);
  Mat img_2 = imread(argv[2], CV_LOAD_IMAGE_COLOR);

  VideoCapture vc("bbb.mp4");

  vc >> img_1;

  imwrite("img1.png", img_1);

  //-- 初始化
  std::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");

  detector->detect(img_1, keypoints_1);
  descriptor->compute(img_1, keypoints_1, descriptors_1);

  Mat outimg1;
  drawKeypoints(img_1, keypoints_1, outimg1, Scalar::all(-1),
                DrawMatchesFlags::DEFAULT);
  imshow("ORB features", outimg1);
  waitKey(3);
  while (1) {
    vc >> img_2;

    assert(img_1.data != nullptr && img_2.data != nullptr);
    Mat show;
    pyrDown(img_2, show);
    imshow("curent raw pydown", show);

    //-- 第一步:檢測 Oriented FAST 角點位置
    chrono::steady_clock::time_point t1 = chrono::steady_clock::now();

    detector->detect(img_2, keypoints_2);

    //-- 第二步:根據角點位置計算 BRIEF 描述子

    descriptor->compute(img_2, keypoints_2, descriptors_2);
    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;

    drawKeypoints(img_2, keypoints_2, outimg1, Scalar::all(-1),
                  DrawMatchesFlags::DEFAULT);

    vector<Point2f> pts;
    for (int ti = 0; ti < keypoints_2.size(); ti++)
      pts.push_back(keypoints_2[ti].pt);
    RotatedRect r = minAreaRect(pts);

    // std::cout<<keypoints_2[0].pt.x<<std::endl;
    // rectangle(, r.tl(),r.br(), (0, 0, 255), 2, 8, 0);
    rectangle(outimg1, r.boundingRect(), Scalar(255, 1, 0));

    pyrDown(outimg1, show);
    // imshow("all matches", img_match);
    // imshow("good matches", );

    imshow("ORB features", show);

    //-- 第三步:對兩幅圖像中的BRIEF描述子進行匹配,使用 Hamming 距離
    vector<DMatch> matches;
    t1 = chrono::steady_clock::now();
    matcher->match(descriptors_1, descriptors_2, matches);
    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);

    //當描述子之間的距離大於兩倍的最小距離時,即認爲匹配有誤.但有時候最小距離會非常小,設置一個經驗值30作爲下限.
    std::vector<DMatch> good_matches;
    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]);
      }
    }

    //-- 第五步:繪製匹配結果
    Mat img_match;
    Mat img_goodmatch;
    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);
    pyrDown(img_goodmatch, show);
    // imshow("all matches", img_match);
    imshow("good matches", show);
    waitKey(1);
  }

  return 0;
}

 

ubuntu 運行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章