https://mp.weixin.qq.com/s?__biz=MzIxOTczOTM4NA==&mid=2247486992&idx=1&sn=ecb7c3ef9bd968e51914c2f5b767428d&chksm=97d7eb87a0a062912a9db9fb16a08129f373791fd3918952342d5db46c0bc4880326a7933671&cur_album_id=1361700104461467649&scene=189#wechat_redirect
完成代码
1定义
// 曲线模型的顶点,模板参数:优化变量维度和数据类型 class CurveFittingVertex: public g2o::BaseVertex<3, Eigen::Vector3d> { public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW virtual void setToOriginImpl() // 重置 { _estimate << 0,0,0; } virtual void oplusImpl( const double* update ) // 更新 {<br> _estimate += Eigen::Vector3d(update); } // 存盘和读盘:留空 virtual bool read( istream& in ) {} virtual bool write( ostream& out ) const {} };
2使用
CurveFittingVertex* v = new CurveFittingVertex(); v->setEstimate( Eigen::Vector3d(0,0,0) ); v->setId(0);
节点类 BaseVertex<D,T>
模板参数 D 和 T
D是int 类型的,表示vertex的最小维度,比如3D空间中旋转是3维的,那么这里 D = 3
T是待估计vertex的数据类型,比如用四元数表达三维旋转的话,T就是Quaternion 类型
节点类
BaseVertex<D,T>
函数
virtual bool read(std::istream& is); virtual bool write(std::ostream& os) const; virtual void oplusImpl(const number_t* update); virtual void setToOriginImpl();
read,write:分别是读盘、存盘函数,一般情况下不需要进行读/写操作的话,仅仅声明一下就可以
setToOriginImpl:顶点重置函数,设定被优化变量的原始值。
oplusImpl:顶点更新函数。非常重要的一个函数,主要用于优化过程中增量△x 的计算。我们根据增量方程计算出增量之后,就是通过这个函数对估计值进行调整的,因此这个函数的内容一定要重视。
通用例子0
class myVertex: public g2::BaseVertex<Dim, Type> { public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW myVertex(){} virtual void read(std::istream& is) {} virtual void write(std::ostream& os) const {} virtual void setOriginImpl() { _estimate = Type(); } virtual void oplusImpl(const double* update) override { _estimate += /*update*/; } }
例子节点1 y=ax^2+bx+c
参数 a b c
构造类型 Eigen::Vector3d
初值设置为0
跟新方程 x + △x
_estimate += Eigen::Vector3d(update);
更新时也是直接把更新量 update 加上去的
对于这个例子是可以直接加,因为顶点类型是Eigen::Vector3d,属于向量,是可以通过加法来更新的。
class CurveFittingVertex: public g2o::BaseVertex<3, Eigen::Vector3d> { public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW virtual void setToOriginImpl() // 重置 { _estimate << 0,0,0; } virtual void oplusImpl( const double* update ) // 更新 { _estimate += Eigen::Vector3d(update); } // 存盘和读盘:留空 virtual bool read( istream& in ) {} virtual bool write( ostream& out ) const {} };
例子节点2 李代数表示三维点 VertexPointXYZ
空间点位置 VertexPointXYZ,维度为3,类型是Eigen的Vector3
跟新 直接相加
class G2O_TYPES_SBA_API VertexSBAPointXYZ : public BaseVertex<3, Vector3> { public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW VertexSBAPointXYZ(); virtual bool read(std::istream& is); virtual bool write(std::ostream& os) const; virtual void setToOriginImpl() { _estimate.fill(0); } virtual void oplusImpl(const number_t* update) { Eigen::Map<const Vector3> v(update); _estimate += v; } };
例子节点3 李代数表示位姿VertexSE3Expmap
节点 T=Rt
// 第一个参数6 表示内部存储的优化变量维度,这是个6维的李代数
// 第二个参数是优化变量的类型,这里使用了g2o定义的相机位姿类型:SE3Quat
跟新方程 x + △x
Eigen::Map<const Vector6> update(update_); setEstimate(SE3Quat::exp(update)*estimate()); //更新方式
李代数数的位姿不是直接相加,是相乘。
它内部使用了四元数表达旋转,然后加上位移来存储位姿,同时支持李代数上的运算,比如对数映射(log函数)、李代数上增量(update函数)等操作
是不能直接加,原因是变换矩阵不满足加法封闭。
为什么?
为什么相机位姿顶点类VertexSE3Expmap使用了李代数表示相机位姿,而不是使用旋转矩阵和平移矩阵?
--1可以求导,2无约束条件。这是因为旋转矩阵是有约束的矩阵,它必须是正交矩阵且行列式为1。使用它作为优化变量就会引入额外的约束条件,从而增大优化的复杂度。而将旋转矩阵通过李群-李代数之间的转换关系转换为李代数表示,就可以把位姿估计变成无约束的优化问题,求解难度降低。
/** \* \brief SE3 Vertex parameterized internally with a transformation matrix and externally with its exponential map */ class G2O_TYPES_SBA_API VertexSE3Expmap : public BaseVertex<6, SE3Quat>{ public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW VertexSE3Expmap(); bool read(std::istream& is); bool write(std::ostream& os) const; virtual void setToOriginImpl() { _estimate = SE3Quat(); } virtual void oplusImpl(const number_t* update_) { Eigen::Map<const Vector6> update(update_); setEstimate(SE3Quat::exp(update)*estimate()); //更新方式 } };
添加节点
例子1 曲线拟合的例子
y=ax^2+bx+c
构造一个点[a,b,c] 只有一个点
// 往图中增加顶点 CurveFittingVertex* v = new CurveFittingVertex(); v->setEstimate( Eigen::Vector3d(0,0,0) ); v->setId(0); optimizer.addVertex( v );
例子2三维点优化
Rt 多个地图点 多个优化节点 但是是一元点 没有位姿 不是二元
// 往图中增加顶点 CurveFittingVertex* v = new CurveFittingVertex(); v->setEstimate( Eigen::Vector3d(0,0,0) ); v->setId(0); optimizer.addVertex( v ); int index = 1; for ( const Point3f p:points_3d ) // landmarks { g2o::VertexSBAPointXYZ* point = new g2o::VertexSBAPointXYZ(); point->setId ( index++ ); point->setEstimate ( Eigen::Vector3d ( p.x, p.y, p.z ) ); point->setMarginalized ( true ); optimizer.addVertex ( point );
例子3 6维度位姿 优化
构建一个节点,用于表示当前帧位姿
获取到了当前帧的位姿mTcw
,将其赋给vSE3
。我们设置当前节点的ID为0,