视觉slam十四讲第七章课后习题7

时间:2023-03-10 00:34:38
视觉slam十四讲第七章课后习题7

版权声明:本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/newneul/p/8544369.html

 7、题目要求:在ICP程序中,将空间点也作为优化变量考虑进来,程序应该如何书写?最后结果会有何变化?

  分析:在ICP例程中,本书使用的是自定义的一个继承BaseUnaryEdge的边,从例子中的EdgeProjectXYZRGBDPoseOnly这个类在linearizeOplus中写下了关于位姿节点的雅克比矩阵,里面也没有相机模型参数模型(没有涉及到相机内参),没有关于空间坐标点的雅克比矩阵。通过书上175页误差函数,可以将空间点也作为优化变量,可在这个函数内部加入关于空间点的雅克比矩阵-R,因为优化空间点时会与位姿节点构成关系,所以在图中我们将空间点和位姿节点链接起来,建立一个二元边,仿照g2o::EdgeProjectXYZ2UV这个类的写法。写下修改后的类:

 class EdgeProjectXYZRGBDPoseOnly : public g2o::BaseBinaryEdge<, Eigen::Vector3d,g2o::VertexSBAPointXYZ, g2o::VertexSE3Expmap>
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
EdgeProjectXYZRGBDPoseOnly( ) {} virtual void computeError()
{
const g2o::VertexSBAPointXYZ * point = dynamic_cast< const g2o::VertexSBAPointXYZ *> ( _vertices[] );
const g2o::VertexSE3Expmap * pose = dynamic_cast<const g2o::VertexSE3Expmap*> ( _vertices[] );
// measurement is p, point is p'
//pose->estimate().map( _point );中用estimate()估计一个值后,然后用映射函数 就是旋转加平移 将其_point映射到另一个相机坐标系下去
//观测值 - 映射值 因为我们做的试验是3D-3D 所以这里把第一帧的3D坐标当做测量值,然后把第二帧坐标映射到第一帧坐标系中
_error = _measurement - pose->estimate().map( point->estimate() );
}
//表示线性化 误差函数 就是要计算雅克比矩阵
virtual void linearizeOplus()override final //这里override 表示override覆盖基类的同名同参函数, final表示派生类的某个函数不能覆盖这个函数
{
g2o::VertexSE3Expmap* pose = dynamic_cast<g2o::VertexSE3Expmap *> (_vertices[]);
g2o::VertexSBAPointXYZ * point = dynamic_cast< g2o::VertexSBAPointXYZ * > (_vertices[] );
g2o::SE3Quat T(pose->estimate());
Eigen::Vector3d xyz_trans = T.map( point->estimate() );//映射到第二帧相机坐标系下的坐标值
double x = xyz_trans[]; //第一帧到第二帧坐标系下变换后的坐标值
double y = xyz_trans[];
double z = xyz_trans[]; //关于空间点的雅克比矩阵-R
_jacobianOplusXi = -T.rotation().toRotationMatrix(); //3x6的关于优化变量的雅克比矩阵 可以看书上p179页 自己推到的结果
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = -z;
_jacobianOplusXj(,) = y;
_jacobianOplusXj(,) = -;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = ; _jacobianOplusXj(,) = z;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = -x;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = -;
_jacobianOplusXj(,) = ; _jacobianOplusXj(,) = -y;
_jacobianOplusXj(,) = x;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = -;
} bool read ( istream& in ) {}
bool write ( ostream& out ) const {}
};

在BA优化过程中我们假定仅仅优化第二帧的空间点坐标,因此在g2o::SparseOptimizer中我们加入空间点为待优化节点,对应节点部分添加如下代码:

 #if MyselfOptimizerMethod                                   //添加空间节点  并且按照书上的方式进行优化的
int pointIndex = ; //因为上面的位姿节点就1个 所以我们这里标号为1
for( auto &p: pts2 ){
auto point = new g2o::VertexSBAPointXYZ();
point->setId( pointIndex++ );
point->setMarginalized( true ); //设置边缘化(必须设置 否则出错)
point->setEstimate( Eigen::Vector3d( p.x, p.y, p.z ) );
optimizer.addVertex( point );
}
#endif

对应边的部分添加如下代码:

  edge->setVertex(  , dynamic_cast< g2o::VertexSBAPointXYZ *> ( optimizer.vertex(index)) );
edge->setVertex( , dynamic_cast< g2o::OptimizableGraph::Vertex *> ( optimizer.vertex() ) );

在BA函数最后加入优化前后第二帧点的位置信息,仅仅以前三个点对为例:

     cout<<"输出优化后的point值:"<<endl;
for (int j = ; j < ; ++j) {
cout<< dynamic_cast< g2o::VertexSBAPointXYZ * > ( optimizer.vertex(j+) )->estimate()<<endl<<endl;//Eigen::Vector3d
}
cout<<endl<<"优化前的点:"<<endl;
for (int i = ; i < ; ++i) {
cout<<pts2[i]<<endl<<endl;
}

需要注意的是自己在代码开头部分加入了一些配置信息的输出:

 void printConfigInfo(){
cout<<"This is the example from GaoXiang's book : ICP结果是第二帧到第一帧的RT."<<endl;
#if BAOptimizer cout<<"BA Optimizer is Provided!"<<endl;
#if ISBAProvideInitValue
cout<<"The RT from ICP's result is Provided for BA as a initialization."<<endl;
#else
cout<<" But,Not provide initialization for BA!"<<endl;
#endif #if MyselfOptimizerMethod
cout<<"优化了空间点和位姿节点!"<<endl;
#else
cout<<"未对空间点进行优化!"<<endl;
#endif #endif
}

在最开始配置区部分,读者可以自己调节系统如何输出,配置区代码如下:

 /*+++++++++++++++++++++++++++++++++++++++++配置信息+++++++++++++++++++++++++++++++++++++++++++++***/

 #define  MyselfOptimizerMethod  1   //  1: 课后习题7代码结果
// 0: 3d-3d书上例子
#define ISBAProvideInitValue 0 // 1: 用ICP结果为BA提供初值
// 0: 用单位矩阵I和0平移向量作为初始值 #define BAOptimizer 1 // 1: 加入BA优化
// 0: 不加入BA优化 /*+++++++++++++++++++++++++++++++++++++++++End配置信息+++++++++++++++++++++++++++++++++++++++++++++*/

总体代码以及结果图如下:

 #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 <Eigen/Geometry>
#include <Eigen/SVD>
#include <g2o/core/base_vertex.h>
#include <g2o/core/base_unary_edge.h>
#include <g2o/core/block_solver.h>
#include <g2o/core/optimization_algorithm_gauss_newton.h>
#include <g2o/solvers/eigen/linear_solver_eigen.h>
#include <g2o/types/sba/types_six_dof_expmap.h>
#include <chrono>
#include <g2o/core/optimization_algorithm_levenberg.h> using namespace std;
using namespace cv;
/*+++++++++++++++++++++++++++++++++++++++++配置信息+++++++++++++++++++++++++++++++++++++++++++++***/ #define MyselfOptimizerMethod 1 // 1: 课后习题7代码结果
// 0: 3d-3d书上例子
#define ISBAProvideInitValue 0 // 1: 用ICP结果为BA提供初值
// 0: 用单位矩阵I和0平移向量作为初始值 #define BAOptimizer 1 // 1: 加入BA优化
// 0: 不加入BA优化 /*+++++++++++++++++++++++++++++++++++++++++End配置信息+++++++++++++++++++++++++++++++++++++++++++++*/ 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 ); // 像素坐标转相机归一化坐标
Point2d pixel2cam ( const Point2d& p, const Mat& K ); void pose_estimation_3d3d (
const vector<Point3f>& pts1,
const vector<Point3f>& pts2,
Mat& R, Mat& t
); #if BAOptimizer
void bundleAdjustment (
const vector< Point3f >& pts1,
const vector< Point3f >& pts2,
Mat& R, Mat& t ); #if MyselfOptimizerMethod
//课后习题7题
class EdgeProjectXYZRGBDPoseOnly : public g2o::BaseBinaryEdge<, Eigen::Vector3d,g2o::VertexSBAPointXYZ, g2o::VertexSE3Expmap>
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
EdgeProjectXYZRGBDPoseOnly( ) {} virtual void computeError()
{
const g2o::VertexSBAPointXYZ * point = dynamic_cast< const g2o::VertexSBAPointXYZ *> ( _vertices[] );
const g2o::VertexSE3Expmap * pose = dynamic_cast<const g2o::VertexSE3Expmap*> ( _vertices[] );
// measurement is p, point is p'
//pose->estimate().map( _point );中用estimate()估计一个值后,然后用映射函数 就是旋转加平移 将其_point映射到另一个相机坐标系下去
//观测值 - 映射值 因为我们做的试验是3D-3D 所以这里把第一帧的3D坐标当做测量值,然后把第二帧坐标映射到第一帧坐标系中
_error = _measurement - pose->estimate().map( point->estimate() );
}
//表示线性化 误差函数 就是要计算雅克比矩阵
virtual void linearizeOplus()override final //这里override 表示override覆盖基类的同名同参函数, final表示派生类的某个函数不能覆盖这个函数
{
g2o::VertexSE3Expmap* pose = dynamic_cast<g2o::VertexSE3Expmap *> (_vertices[]);
g2o::VertexSBAPointXYZ * point = dynamic_cast< g2o::VertexSBAPointXYZ * > (_vertices[] );
g2o::SE3Quat T(pose->estimate());
Eigen::Vector3d xyz_trans = T.map( point->estimate() );//映射到第二帧相机坐标系下的坐标值
double x = xyz_trans[]; //第一帧到第二帧坐标系下变换后的坐标值
double y = xyz_trans[];
double z = xyz_trans[]; //关于空间点的雅克比矩阵-R
_jacobianOplusXi = -T.rotation().toRotationMatrix(); //3x6的关于优化变量的雅克比矩阵 可以看书上p179页 自己推到的结果
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = -z;
_jacobianOplusXj(,) = y;
_jacobianOplusXj(,) = -;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = ; _jacobianOplusXj(,) = z;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = -x;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = -;
_jacobianOplusXj(,) = ; _jacobianOplusXj(,) = -y;
_jacobianOplusXj(,) = x;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = ;
_jacobianOplusXj(,) = -;
} bool read ( istream& in ) {}
bool write ( ostream& out ) const {}
};
#else
// g2o edge 边代表误差 所以在里面要override一个computerError函数
class EdgeProjectXYZRGBDPoseOnly : public g2o::BaseUnaryEdge<, Eigen::Vector3d, g2o::VertexSE3Expmap>
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
EdgeProjectXYZRGBDPoseOnly( const Eigen::Vector3d& point ) : _point(point) {} virtual void computeError()
{
const g2o::VertexSE3Expmap* pose = dynamic_cast<const g2o::VertexSE3Expmap*> ( _vertices[] );
// measurement is p, point is p'
//pose->estimate().map( _point );中用estimate()估计一个值后,然后用映射函数 就是旋转加平移 将其_point映射到另一个相机坐标系下去
//观测值 - 映射值 因为我们做的试验是3D-3D 所以这里把第一帧的3D坐标当做测量值,然后把第二帧坐标映射到第一帧坐标系中
_error = _measurement - pose->estimate().map( _point );
}
//表示线性化 误差函数 就是要计算雅克比矩阵
virtual void linearizeOplus()
{
g2o::VertexSE3Expmap* pose = dynamic_cast<g2o::VertexSE3Expmap *>(_vertices[]);
g2o::SE3Quat T(pose->estimate());
Eigen::Vector3d xyz_trans = T.map(_point);//映射到第二帧相机坐标系下的坐标值
double x = xyz_trans[]; //第一帧到第二帧坐标系下变换后的坐标值
double y = xyz_trans[];
double z = xyz_trans[]; //3x6的关于优化变量的雅克比矩阵 可以看书上p179页 自己推到的结果
_jacobianOplusXi(,) = ;
_jacobianOplusXi(,) = -z;
_jacobianOplusXi(,) = y;
_jacobianOplusXi(,) = -;
_jacobianOplusXi(,) = ;
_jacobianOplusXi(,) = ; _jacobianOplusXi(,) = z;
_jacobianOplusXi(,) = ;
_jacobianOplusXi(,) = -x;
_jacobianOplusXi(,) = ;
_jacobianOplusXi(,) = -;
_jacobianOplusXi(,) = ; _jacobianOplusXi(,) = -y;
_jacobianOplusXi(,) = x;
_jacobianOplusXi(,) = ;
_jacobianOplusXi(,) = ;
_jacobianOplusXi(,) = ;
_jacobianOplusXi(,) = -;
} bool read ( istream& in ) {}
bool write ( ostream& out ) const {}
protected:
Eigen::Vector3d _point; //设立误差点 之后将其与测量值进行相减 求得误差
};
#endif #endif
//自己设置的打印配置信息
void printConfigInfo(){
cout<<"This is the example from GaoXiang's book : ICP结果是第二帧到第一帧的RT."<<endl;
#if BAOptimizer cout<<"BA Optimizer is Provided!"<<endl;
#if ISBAProvideInitValue
cout<<"The RT from ICP's result is Provided for BA as a initialization."<<endl;
#else
cout<<" But,Not provide initialization for BA!"<<endl;
#endif #if MyselfOptimizerMethod
cout<<"优化了空间点和位姿节点!"<<endl;
#else
cout<<"未对空间点进行优化!"<<endl;
#endif #endif
}
int main ( int argc, char** argv )
{
if ( argc != )
{
cout<<"usage: pose_estimation_3d3d img1 img2 depth1 depth2"<<endl;
return ;
}
printConfigInfo();//输出配置信息
//-- 读取图像
Mat img_1 = imread ( argv[], CV_LOAD_IMAGE_COLOR );
Mat img_2 = imread ( argv[], CV_LOAD_IMAGE_COLOR ); 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; // 建立3D点
Mat depth1 = imread ( argv[], CV_LOAD_IMAGE_UNCHANGED ); // 深度图为16位无符号数,单通道图像
Mat depth2 = imread ( argv[], CV_LOAD_IMAGE_UNCHANGED ); // 深度图为16位无符号数,单通道图像
Mat K = ( Mat_<double> ( , ) << 520.9, , 325.1, , 521.0, 249.7, , , );
vector<Point3f> pts1, pts2; for ( DMatch m:matches )
{
ushort d1 = depth1.ptr<unsigned short> ( int ( keypoints_1[m.queryIdx].pt.y ) ) [ int ( keypoints_1[m.queryIdx].pt.x ) ];
ushort d2 = depth2.ptr<unsigned short> ( int ( keypoints_2[m.trainIdx].pt.y ) ) [ int ( keypoints_2[m.trainIdx].pt.x ) ];
if ( d1== || d2== ) // bad depth
continue;
Point2d p1 = pixel2cam ( keypoints_1[m.queryIdx].pt, K );
Point2d p2 = pixel2cam ( keypoints_2[m.trainIdx].pt, K );
float dd1 = float ( d1 ) /5000.0;
float dd2 = float ( d2 ) /5000.0; //存储特征点的3D坐标
pts1.push_back ( Point3f ( p1.x*dd1, p1.y*dd1, dd1 ) );
pts2.push_back ( Point3f ( p2.x*dd2, p2.y*dd2, dd2 ) );
} cout<<"3d-3d pairs: "<<pts1.size() <<endl;
Mat R, t;
pose_estimation_3d3d ( pts1, pts2, R, t ); //ICP方式的位姿估计
cout<<"ICP via SVD results: "<<endl;
cout<<"R = "<<R<<endl;
cout<<"t = "<<t<<endl;
//实际上是第一帧到第二帧的R T
cout<<"第一帧到第二帧的旋转和平移:" << endl << "R_inv = "<<R.t() <<endl;
cout<<"t_inv = "<<-R.t() *t<<endl; cout<<"calling bundle adjustment"<<endl; #if BAOptimizer
bundleAdjustment( pts1, pts2, R, t ); //BA优化估计相机位姿 RT 是提供优化的初始值
#endif // verify p1 = R*p2 + t
/* for ( int i=0; i<5; i++ )
{
cout<<"p1 = "<<pts1[i]<<endl;
cout<<"p2 = "<<pts2[i]<<endl;
cout<<"(R*p2+t) = "<<
R * (Mat_<double>(3,1)<<pts2[i].x, pts2[i].y, pts2[i].z) + t
<<endl;
cout<<endl;
}
*/
} 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=, max_dist=; //找出所有匹配之间的最小距离和最大距离, 即是最相似的和最不相似的两组点之间的距离
for ( int i = ; 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 = ; i < descriptors_1.rows; i++ )
{
if ( match[i].distance <= max ( *min_dist, 30.0 ) )
{
matches.push_back ( match[i] );
}
}
} Point2d pixel2cam ( const Point2d& p, const Mat& K )
{
return Point2d
(
( p.x - K.at<double> ( , ) ) / K.at<double> ( , ),
( p.y - K.at<double> ( , ) ) / K.at<double> ( , )
);
} void pose_estimation_3d3d (
const vector<Point3f>& pts1,
const vector<Point3f>& pts2,
Mat& R, Mat& t
)
{
Point3f p1, p2; // center of mass
int N = pts1.size();
for ( int i=; i<N; i++ )
{
p1 += pts1[i];
p2 += pts2[i];
}
p1 = Point3f( Vec3f(p1) / N);
p2 = Point3f( Vec3f(p2) / N);
vector<Point3f> q1 ( N ), q2 ( N ); // remove the center 去质心坐标
for ( int i=; i<N; i++ )
{
q1[i] = pts1[i] - p1;
q2[i] = pts2[i] - p2;
} // compute q1*q2^T
Eigen::Matrix3d W = Eigen::Matrix3d::Zero();
for ( int i=; i<N; i++ )
{
W += Eigen::Vector3d ( q1[i].x, q1[i].y, q1[i].z ) * Eigen::Vector3d ( q2[i].x, q2[i].y, q2[i].z ).transpose();
}
cout<<"W="<<W<<endl; // SVD on W
Eigen::JacobiSVD<Eigen::Matrix3d> svd ( W, Eigen::ComputeFullU|Eigen::ComputeFullV );//因为知道矩阵W的类型 所以在分解的时候直接是FullU | FullV
Eigen::Matrix3d U = svd.matrixU();
Eigen::Matrix3d V = svd.matrixV();
cout<<"U="<<U<<endl;
cout<<"V="<<V<<endl; //例程本身的实现方式 求出的R T是第二帧到第一帧的
Eigen::Matrix3d R_ = U* ( V.transpose() );
Eigen::Vector3d t_ = Eigen::Vector3d ( p1.x, p1.y, p1.z ) - R_ * Eigen::Vector3d ( p2.x, p2.y, p2.z ); // convert to cv::Mat
R = ( Mat_<double> ( , ) <<
R_ ( , ), R_ ( , ), R_ ( , ),
R_ ( , ), R_ ( , ), R_ ( , ),
R_ ( , ), R_ ( , ), R_ ( , )
);
t = ( Mat_<double> ( , ) << t_ ( , ), t_ ( , ), t_ ( , ) );
}
#if BAOptimizer
void bundleAdjustment (
const vector< Point3f >& pts1,
const vector< Point3f >& pts2,
Mat& R, Mat& t ) //实际上 R t在这里并不是必要的 这个只是用来提供BA初始值
{
// 初始化g2o
typedef g2o::BlockSolver< g2o::BlockSolverTraits<,> > Block; // pose维度为 6, landmark 维度为 3
std::unique_ptr<Block::LinearSolverType>linearSolver = g2o::make_unique<g2o::LinearSolverEigen<Block::PoseMatrixType>>(); //线性方程求解器
std::unique_ptr<Block>solver_ptr = g2o::make_unique<Block>( std::move(linearSolver) ); //矩阵块求解器
g2o::OptimizationAlgorithmLevenberg * solver = new g2o::OptimizationAlgorithmLevenberg( std::move(solver_ptr) ); //LM法 /* //另一种修改错误的方式
Block::LinearSolverType* linearSolver = new g2o::LinearSolverEigen<Block::PoseMatrixType>(); // 线性方程求解器
Block* solver_ptr = new Block( std::unique_ptr<Block::LinearSolverType>(linearSolver) ); // 矩阵块求解器
g2o::OptimizationAlgorithmLevenberg * solver = new g2o::OptimizationAlgorithmLevenberg ( std::unique_ptr <g2o::Solver>(solver_ptr) ); //LM法
*/
g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm( solver ); // vertex 这里仅仅添加了第二帧节点
auto pose = new g2o::VertexSE3Expmap(); // camera pose
pose->setId(); // 位姿节点编号为0
#if ISBAProvideInitValue // 为图优化提供ICP结果作为初值
Eigen::Matrix3d R_mat;
R_mat <<
R.at<double> ( , ), R.at<double> ( , ), R.at<double> ( , ),
R.at<double> ( , ), R.at<double> ( , ), R.at<double> ( , ),
R.at<double> ( , ), R.at<double> ( , ), R.at<double> ( , );
pose->setEstimate( g2o::SE3Quat(
R_mat,
Eigen::Vector3d( t.at<double> ( , ),t.at<double> ( , ),t.at<double> ( , ) )
)
);
#else // 提供默认初值
pose->setEstimate( g2o::SE3Quat(
Eigen::Matrix3d::Identity(),
Eigen::Vector3d( ,, )
) );
#endif
optimizer.addVertex( pose ); //向优化器中添加节点
#if MyselfOptimizerMethod //添加空间节点 并且按照书上的方式进行优化的
int pointIndex = ; //因为上面的位姿节点就1个 所以我们这里标号为1
for( auto &p: pts2 ){
auto point = new g2o::VertexSBAPointXYZ();
point->setId( pointIndex++ );
point->setMarginalized( true ); //设置边缘化(必须设置 否则出错)
point->setEstimate( Eigen::Vector3d( p.x, p.y, p.z ) );
optimizer.addVertex( point );
}
#endif
// edges
int index = ;
vector<EdgeProjectXYZRGBDPoseOnly*> edges;
for ( size_t i=; i<pts1.size(); i++ )
{
#if MyselfOptimizerMethod
auto edge = new EdgeProjectXYZRGBDPoseOnly( ); //在课后习题中修改的EdgeProjectXYZRGBDPoseOnly可以去掉_point成员变量
#else
auto edge = new EdgeProjectXYZRGBDPoseOnly(
Eigen::Vector3d(pts2[i].x, pts2[i].y, pts2[i].z) );
#endif
edge->setId( index );
#if MyselfOptimizerMethod
edge->setVertex( , dynamic_cast< g2o::VertexSBAPointXYZ *> ( optimizer.vertex(index)) );
edge->setVertex( , dynamic_cast< g2o::OptimizableGraph::Vertex *> ( optimizer.vertex() ) );
#else //参考ORB_SLAM:这里将原来的g2o::VertexSE3Expmap* 替换为g2o::OptimizableGraph::Vertex *
edge->setVertex( , dynamic_cast< g2o::OptimizableGraph::Vertex *> ( optimizer.vertex() ) );
#endif
//这里以第一帧为测量值 说明优化的是第二帧到第一帧的旋转r和平移t
edge->setMeasurement( Eigen::Vector3d (
pts1[i].x, pts1[i].y, pts1[i].z)
);
edge->setInformation( Eigen::Matrix3d::Identity()*1e4 );
optimizer.addEdge(edge); //向优化器中添加边
index++;
edges.push_back(edge); //存储边指针
} chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
optimizer.setVerbose( true );
optimizer.initializeOptimization();
optimizer.optimize();
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<<endl<<"after optimization:"<<endl;
cout<<"T="<<endl<<Eigen::Isometry3d( pose->estimate() ).matrix()<<endl;
/*
cout<<"输出优化后的point值:"<<endl;
for (int j = 0; j <3 ; ++j) {
cout<< dynamic_cast< g2o::VertexSBAPointXYZ * > ( optimizer.vertex(j+1) )->estimate()<<endl<<endl;//Eigen::Vector3d
}
cout<<endl<<"优化前的点:"<<endl;
for (int i = 0; i <3 ; ++i) {
cout<<pts2[i]<<endl<<endl;
}
*/ }
#endif

运行结果截图:

视觉slam十四讲第七章课后习题7

参考资料:
视觉slam十四讲从理论到实践

欢迎大家关注我的微信公众号「佛系师兄」,里面有关于 Ceres 以及 OpenCV 等更多技术文章。

比如

反复研究好几遍,我才发现关于 CMake 变量还可以这样理解!

更多好的文章会优先在里面不定期分享!打开微信客户端,扫描下方二维码即可关注!

视觉slam十四讲第七章课后习题7