视觉SLAM——三角测量

实践:三角测量

slambook2/ch7/triangulation.cpp

#include <iostream>
#include <opencv2/opencv.hpp>
// #include "extra.h" // used in opencv2
using namespace std;
using namespace cv;

void find_feature_matches(
  const Mat &img_1, const Mat &img_2,
  std::vector<KeyPoint> &keypoints_1,
  std::vector<KeyPoint> &keypoints_2,
  std::vector<DMatch> &matches);

void pose_estimation_2d2d(
  const std::vector<KeyPoint> &keypoints_1,
  const std::vector<KeyPoint> &keypoints_2,
  const std::vector<DMatch> &matches,
  Mat &R, Mat &t);

void triangulation(
  const vector<KeyPoint> &keypoint_1,
  const vector<KeyPoint> &keypoint_2,
  const std::vector<DMatch> &matches,
  const Mat &R, const Mat &t,
  vector<Point3d> &points
);

/// 作图用
inline cv::Scalar get_color(float depth) {
    
    
  float up_th = 50, low_th = 10, th_range = up_th - low_th;
  if (depth > up_th) depth = up_th;
  if (depth < low_th) depth = low_th;
  return cv::Scalar(255 * depth / th_range, 0, 255 * (1 - depth / th_range));
}

// 像素坐标转相机归一化坐标
Point2f pixel2cam(const Point2d &p, const Mat &K);

int main(int argc, char **argv) {
    
    
/*  if (argc != 3) {
    cout << "usage: triangulation img1 img2" << endl;
    return 1;
  }*/
  //-- 读取图像
  Mat img_1 = imread("../camera1left.png");
  Mat img_2 = imread("../camera1right.png");

  vector<KeyPoint> keypoints_1, keypoints_2;
  vector<DMatch> matches;
  find_feature_matches(img_1, img_2, keypoints_1, keypoints_2, matches);
  cout << "一共找到了" << matches.size() << "组匹配点" << endl;

  //-- 估计两张图像间运动
  Mat R, t;
  pose_estimation_2d2d(keypoints_1, keypoints_2, matches, R, t);

  //-- 三角化
  vector<Point3d> points;
  triangulation(keypoints_1, keypoints_2, matches, R, t, points);

  //-- 验证三角化点与特征点的重投影关系
  Mat K = (Mat_<double>(3, 3) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1);
  Mat img1_plot = img_1.clone();
  Mat img2_plot = img_2.clone();
  for (int i = 0; i < matches.size(); i++) {
    
    
    // 第一个图
    float depth1 = points[i].z;
    cout << "depth: " << depth1 << endl;
    Point2d pt1_cam = pixel2cam(keypoints_1[matches[i].queryIdx].pt, K);
    cv::circle(img1_plot, keypoints_1[matches[i].queryIdx].pt, 2, get_color(depth1), 2);

    // 第二个图
    Mat pt2_trans = R * (Mat_<double>(3, 1) << points[i].x, points[i].y, points[i].z) + t;
    float depth2 = pt2_trans.at<double>(2, 0);
    cv::circle(img2_plot, keypoints_2[matches[i].trainIdx].pt, 2, get_color(depth2), 2);
  }
  cv::imshow("img 1", img1_plot);
  cv::imshow("img 2", img2_plot);
  cv::imwrite("img 1.jpg", img1_plot);
  cv::imwrite("img 2.jpg", img2_plot);
  cv::waitKey();

  return 0;
}

void find_feature_matches(const Mat &img_1, const Mat &img_2,
                          std::vector<KeyPoint> &keypoints_1,
                          std::vector<KeyPoint> &keypoints_2,
                          std::vector<DMatch> &matches) {
    
    
  //-- 初始化
  Mat descriptors_1, descriptors_2;
  // used in OpenCV3
  Ptr<FeatureDetector> detector = ORB::create();
  Ptr<DescriptorExtractor> descriptor = ORB::create();
  // use this if you are in OpenCV2
  // Ptr<FeatureDetector> detector = FeatureDetector::create ( "ORB" );
  // Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create ( "ORB" );
  Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
  //-- 第一步:检测 Oriented FAST 角点位置
  detector->detect(img_1, keypoints_1);
  detector->detect(img_2, keypoints_2);

  //-- 第二步:根据角点位置计算 BRIEF 描述子
  descriptor->compute(img_1, keypoints_1, descriptors_1);
  descriptor->compute(img_2, keypoints_2, descriptors_2);

  //-- 第三步:对两幅图像中的BRIEF描述子进行匹配,使用 Hamming 距离
  vector<DMatch> match;
  // BFMatcher matcher ( NORM_HAMMING );
  matcher->match(descriptors_1, descriptors_2, match);

  //-- 第四步:匹配点对筛选
  double min_dist = 10000, max_dist = 0;

  //找出所有匹配之间的最小距离和最大距离, 即是最相似的和最不相似的两组点之间的距离
  for (int i = 0; i < descriptors_1.rows; i++) {
    
    
    double dist = match[i].distance;
    if (dist < min_dist) min_dist = dist;
    if (dist > max_dist) max_dist = dist;
  }

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

  //当描述子之间的距离大于两倍的最小距离时,即认为匹配有误.但有时候最小距离会非常小,设置一个经验值30作为下限.
  for (int i = 0; i < descriptors_1.rows; i++) {
    
    
    if (match[i].distance <= max(2 * min_dist, 30.0)) {
    
    
      matches.push_back(match[i]);
    }
  }
}

void pose_estimation_2d2d(
  const std::vector<KeyPoint> &keypoints_1,
  const std::vector<KeyPoint> &keypoints_2,
  const std::vector<DMatch> &matches,
  Mat &R, Mat &t) {
    
    
  // 相机内参,TUM Freiburg2
  Mat K = (Mat_<double>(3, 3) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1);

  //-- 把匹配点转换为vector<Point2f>的形式
  vector<Point2f> points1;
  vector<Point2f> points2;

  for (int i = 0; i < (int) matches.size(); i++) {
    
    
    points1.push_back(keypoints_1[matches[i].queryIdx].pt);
    points2.push_back(keypoints_2[matches[i].trainIdx].pt);
  }

  //-- 计算本质矩阵
  Point2d principal_point(325.1, 249.7);        //相机主点, TUM dataset标定值
  int focal_length = 521;            //相机焦距, TUM dataset标定值
  Mat essential_matrix;
  essential_matrix = findEssentialMat(points1, points2, focal_length, principal_point);

  //-- 从本质矩阵中恢复旋转和平移信息.
  recoverPose(essential_matrix, points1, points2, R, t, focal_length, principal_point);
}

void triangulation(
  const vector<KeyPoint> &keypoint_1,
  const vector<KeyPoint> &keypoint_2,
  const std::vector<DMatch> &matches,
  const Mat &R, const Mat &t,
  vector<Point3d> &points) {
    
    
  Mat T1 = (Mat_<float>(3, 4) <<
    1, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, 0);
  Mat T2 = (Mat_<float>(3, 4) <<
    R.at<double>(0, 0), R.at<double>(0, 1), R.at<double>(0, 2), t.at<double>(0, 0),
    R.at<double>(1, 0), R.at<double>(1, 1), R.at<double>(1, 2), t.at<double>(1, 0),
    R.at<double>(2, 0), R.at<double>(2, 1), R.at<double>(2, 2), t.at<double>(2, 0)
  );

  Mat K = (Mat_<double>(3, 3) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1);
  vector<Point2f> pts_1, pts_2;
  for (DMatch m:matches) {
    
    
    // 将像素坐标转换至相机坐标
    pts_1.push_back(pixel2cam(keypoint_1[m.queryIdx].pt, K));
    pts_2.push_back(pixel2cam(keypoint_2[m.trainIdx].pt, K));
  }

  Mat pts_4d;
  cv::triangulatePoints(T1, T2, pts_1, pts_2, pts_4d);

  // 转换成非齐次坐标
  for (int i = 0; i < pts_4d.cols; i++) {
    
    
    Mat x = pts_4d.col(i);
    x /= x.at<float>(3, 0); // 归一化
    Point3d p(
      x.at<float>(0, 0),
      x.at<float>(1, 0),
      x.at<float>(2, 0)
    );
    points.push_back(p);
  }
}

Point2f pixel2cam(const Point2d &p, const Mat &K) {
    
    
  return Point2f
    (
      (p.x - K.at<double>(0, 2)) / K.at<double>(0, 0),
      (p.y - K.at<double>(1, 2)) / K.at<double>(1, 1)
    );
}


输入1
在这里插入图片描述
在这里插入图片描述

输出1
在这里插入图片描述在这里插入图片描述

/home/wenjing/slamook2-master/ch7/cmake-build-debug/triangulation
-- Max dist : 92.000000 
-- Min dist : 7.000000 
一共找到了100组匹配点
depth: 8.45114
depth: 8.11168
depth: 8.18347
depth: 7.96737
depth: 8.96682
depth: 8.38599
depth: 8.1134
depth: 8.7149
depth: 8.3377
depth: 8.31379
depth: 7.96613
depth: 8.58297
depth: 7.25324
depth: 3.06797
depth: 3.09099
depth: 7.23383
depth: 7.78679
depth: 6.51941
depth: 8.33057
depth: 8.18771
depth: 8.04713
depth: 8.09279
depth: 8.5723
depth: 8.87928
depth: 8.86888
depth: 8.88306
depth: 8.87843
depth: 8.49311
depth: 8.1599
depth: 7.70485
depth: 7.08439
depth: 8.15342
depth: 8.7219
depth: 8.95212
depth: 3.01164
depth: 8.24874
depth: 8.19046
depth: 7.9547
depth: 8.00198
depth: 2.57209
depth: 7.29087
depth: 8.36602
depth: 8.46478
depth: 8.23341
depth: 8.07357
depth: 8.15018
depth: 8.90094
depth: 8.62156
depth: 3.19358
depth: 7.76986
depth: 8.40786
depth: 8.78857
depth: 8.83813
depth: 8.26196
depth: 6.97453
depth: 5.50871
depth: 8.18869
depth: 7.86875
depth: 8.14381
depth: 7.1342
depth: 8.76902
depth: 9.10378
depth: 8.8101
depth: 2.86304
depth: 6.25801
depth: 8.73311
depth: 6.5171
depth: 9.09747
depth: 8.81868
depth: 9.97354
depth: 3.12421
depth: 8.45446
depth: 9.37705
depth: 7.54711
depth: 7.06065
depth: 8.67746
depth: 8.1195
depth: 9.11999
depth: 7.85653
depth: 7.52625
depth: 9.96713
depth: 10.3234
depth: 9.34343
depth: 7.64548
depth: 10.6162
depth: 3.65146
depth: 10.1449
depth: 8.35186
depth: 11.7697
depth: 8.77597
depth: 10.8318
depth: 9.04562
depth: 9.25177
depth: 8.61644
depth: 8.95525
depth: 8.2801
depth: 10.2364
depth: 8.49984
depth: 3.07061
depth: 8.42966

Process finished with exit code 143 (interrupted by signal 15: SIGTERM)

输入2
在这里插入图片描述
在这里插入图片描述

输出2
在这里插入图片描述
在这里插入图片描述

/home/wenjing/slambook2-master/ch7/cmake-build-debug/triangulation
-- Max dist : 88.000000 
-- Min dist : 11.000000 
一共找到了141组匹配点
depth: 12.1936
depth: 5.50284
depth: 20.2037
depth: 12.6348
depth: 8.61129
depth: 5.48712
depth: 11.1258
depth: 5.43754
depth: 5.44723
depth: 5.43531
depth: -3.19365
depth: 4.88671
depth: 5.4166
depth: 5.37585
depth: 4.87743
depth: 4.76093
depth: 5.53516
depth: 8.8119
depth: 5.44342
depth: 5.47139
depth: 14.8435
depth: 10.7859
depth: 12.6084
depth: 5.36815
depth: 15.137
depth: 5.54555
depth: 5.41961
depth: 5.41276
depth: 5.42466
depth: 5.45274
depth: 4.93806
depth: 5.41655
depth: 4.76696
depth: 4.72023
depth: 14.9405
depth: 0.0852974
depth: 8.69154
depth: 9.20348
depth: 9.77177
depth: 8.47542
depth: 13.0462
depth: 5.2482
depth: 10.7263
depth: 11.4933
depth: 5.55138
depth: 11.0053
depth: 5.40849
depth: 4.7257
depth: 5.35865
depth: 5.42802
depth: 0.0860801
depth: 9.61575
depth: 4.89135
depth: 5.46959
depth: 5.42094
depth: 4.93644
depth: 13.2084
depth: 5.5964
depth: 4.95895
depth: 8.99151
depth: 19.6422
depth: 8.40703
depth: 9.47849
depth: -6.50991
depth: 13.8304
depth: 11.2885
depth: 5.4374
depth: 13.1997
depth: 0.295601
depth: 5.28469
depth: 5.58023
depth: 9.13569
depth: 5.34983
depth: 27.2414
depth: 5.5266
depth: 4.8699
depth: 5.10326
depth: 4.91887
depth: 21.4711
depth: 5.42183
depth: 4.84163
depth: 9.98929
depth: 8.87488
depth: 14.6109
depth: 5.07263
depth: 12.4797
depth: 5.15581
depth: 13.0149
depth: 4.95909
depth: 10.2903
depth: 8.50943
depth: 5.07552
depth: 18.6864
depth: 12.8977
depth: 4.89074
depth: 14.4791
depth: 4.87003
depth: 5.18218
depth: 24.0619
depth: 10.9178
depth: 5.16989
depth: 10.5014
depth: 5.04621
depth: 11.3929
depth: -1.2595
depth: 10.8301
depth: 4.41393
depth: 4.66484
depth: 4.80303
depth: 4.65841
depth: 4.94726
depth: 4.97852
depth: 5.17086
depth: 4.98965
depth: 5.72032
depth: 12.7146
depth: -1.16323
depth: 8.75494
depth: 8.0708
depth: 11.5101
depth: 14.5543
depth: 5.21805
depth: 5.24226
depth: 14.5061
depth: 5.36566
depth: 5.36726
depth: 5.46921
depth: 5.40974
depth: 5.56659
depth: -1.22461
depth: -1.09033
depth: 20.2956
depth: 4.89184
depth: 8.47339
depth: 9.98813
depth: 14.6491
depth: 5.04147
depth: 5.62211
depth: 4.97778
depth: 12.924
depth: 8.86879

输入3
在这里插入图片描述
在这里插入图片描述

输出3
在这里插入图片描述
在这里插入图片描述

/home/wenjing/slambook2-master/ch7/cmake-build-debug/triangulation
-- Max dist : 95.000000 
-- Min dist : 4.000000 
一共找到了79组匹配点
depth: 45.3724
depth: 16.6993
depth: 14.046
depth: 13.267
depth: 13.6788
depth: 30.0102
depth: 14.6864
depth: 13.8394
depth: 14.408
depth: 15.118
depth: 17.1833
depth: 16.2955
depth: 14.4425
depth: 15.5032
depth: 16.6199
depth: 29.6163
depth: 29.8479
depth: 14.2034
depth: 17.2911
depth: 13.952
depth: 14.7138
depth: 51.8393
depth: 50.3779
depth: 15.2634
depth: 15.1423
depth: 13.5731
depth: 15.2204
depth: 17.2002
depth: 15.1018
depth: 13.1856
depth: 28.1319
depth: 27.8687
depth: 14.8212
depth: 16.2624
depth: 13.3843
depth: 14.8062
depth: 14.6493
depth: 16.8406
depth: 14.6166
depth: 44.9113
depth: 28.6374
depth: 28.7027
depth: 46.6309
depth: 15.6839
depth: 14.9472
depth: 14.5156
depth: 14.0974
depth: 13.5684
depth: 19.3896
depth: 17.501
depth: 29.7291
depth: 15.5625
depth: 53.3157
depth: 16.3022
depth: 12.9899
depth: 17.7342
depth: 18.2738
depth: 17.8923
depth: 16.5829
depth: 15.7578
depth: 17.9717
depth: 18.049
depth: 14.3796
depth: 10.4467
depth: 11.2243
depth: 13.9109
depth: 16.0181
depth: 22.9477
depth: 15.9597
depth: 10.2454
depth: 9.95815
depth: 16.3941
depth: 14.9895
depth: 18.6442
depth: 18.0098
depth: 11.627
depth: 16.2583
depth: 13.4797
depth: 17.2989

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_43297891/article/details/114402025