日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

视觉十四讲:第七讲_3D-2D:P3P

發(fā)布時(shí)間:2024/5/24 综合教程 40 生活家
生活随笔 收集整理的這篇文章主要介紹了 视觉十四讲:第七讲_3D-2D:P3P 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.P3P

P3P輸入數(shù)據(jù)為三對(duì)3D-2D的匹配點(diǎn),一個(gè)單目相機(jī),經(jīng)過(guò)初始化,得到初始的3D點(diǎn),就可以依次得到后續(xù)的姿態(tài)和3D點(diǎn)。

ABC是上一時(shí)刻求的的3D點(diǎn), abc是與上一次時(shí)刻的匹配點(diǎn)。利用相似原理,可求出abc在相機(jī)坐標(biāo)下的3D坐標(biāo),最后就可以把問(wèn)題轉(zhuǎn)換為3D-3D坐標(biāo)的估計(jì)問(wèn)題。

問(wèn)題:只利用3個(gè)點(diǎn),不能運(yùn)用多余的信息;受噪聲影響,存在無(wú)匹配,容易算法失敗

通常做法是用P3P估計(jì)出相機(jī)位姿,然后再構(gòu)建最小二乘優(yōu)化問(wèn)題對(duì)估計(jì)值進(jìn)行優(yōu)化調(diào)整(Bundle Adjustment)

2.使用Pnp來(lái)求解

int main(int argc, char **argv) {
  if (argc != 5) {
    cout << "usage: pose_estimation_3d2d img1 img2 depth1 depth2" << endl;
    return 1;
  }
  //-- 讀取圖像
  Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
  Mat img_2 = imread(argv[2], CV_LOAD_IMAGE_COLOR);
  assert(img_1.data && img_2.data && "Can not load images!");

  vector<KeyPoint> keypoints_1, keypoints_2;
  vector<DMatch> matches;
  //獲取匹配點(diǎn)
  find_feature_matches(img_1, img_2, keypoints_1, keypoints_2, matches); 
  cout << "一共找到了" << matches.size() << "組匹配點(diǎn)" << endl;

  // 建立3D點(diǎn)
  Mat d1 = imread(argv[3], CV_LOAD_IMAGE_UNCHANGED);       // 第一張圖的深度圖為16位無(wú)符號(hào)數(shù),單通道圖像
  Mat K = (Mat_<double>(3, 3) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1);
  vector<Point3f> pts_3d;
  vector<Point2f> pts_2d;
  for (DMatch m:matches) {
	//取出第一張圖匹配點(diǎn)的深度數(shù)據(jù)
    //Mat.ptr<>(行)[列]
    ushort d = d1.ptr<unsigned short>(int(keypoints_1[m.queryIdx].pt.y))[int(keypoints_1[m.queryIdx].pt.x)];
    if (d == 0)   // bad depth
      continue;
    float dd = d / 5000.0;
	//將圖1的匹配點(diǎn)的像素坐標(biāo)轉(zhuǎn)相機(jī)歸一化坐標(biāo)
    Point2d p1 = pixel2cam(keypoints_1[m.queryIdx].pt, K);
	//轉(zhuǎn)換為相機(jī)坐標(biāo)的3D點(diǎn)
    pts_3d.push_back(Point3f(p1.x * dd, p1.y * dd, dd));
	//獲取圖2的匹配點(diǎn)的像素坐標(biāo)
    pts_2d.push_back(keypoints_2[m.trainIdx].pt);
  }

  cout << "3d-2d pairs: " << pts_3d.size() << endl;

  /**************pnp**************/
  chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  Mat r, t;
  solvePnP(pts_3d, pts_2d, K, Mat(), r, t, false); // 調(diào)用OpenCV 的 PnP 求解,可選擇EPNP,DLS等方法
  Mat R;
  cv::Rodrigues(r, R); // r為旋轉(zhuǎn)向量形式,用Rodrigues公式轉(zhuǎn)換為矩陣
  chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
  chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  cout << "solve pnp in opencv cost time: " << time_used.count() << " seconds." << endl;

  cout << "R=" << endl << R << endl;
  cout << "t=" << endl << t << endl;
}

3.使用高斯牛頓法來(lái)優(yōu)化求解

1.構(gòu)建最小二乘問(wèn)題:

(T^{*}=argminfrac{1}{2} displaystyle sum^{n}_{i=1}||u_{i}-frac{1}{s_{i}}KTP_{i}||^{2}_{2})
誤差是觀測(cè)像素點(diǎn)-投影像素點(diǎn),稱(chēng)為重投影誤差

使用高斯牛頓法的重點(diǎn)在于求誤差項(xiàng)對(duì)于每個(gè)優(yōu)化變量的導(dǎo)數(shù)
線性化:(e(x+Delta x) approx e(x) + J^{T}Delta x)

高斯的增量方程為: ((displaystyle sum^{100}_{i=1} J_{i}(sigma^{2})^{-1} J_{i}^{T})Delta x_{k}=displaystyle sum^{100}_{i=1} -J_{i}(sigma^{2})^{-1} e_{i})
(HDelta x_{k}=b)

2.求雅可比矩陣:

1.使用非齊次坐標(biāo),像素誤差e是2維,x為相機(jī)位姿是6維,(J^{T})是一個(gè)2*6的矩陣。
2.將P變換到相機(jī)坐標(biāo)下為(P^{'}=[X^{'},Y^{'},Z^{'}]^{T}),則:(su=KP^{'})
3.消去s得:(u=f_{x}frac{X^{'}}{Z^{'}}+c_{x}) (v=f_{y}frac{Y^{'}}{Z^{'}}+c_{y})
4.對(duì)T左乘擾動(dòng)量(delta xi),考慮e的變化關(guān)于擾動(dòng)量的導(dǎo)數(shù)。則(frac{partial e}{partial delta xi}= frac{partial e}{partial P^{'}} frac{partial P^{'}}{partial delta xi})
5.容易得出(frac{partial e}{partial P^{'}}) = (-left[ egin{matrix} frac{f_{x}}{Z^{'}} & 0 & -frac{f_{x}X^{'}}{Z^{'2}} \ 0 & frac{f_{y}}{Z^{'}} & -frac{f_{y}Y^{'}}{Z^{'2}} end{matrix} ight])
6.由李代數(shù)導(dǎo)數(shù)得:(frac{partial (TP)}{partial delta xi} = left[ egin{matrix} I & -P^{' Lambda} \ 0^{T} & 0^{T} end{matrix} ight])
7.在(P^{'})的定義中,取了前三維,所以(frac{partial P^{'}}{partial delta xi} = left[ egin{matrix} I & -P^{' Lambda} end{matrix} ight])
8.將兩個(gè)式子相乘就可以得到雅可比矩陣:
(frac{partial e}{partial delta xi} = - left[ egin{matrix} frac{f_{x}}{Z^{'}} & 0 & -frac{f_{x}X^{'}}{Z^{'2}} & -frac{f_{x}X^{'}Y^{'}}{Z^{'2}} & f_{x} + frac{f_{x}X^{'2}}{Z^{'2}} &- frac{f_{x}Y^{'}}{Z^{'}} \ 0 & frac{f_{y}}{Z^{'}} & -frac{f_{y}Y^{'}}{Z^{'2}} & -f_{y} - frac{f_{y}Y^{'2}}{Z^{'2}} & frac{f_{y}X^{'}Y^{'}}{Z^{'2}} & frac{f_{y}X^{'}}{Z^{'}} end{matrix} ight])

3.程序:
void bundleAdjustmentGaussNewton(
  const VecVector3d &points_3d,
  const VecVector2d &points_2d,
  const Mat &K,
  Sophus::SE3d &pose) {
  typedef Eigen::Matrix<double, 6, 1> Vector6d;
  const int iterations = 10;
  double cost = 0, lastCost = 0;
  double fx = K.at<double>(0, 0);
  double fy = K.at<double>(1, 1);
  double cx = K.at<double>(0, 2);
  double cy = K.at<double>(1, 2);

  for (int iter = 0; iter < iterations; iter++) {
    Eigen::Matrix<double, 6, 6> H = Eigen::Matrix<double, 6, 6>::Zero();
    Vector6d b = Vector6d::Zero();

    cost = 0;
    // compute cost
    for (int i = 0; i < points_3d.size(); i++) {
	  //世界坐標(biāo)轉(zhuǎn)為相機(jī)坐標(biāo)
      Eigen::Vector3d pc = pose * points_3d[i];
      double inv_z = 1.0 / pc[2];
      double inv_z2 = inv_z * inv_z;
	  //相機(jī)坐標(biāo)轉(zhuǎn)為像素坐標(biāo)
      Eigen::Vector2d proj(fx * pc[0] / pc[2] + cx, fy * pc[1] / pc[2] + cy);

      Eigen::Vector2d e = points_2d[i] - proj;  //誤差,觀測(cè)值-預(yù)測(cè)值,反之取負(fù)

      cost += e.squaredNorm();  //誤差里的每項(xiàng)平方和
      Eigen::Matrix<double, 2, 6> J;
	  //雅可比矩陣賦值
      J << -fx * inv_z,
        0,
        fx * pc[0] * inv_z2,
        fx * pc[0] * pc[1] * inv_z2,
        -fx - fx * pc[0] * pc[0] * inv_z2,
        fx * pc[1] * inv_z,
        0,
        -fy * inv_z,
        fy * pc[1] * inv_z2,
        fy + fy * pc[1] * pc[1] * inv_z2,
        -fy * pc[0] * pc[1] * inv_z2,
        -fy * pc[0] * inv_z;

      H += J.transpose() * J;
      b += -J.transpose() * e;
    }

    Vector6d dx;
    dx = H.ldlt().solve(b);

    if (isnan(dx[0])) {
      cout << "result is nan!" << endl;
      break;
    }

    if (iter > 0 && cost >= lastCost) {
      // cost increase, update is not good
      cout << "cost: " << cost << ", last cost: " << lastCost << endl;
      break;
    }

    // update your estimation
	//更新位姿
    pose = Sophus::SE3d::exp(dx) * pose;
    lastCost = cost;

    cout << "iteration " << iter << " cost=" << std::setprecision(12) << cost << endl;
  //范數(shù),誤差足夠小
    if (dx.norm() < 1e-6) {
      // converge
      break;
    }
  }

  cout << "pose by g-n: 
" << pose.matrix() << endl;
}

4.使用g2o來(lái)進(jìn)行BA優(yōu)化

節(jié)點(diǎn)(待優(yōu)化的變量): 第二個(gè)相機(jī)的位姿 (T in SE(3))
邊(誤差): 每個(gè)3D在第二個(gè)相機(jī)中的投影

1.構(gòu)建節(jié)點(diǎn)和邊框架:
// 曲線模型的頂點(diǎn),模板參數(shù):優(yōu)化變量維度和數(shù)據(jù)類(lèi)型
class VertexPose : public g2o::BaseVertex<6, Sophus::SE3d> {
public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW;

  // 初始化
  virtual void setToOriginImpl() override {
    _estimate = Sophus::SE3d();
  }

  //更新估計(jì)值
  virtual void oplusImpl(const double *update) override {
    Eigen::Matrix<double, 6, 1> update_eigen;
    update_eigen << update[0], update[1], update[2], update[3], update[4], update[5];
    _estimate = Sophus::SE3d::exp(update_eigen) * _estimate;
  }

  virtual bool read(istream &in) override {}

  virtual bool write(ostream &out) const override {}
};

// 誤差模型 模板參數(shù):觀測(cè)值維度,類(lèi)型,連接頂點(diǎn)類(lèi)型
class EdgeProjection : public g2o::BaseUnaryEdge<2, Eigen::Vector2d, VertexPose> {
public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
  //輸入的變量:位姿,相機(jī)內(nèi)參
  EdgeProjection(const Eigen::Vector3d &pos, const Eigen::Matrix3d &K) : _pos3d(pos), _K(K) {}

  //計(jì)算誤差
  virtual void computeError() override {
    const VertexPose *v = static_cast<VertexPose *> (_vertices[0]);
    //獲取估計(jì)值
    Sophus::SE3d T = v->estimate();
    //將3D世界坐標(biāo)轉(zhuǎn)為相機(jī)像素坐標(biāo)
    Eigen::Vector3d pos_pixel = _K * (T * _pos3d);
    //歸一化
    pos_pixel /= pos_pixel[2];
    //計(jì)算誤差
    _error = _measurement - pos_pixel.head<2>();
  }

  //計(jì)算雅可比矩陣,公式上面已推導(dǎo)
  virtual void linearizeOplus() override {
    const VertexPose *v = static_cast<VertexPose *> (_vertices[0]);
    Sophus::SE3d T = v->estimate();
    //世界坐標(biāo)轉(zhuǎn)化為相機(jī)坐標(biāo)
    Eigen::Vector3d pos_cam = T * _pos3d;
    //獲取相機(jī)內(nèi)參
    double fx = _K(0, 0);
    double fy = _K(1, 1);
    double cx = _K(0, 2);
    double cy = _K(1, 2);
    double X = pos_cam[0];
    double Y = pos_cam[1];
    double Z = pos_cam[2];
    double Z2 = Z * Z;
    //傳入雅可比矩陣參數(shù)
    _jacobianOplusXi
      << -fx / Z, 0, fx * X / Z2, fx * X * Y / Z2, -fx - fx * X * X / Z2, fx * Y / Z,
      0, -fy / Z, fy * Y / (Z * Z), fy + fy * Y * Y / Z2, -fy * X * Y / Z2, -fy * X / Z;
  }

  virtual bool read(istream &in) override {}

  virtual bool write(ostream &out) const override {}

private:
  Eigen::Vector3d _pos3d;
  Eigen::Matrix3d _K;
};
2.組成圖優(yōu)化:
void bundleAdjustmentG2O(
  const VecVector3d &points_3d,
  const VecVector2d &points_2d,
  const Mat &K,
  Sophus::SE3d &pose) {

  // 構(gòu)建圖優(yōu)化,先設(shè)定g2o
  typedef g2o::BlockSolver<g2o::BlockSolverTraits<6, 3>> BlockSolverType;  // 位姿維度為6,誤差維度為3
  typedef g2o::LinearSolverDense<BlockSolverType::PoseMatrixType> LinearSolverType; // 線性求解器類(lèi)型
  // 梯度下降方法,可以從GN, LM, DogLeg 中選
  auto solver = new g2o::OptimizationAlgorithmGaussNewton(
    g2o::make_unique<BlockSolverType>(g2o::make_unique<LinearSolverType>()));
  g2o::SparseOptimizer optimizer;     // 圖模型
  optimizer.setAlgorithm(solver);   // 設(shè)置求解器
  optimizer.setVerbose(true);       // 打開(kāi)調(diào)試輸出

  // 往圖中添加節(jié)點(diǎn)
  VertexPose *vertex_pose = new VertexPose(); // camera vertex_pose
  vertex_pose->setId(0);
  vertex_pose->setEstimate(Sophus::SE3d());
  optimizer.addVertex(vertex_pose);

  // K 相機(jī)內(nèi)參
  Eigen::Matrix3d K_eigen;
  K_eigen <<
          K.at<double>(0, 0), K.at<double>(0, 1), K.at<double>(0, 2),
    K.at<double>(1, 0), K.at<double>(1, 1), K.at<double>(1, 2),
    K.at<double>(2, 0), K.at<double>(2, 1), K.at<double>(2, 2);

  // edges 邊
  int index = 1;
  for (size_t i = 0; i < points_2d.size(); ++i) {
    auto p2d = points_2d[i];
    auto p3d = points_3d[i];
    EdgeProjection *edge = new EdgeProjection(p3d, K_eigen);
    edge->setId(index);
    edge->setVertex(0, vertex_pose); // 設(shè)置連接的頂點(diǎn)
    edge->setMeasurement(p2d);  //傳入觀測(cè)值
    edge->setInformation(Eigen::Matrix2d::Identity()); // 信息矩陣
    optimizer.addEdge(edge);
    index++;
  }

  //執(zhí)行優(yōu)化
  chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  optimizer.setVerbose(true);
  optimizer.initializeOptimization();
  optimizer.optimize(10);
  chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
  chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  cout << "optimization costs time: " << time_used.count() << " seconds." << endl;
  //獲取結(jié)果
  cout << "pose estimated by g2o =
" << vertex_pose->estimate().matrix() << endl;
  pose = vertex_pose->estimate();
}

5.完整代碼

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <Eigen/Core>
#include <g2o/core/base_vertex.h>
#include <g2o/core/base_unary_edge.h>
#include <g2o/core/sparse_optimizer.h>
#include <g2o/core/block_solver.h>
#include <g2o/core/solver.h>
#include <g2o/core/optimization_algorithm_gauss_newton.h>
#include <g2o/solvers/dense/linear_solver_dense.h>
#include <sophus/se3.hpp>
#include <chrono>

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);

// 像素坐標(biāo)轉(zhuǎn)相機(jī)歸一化坐標(biāo)
Point2d pixel2cam(const Point2d &p, const Mat &K);

// BA by g2o
//STL容器中的元素是Eigen的數(shù)據(jù)結(jié)構(gòu),例如這里定義一個(gè)vector容器,元素是Vector2d 
typedef vector<Eigen::Vector2d, Eigen::aligned_allocator<Eigen::Vector2d>> VecVector2d;
typedef vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d>> VecVector3d;

void bundleAdjustmentG2O(
  const VecVector3d &points_3d,
  const VecVector2d &points_2d,
  const Mat &K,
  Sophus::SE3d &pose
);

// BA by gauss-newton
void bundleAdjustmentGaussNewton(
  const VecVector3d &points_3d,
  const VecVector2d &points_2d,
  const Mat &K,
  Sophus::SE3d &pose
);

int main(int argc, char **argv) {
  if (argc != 5) {
    cout << "usage: pose_estimation_3d2d img1 img2 depth1 depth2" << endl;
    return 1;
  }
  //-- 讀取圖像
  Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
  Mat img_2 = imread(argv[2], CV_LOAD_IMAGE_COLOR);
  assert(img_1.data && img_2.data && "Can not load images!");

  vector<KeyPoint> keypoints_1, keypoints_2;
  vector<DMatch> matches;
  //獲取匹配點(diǎn)
  find_feature_matches(img_1, img_2, keypoints_1, keypoints_2, matches); 
  cout << "一共找到了" << matches.size() << "組匹配點(diǎn)" << endl;

  // 建立3D點(diǎn)
  Mat d1 = imread(argv[3], CV_LOAD_IMAGE_UNCHANGED);       // 第一張圖的深度圖為16位無(wú)符號(hào)數(shù),單通道圖像
  Mat K = (Mat_<double>(3, 3) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1);
  vector<Point3f> pts_3d;
  vector<Point2f> pts_2d;
  for (DMatch m:matches) {
	//取出第一張圖匹配點(diǎn)的深度數(shù)據(jù)
    ushort d = d1.ptr<unsigned short>(int(keypoints_1[m.queryIdx].pt.y))[int(keypoints_1[m.queryIdx].pt.x)];
    if (d == 0)   // bad depth
      continue;
    float dd = d / 5000.0;
	//將圖1的匹配點(diǎn)的像素坐標(biāo)轉(zhuǎn)相機(jī)歸一化坐標(biāo)
    Point2d p1 = pixel2cam(keypoints_1[m.queryIdx].pt, K);
	//轉(zhuǎn)換為相機(jī)坐標(biāo)的3D點(diǎn)
    pts_3d.push_back(Point3f(p1.x * dd, p1.y * dd, dd));
	//獲取圖2的匹配點(diǎn)的像素坐標(biāo)
    pts_2d.push_back(keypoints_2[m.trainIdx].pt);
  }

  cout << "3d-2d pairs: " << pts_3d.size() << endl;

  /**************pnp**************/
  chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  Mat r, t;
  solvePnP(pts_3d, pts_2d, K, Mat(), r, t, false); // 調(diào)用OpenCV 的 PnP 求解,可選擇EPNP,DLS等方法
  Mat R;
  cv::Rodrigues(r, R); // r為旋轉(zhuǎn)向量形式,用Rodrigues公式轉(zhuǎn)換為矩陣
  chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
  chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  cout << "solve pnp in opencv cost time: " << time_used.count() << " seconds." << endl;

  cout << "R=" << endl << R << endl;
  cout << "t=" << endl << t << endl;
  /********************************/
  

  VecVector3d pts_3d_eigen;
  VecVector2d pts_2d_eigen;
  //
  for (size_t i = 0; i < pts_3d.size(); ++i) {
	//圖1的3D點(diǎn)
    pts_3d_eigen.push_back(Eigen::Vector3d(pts_3d[i].x, pts_3d[i].y, pts_3d[i].z));
	//圖2的2D點(diǎn)
    pts_2d_eigen.push_back(Eigen::Vector2d(pts_2d[i].x, pts_2d[i].y));
  }

  //高斯牛頓法優(yōu)化
  cout << "calling bundle adjustment by gauss newton" << endl;
  Sophus::SE3d pose_gn;
  t1 = chrono::steady_clock::now();
  bundleAdjustmentGaussNewton(pts_3d_eigen, pts_2d_eigen, K, pose_gn);
  t2 = chrono::steady_clock::now();
  time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  cout << "solve pnp by gauss newton cost time: " << time_used.count() << " seconds." << endl;

  //g2o優(yōu)化
  cout << "calling bundle adjustment by g2o" << endl;
  Sophus::SE3d pose_g2o;
  t1 = chrono::steady_clock::now();
  bundleAdjustmentG2O(pts_3d_eigen, pts_2d_eigen, K, pose_g2o);
  t2 = chrono::steady_clock::now();
  time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  cout << "solve pnp by g2o cost time: " << time_used.count() << " seconds." << endl;
  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");
  //-- 第一步:檢測(cè) Oriented FAST 角點(diǎn)位置
  detector->detect(img_1, keypoints_1);
  detector->detect(img_2, keypoints_2);

  //-- 第二步:根據(jù)角點(diǎn)位置計(jì)算 BRIEF 描述子
  descriptor->compute(img_1, keypoints_1, descriptors_1);
  descriptor->compute(img_2, keypoints_2, descriptors_2);

  //-- 第三步:對(duì)兩幅圖像中的BRIEF描述子進(jìn)行匹配,使用 Hamming 距離
  vector<DMatch> match;
  // BFMatcher matcher ( NORM_HAMMING );
  matcher->match(descriptors_1, descriptors_2, match);

  //-- 第四步:匹配點(diǎn)對(duì)篩選
  double min_dist = 10000, max_dist = 0;

  //找出所有匹配之間的最小距離和最大距離, 即是最相似的和最不相似的兩組點(diǎn)之間的距離
  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 
", max_dist);
  printf("-- Min dist : %f 
", min_dist);

  //當(dāng)描述子之間的距離大于兩倍的最小距離時(shí),即認(rèn)為匹配有誤.但有時(shí)候最小距離會(huì)非常小,設(shè)置一個(gè)經(jīng)驗(yàn)值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]);
    }
  }
}

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

void bundleAdjustmentGaussNewton(
  const VecVector3d &points_3d,
  const VecVector2d &points_2d,
  const Mat &K,
  Sophus::SE3d &pose) {
  typedef Eigen::Matrix<double, 6, 1> Vector6d;
  const int iterations = 10;
  double cost = 0, lastCost = 0;
  double fx = K.at<double>(0, 0);
  double fy = K.at<double>(1, 1);
  double cx = K.at<double>(0, 2);
  double cy = K.at<double>(1, 2);

  for (int iter = 0; iter < iterations; iter++) {
    Eigen::Matrix<double, 6, 6> H = Eigen::Matrix<double, 6, 6>::Zero();
    Vector6d b = Vector6d::Zero();

    cost = 0;
    // compute cost
    for (int i = 0; i < points_3d.size(); i++) {
	  //世界坐標(biāo)轉(zhuǎn)為相機(jī)坐標(biāo)
      Eigen::Vector3d pc = pose * points_3d[i];
      double inv_z = 1.0 / pc[2];
      double inv_z2 = inv_z * inv_z;
	  //相機(jī)坐標(biāo)轉(zhuǎn)為像素坐標(biāo)
      Eigen::Vector2d proj(fx * pc[0] / pc[2] + cx, fy * pc[1] / pc[2] + cy);

      Eigen::Vector2d e = points_2d[i] - proj;  //誤差,觀測(cè)值-預(yù)測(cè)值,反之取負(fù)

      cost += e.squaredNorm();  //誤差里的每項(xiàng)平方和
      Eigen::Matrix<double, 2, 6> J;
	  //雅可比矩陣賦值
      J << -fx * inv_z,
        0,
        fx * pc[0] * inv_z2,
        fx * pc[0] * pc[1] * inv_z2,
        -fx - fx * pc[0] * pc[0] * inv_z2,
        fx * pc[1] * inv_z,
        0,
        -fy * inv_z,
        fy * pc[1] * inv_z2,
        fy + fy * pc[1] * pc[1] * inv_z2,
        -fy * pc[0] * pc[1] * inv_z2,
        -fy * pc[0] * inv_z;

      H += J.transpose() * J;
      b += -J.transpose() * e;
    }

    Vector6d dx;
    dx = H.ldlt().solve(b);

    if (isnan(dx[0])) {
      cout << "result is nan!" << endl;
      break;
    }

    if (iter > 0 && cost >= lastCost) {
      // cost increase, update is not good
      cout << "cost: " << cost << ", last cost: " << lastCost << endl;
      break;
    }

    // update your estimation
	//更新位姿
    pose = Sophus::SE3d::exp(dx) * pose;
    lastCost = cost;

    cout << "iteration " << iter << " cost=" << std::setprecision(12) << cost << endl;
    if (dx.norm() < 1e-6) {
      // converge
      break;
    }
  }

  cout << "pose by g-n: 
" << pose.matrix() << endl;
}

/// vertex and edges used in g2o ba
class VertexPose : public g2o::BaseVertex<6, Sophus::SE3d> {
public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW;

  virtual void setToOriginImpl() override {
    _estimate = Sophus::SE3d();
  }

  /// left multiplication on SE3
  virtual void oplusImpl(const double *update) override {
    Eigen::Matrix<double, 6, 1> update_eigen;
    update_eigen << update[0], update[1], update[2], update[3], update[4], update[5];
    _estimate = Sophus::SE3d::exp(update_eigen) * _estimate;
  }

  virtual bool read(istream &in) override {}

  virtual bool write(ostream &out) const override {}
};

class EdgeProjection : public g2o::BaseUnaryEdge<2, Eigen::Vector2d, VertexPose> {
public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW;

  EdgeProjection(const Eigen::Vector3d &pos, const Eigen::Matrix3d &K) : _pos3d(pos), _K(K) {}

  virtual void computeError() override {
    const VertexPose *v = static_cast<VertexPose *> (_vertices[0]);
    Sophus::SE3d T = v->estimate();
    Eigen::Vector3d pos_pixel = _K * (T * _pos3d);
    pos_pixel /= pos_pixel[2];
    _error = _measurement - pos_pixel.head<2>();
  }

  virtual void linearizeOplus() override {
    const VertexPose *v = static_cast<VertexPose *> (_vertices[0]);
    Sophus::SE3d T = v->estimate();
    Eigen::Vector3d pos_cam = T * _pos3d;
    double fx = _K(0, 0);
    double fy = _K(1, 1);
    double cx = _K(0, 2);
    double cy = _K(1, 2);
    double X = pos_cam[0];
    double Y = pos_cam[1];
    double Z = pos_cam[2];
    double Z2 = Z * Z;
    _jacobianOplusXi
      << -fx / Z, 0, fx * X / Z2, fx * X * Y / Z2, -fx - fx * X * X / Z2, fx * Y / Z,
      0, -fy / Z, fy * Y / (Z * Z), fy + fy * Y * Y / Z2, -fy * X * Y / Z2, -fy * X / Z;
  }

  virtual bool read(istream &in) override {}

  virtual bool write(ostream &out) const override {}

private:
  Eigen::Vector3d _pos3d;
  Eigen::Matrix3d _K;
};

void bundleAdjustmentG2O(
  const VecVector3d &points_3d,
  const VecVector2d &points_2d,
  const Mat &K,
  Sophus::SE3d &pose) {

  // 構(gòu)建圖優(yōu)化,先設(shè)定g2o
  typedef g2o::BlockSolver<g2o::BlockSolverTraits<6, 3>> BlockSolverType;  // pose is 6, landmark is 3
  typedef g2o::LinearSolverDense<BlockSolverType::PoseMatrixType> LinearSolverType; // 線性求解器類(lèi)型
  // 梯度下降方法,可以從GN, LM, DogLeg 中選
  auto solver = new g2o::OptimizationAlgorithmGaussNewton(
    g2o::make_unique<BlockSolverType>(g2o::make_unique<LinearSolverType>()));
  g2o::SparseOptimizer optimizer;     // 圖模型
  optimizer.setAlgorithm(solver);   // 設(shè)置求解器
  optimizer.setVerbose(true);       // 打開(kāi)調(diào)試輸出

  // vertex
  VertexPose *vertex_pose = new VertexPose(); // camera vertex_pose
  vertex_pose->setId(0);
  vertex_pose->setEstimate(Sophus::SE3d());
  optimizer.addVertex(vertex_pose);

  // K
  Eigen::Matrix3d K_eigen;
  K_eigen <<
          K.at<double>(0, 0), K.at<double>(0, 1), K.at<double>(0, 2),
    K.at<double>(1, 0), K.at<double>(1, 1), K.at<double>(1, 2),
    K.at<double>(2, 0), K.at<double>(2, 1), K.at<double>(2, 2);

  // edges
  int index = 1;
  for (size_t i = 0; i < points_2d.size(); ++i) {
    auto p2d = points_2d[i];
    auto p3d = points_3d[i];
    EdgeProjection *edge = new EdgeProjection(p3d, K_eigen);
    edge->setId(index);
    edge->setVertex(0, vertex_pose);
    edge->setMeasurement(p2d);
    edge->setInformation(Eigen::Matrix2d::Identity());
    optimizer.addEdge(edge);
    index++;
  }

  chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  optimizer.setVerbose(true);
  optimizer.initializeOptimization();
  optimizer.optimize(10);
  chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
  chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  cout << "optimization costs time: " << time_used.count() << " seconds." << endl;
  cout << "pose estimated by g2o =
" << vertex_pose->estimate().matrix() << endl;
  pose = vertex_pose->estimate();
}

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(3d2d)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include_directories(inc)
aux_source_directory(src DIR_SRCS)
SET(SOUR_FILE ${DIR_SRCS})
find_package(OpenCV 3 REQUIRED)
find_package(G2O REQUIRED)
find_package(Sophus REQUIRED)

include_directories(
        ${OpenCV_INCLUDE_DIRS}
        ${G2O_INCLUDE_DIRS}
        ${Sophus_INCLUDE_DIRS}
        "/usr/include/eigen3/"
)


add_executable(3d2d ${SOUR_FILE})
target_link_libraries(3d2d ${OpenCV_LIBS})
target_link_libraries(3d2d g2o_core g2o_stuff)

cmake文件夾:FindG2O.cmake

# Find the header files

FIND_PATH(G2O_INCLUDE_DIR g2o/core/base_vertex.h
  ${G2O_ROOT}/include
  $ENV{G2O_ROOT}/include
  $ENV{G2O_ROOT}
  /usr/local/include
  /usr/include
  /opt/local/include
  /sw/local/include
  /sw/include
  NO_DEFAULT_PATH
  )

# Macro to unify finding both the debug and release versions of the
# libraries; this is adapted from the OpenSceneGraph FIND_LIBRARY
# macro.

MACRO(FIND_G2O_LIBRARY MYLIBRARY MYLIBRARYNAME)

  FIND_LIBRARY("${MYLIBRARY}_DEBUG"
    NAMES "g2o_${MYLIBRARYNAME}_d"
    PATHS
    ${G2O_ROOT}/lib/Debug
    ${G2O_ROOT}/lib
    $ENV{G2O_ROOT}/lib/Debug
    $ENV{G2O_ROOT}/lib
    NO_DEFAULT_PATH
    )

  FIND_LIBRARY("${MYLIBRARY}_DEBUG"
    NAMES "g2o_${MYLIBRARYNAME}_d"
    PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local/lib
    /usr/local/lib64
    /usr/lib
    /usr/lib64
    /opt/local/lib
    /sw/local/lib
    /sw/lib
    )
  
  FIND_LIBRARY(${MYLIBRARY}
    NAMES "g2o_${MYLIBRARYNAME}"
    PATHS
    ${G2O_ROOT}/lib/Release
    ${G2O_ROOT}/lib
    $ENV{G2O_ROOT}/lib/Release
    $ENV{G2O_ROOT}/lib
    NO_DEFAULT_PATH
    )

  FIND_LIBRARY(${MYLIBRARY}
    NAMES "g2o_${MYLIBRARYNAME}"
    PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local/lib
    /usr/local/lib64
    /usr/lib
    /usr/lib64
    /opt/local/lib
    /sw/local/lib
    /sw/lib
    )
  
  IF(NOT ${MYLIBRARY}_DEBUG)
    IF(MYLIBRARY)
      SET(${MYLIBRARY}_DEBUG ${MYLIBRARY})
    ENDIF(MYLIBRARY)
  ENDIF( NOT ${MYLIBRARY}_DEBUG)
  
ENDMACRO(FIND_G2O_LIBRARY LIBRARY LIBRARYNAME)

# Find the core elements
FIND_G2O_LIBRARY(G2O_STUFF_LIBRARY stuff)
FIND_G2O_LIBRARY(G2O_CORE_LIBRARY core)

# Find the CLI library
FIND_G2O_LIBRARY(G2O_CLI_LIBRARY cli)

# Find the pluggable solvers
FIND_G2O_LIBRARY(G2O_SOLVER_CHOLMOD solver_cholmod)
FIND_G2O_LIBRARY(G2O_SOLVER_CSPARSE solver_csparse)
FIND_G2O_LIBRARY(G2O_SOLVER_CSPARSE_EXTENSION csparse_extension)
FIND_G2O_LIBRARY(G2O_SOLVER_DENSE solver_dense)
FIND_G2O_LIBRARY(G2O_SOLVER_PCG solver_pcg)
FIND_G2O_LIBRARY(G2O_SOLVER_SLAM2D_LINEAR solver_slam2d_linear)
FIND_G2O_LIBRARY(G2O_SOLVER_STRUCTURE_ONLY solver_structure_only)
FIND_G2O_LIBRARY(G2O_SOLVER_EIGEN solver_eigen)

# Find the predefined types
FIND_G2O_LIBRARY(G2O_TYPES_DATA types_data)
FIND_G2O_LIBRARY(G2O_TYPES_ICP types_icp)
FIND_G2O_LIBRARY(G2O_TYPES_SBA types_sba)
FIND_G2O_LIBRARY(G2O_TYPES_SCLAM2D types_sclam2d)
FIND_G2O_LIBRARY(G2O_TYPES_SIM3 types_sim3)
FIND_G2O_LIBRARY(G2O_TYPES_SLAM2D types_slam2d)
FIND_G2O_LIBRARY(G2O_TYPES_SLAM3D types_slam3d)

# G2O solvers declared found if we found at least one solver
SET(G2O_SOLVERS_FOUND "NO")
IF(G2O_SOLVER_CHOLMOD OR G2O_SOLVER_CSPARSE OR G2O_SOLVER_DENSE OR G2O_SOLVER_PCG OR G2O_SOLVER_SLAM2D_LINEAR OR G2O_SOLVER_STRUCTURE_ONLY OR G2O_SOLVER_EIGEN)
  SET(G2O_SOLVERS_FOUND "YES")
ENDIF(G2O_SOLVER_CHOLMOD OR G2O_SOLVER_CSPARSE OR G2O_SOLVER_DENSE OR G2O_SOLVER_PCG OR G2O_SOLVER_SLAM2D_LINEAR OR G2O_SOLVER_STRUCTURE_ONLY OR G2O_SOLVER_EIGEN)

# G2O itself declared found if we found the core libraries and at least one solver
SET(G2O_FOUND "NO")
IF(G2O_STUFF_LIBRARY AND G2O_CORE_LIBRARY AND G2O_INCLUDE_DIR AND G2O_SOLVERS_FOUND)
  SET(G2O_FOUND "YES")
ENDIF(G2O_STUFF_LIBRARY AND G2O_CORE_LIBRARY AND G2O_INCLUDE_DIR AND G2O_SOLVERS_FOUND)

總結(jié)

以上是生活随笔為你收集整理的视觉十四讲:第七讲_3D-2D:P3P的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。