SLAM十四讲:直接法(8)

内容总览:一些直接法的代码

RGBD稀疏直接法(g2o)

简述:
假设周围像素不变,不必计算描述子,只使用数百个像素

只能计算稀疏结构

参数
输入:测量值(空间点的灰度),新的灰度图,相机内参

输出相机位姿

计算误差:

virtual void computeError()
    {
        const VertexSE3Expmap* v  =static_cast<const VertexSE3Expmap*> ( _vertices[0] );
        Eigen::Vector3d x_local = v->estimate().map ( x_world_ );        //将世界坐标映射到了图像上,local代表图像局部
        float x = x_local[0]*fx_/x_local[2] + cx_;
        float y = x_local[1]*fy_/x_local[2] + cy_;
        // check x,y is in the image
        if ( x-4<0 || ( x+4 ) >image_->cols || ( y-4 ) <0 || ( y+4 ) >image_->rows )    //范围检测?
        {
            _error ( 0,0 ) = 0.0;
            this->setLevel ( 1 );
        }
        else
        {
            _error ( 0,0 ) = getPixelValue ( x,y ) - _measurement;        //计算误差,计算值减真实值?
        }
    }

将I1的点通过estimate().map映射到另一个图像上,然后用像素公式计算,误差为I2-I1

计算雅可比J

    virtual void linearizeOplus( )        //计算呀可比矩阵
    {
        if ( level() == 1 )    //范围不对,或者第一次的时候,初始化1*6的0矩阵
        {
            _jacobianOplusXi = Eigen::Matrix<double, 1, 6>::Zero();
            return;
        }
        VertexSE3Expmap* vtx = static_cast<VertexSE3Expmap*> ( _vertices[0] );
        Eigen::Vector3d xyz_trans = vtx->estimate().map ( x_world_ );   // q in book        估计出来的世界坐标映射?

        double x = xyz_trans[0];
        double y = xyz_trans[1];
        double invz = 1.0/xyz_trans[2];
        double invz_2 = invz*invz;

        float u = x*fx_*invz + cx_;
        float v = y*fy_*invz + cy_;

        // jacobian from se3 to u,v    
        // NOTE that in g2o the Lie algebra is (\omega, \epsilon), where \omega is so(3) and \epsilon the translation
        Eigen::Matrix<double, 2, 6> jacobian_uv_ksai;

        jacobian_uv_ksai ( 0,0 ) = - x*y*invz_2 *fx_;
        jacobian_uv_ksai ( 0,1 ) = ( 1+ ( x*x*invz_2 ) ) *fx_;
        jacobian_uv_ksai ( 0,2 ) = - y*invz *fx_;
        jacobian_uv_ksai ( 0,3 ) = invz *fx_;
        jacobian_uv_ksai ( 0,4 ) = 0;
        jacobian_uv_ksai ( 0,5 ) = -x*invz_2 *fx_;

        jacobian_uv_ksai ( 1,0 ) = - ( 1+y*y*invz_2 ) *fy_;
        jacobian_uv_ksai ( 1,1 ) = x*y*invz_2 *fy_;
        jacobian_uv_ksai ( 1,2 ) = x*invz *fy_;
        jacobian_uv_ksai ( 1,3 ) = 0;
        jacobian_uv_ksai ( 1,4 ) = invz *fy_;
        jacobian_uv_ksai ( 1,5 ) = -y*invz_2 *fy_;

        Eigen::Matrix<double, 1, 2> jacobian_pixel_uv;

        jacobian_pixel_uv ( 0,0 ) = ( getPixelValue ( u+1,v )-getPixelValue ( u-1,v ) ) /2;
        jacobian_pixel_uv ( 0,1 ) = ( getPixelValue ( u,v+1 )-getPixelValue ( u,v-1 ) ) /2;

        _jacobianOplusXi = jacobian_pixel_uv*jacobian_uv_ksai;
    }

J矩阵,在计算位姿时要用到

插值

    inline float getPixelValue ( float x, float y )
    {
        uchar* data = & image_->data[ int ( y ) * image_->step + int ( x ) ];
        float xx = x - floor ( x );
        float yy = y - floor ( y );
        return float (
                   ( 1-xx ) * ( 1-yy ) * data[0] +
                   xx* ( 1-yy ) * data[1] +
                   ( 1-xx ) *yy*data[ image_->step ] +
                   xx*yy*data[image_->step+1]
               );
    }
   

为了更精确的计算像素亮度,我们使用简单的双线性插值(应该是这个程序段)

以上得出误差项(一元边),下面使用g2o加上优化变量(顶点)

初始化g2o:

typedef g2o::BlockSolver<g2o::BlockSolverTraits<6,1>> DirectBlock;  // 求解的向量是6*1的
//定义线性求解器类型
DirectBlock::LinearSolverType* linearSolver = new g2o::LinearSolverDense< DirectBlock::PoseMatrixType > ();
//根据求解器求得矩阵块
DirectBlock* solver_ptr = new DirectBlock ( linearSolver );
//使用L-M优化
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( solver_ptr ); // L-M

g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm ( solver );
optimizer.setVerbose( true );
//初始化李代数
g2o::VertexSE3Expmap* pose = new g2o::VertexSE3Expmap();
pose->setEstimate ( g2o::SE3Quat ( Tcw.rotation(), Tcw.translation() ) );
pose->setId ( 0 );
optimizer.addVertex ( pose );

直接估计法

添加边

int id=1;
for ( Measurement m: measurements )
{
    //输入坐标,内参,灰度矩阵
  EdgeSE3ProjectDirect* edge = new EdgeSE3ProjectDirect (
      m.pos_world,
      K ( 0,0 ), K ( 1,1 ), K ( 0,2 ), K ( 1,2 ), gray
  );
  //后面应该是一套的操作
  edge->setVertex ( 0, pose );
  edge->setMeasurement ( m.grayscale );
  edge->setInformation ( Eigen::Matrix<double,1,1>::Identity() );
  edge->setId ( id++ );
  optimizer.addEdge ( edge );
}
cout<<"edges in graph: "<<optimizer.edges().size() <<endl;
optimizer.initializeOptimization();
optimizer.optimize ( 30 );
Tcw = pose->estimate();

main函数中的操作

流程
读取图像信息,相机内参,对第一个图提取FAST特征,后面的用直接法
代码如下

 for ( int index=0; index<10; index++ )
    {
        cout<<"*********** loop "<<index<<" ************"<<endl;
               //准备操作,读取各种信息
        fin>>time_rgb>>rgb_file>>time_depth>>depth_file;
        color = cv::imread ( path_to_dataset+"/"+rgb_file );
        depth = cv::imread ( path_to_dataset+"/"+depth_file, -1 );
        if ( color.data==nullptr || depth.data==nullptr )
            continue;
        cv::cvtColor ( color, gray, cv::COLOR_BGR2GRAY );
        if ( index ==0 )
        {
            // 对第一帧提取FAST特征点
            vector<cv::KeyPoint> keypoints;
            cv::Ptr<cv::FastFeatureDetector> detector = cv::FastFeatureDetector::create();
            detector->detect ( color, keypoints );
            for ( auto kp:keypoints )
            {
                // 去掉邻近边缘处的点
                if ( kp.pt.x < 20 || kp.pt.y < 20 || ( kp.pt.x+20 ) >color.cols || ( kp.pt.y+20 ) >color.rows )
                    continue;
                ushort d = depth.ptr<ushort> ( cvRound ( kp.pt.y ) ) [ cvRound ( kp.pt.x ) ];
                if ( d==0 )
                    continue;
                //获得世界坐标点
                Eigen::Vector3d p3d = project2Dto3D ( kp.pt.x, kp.pt.y, d, fx, fy, cx, cy, depth_scale );
                float grayscale = float ( gray.ptr<uchar> ( cvRound ( kp.pt.y ) ) [ cvRound ( kp.pt.x ) ] );
                measurements.push_back ( Measurement ( p3d, grayscale ) );
            }
            prev_color = color.clone();
            continue;
        }
        // 使用直接法计算相机运动
        chrono::steady_clock::time_point t1 = chrono::steady_clock::now();

        poseEstimationDirect ( measurements, &gray, K, Tcw );

        chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
        chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>> ( t2-t1 );
        cout<<"direct method costs time: "<<time_used.count() <<" seconds."<<endl;
        cout<<"Tcw="<<Tcw.matrix() <<endl;W
        

半稠密直接

代码

for ( int x=10; x<gray.cols-10; x++ )
  for ( int y=10; y<gray.rows-10; y++ )
   {
     Eigen::Vector2d delta (gray.ptr<uchar>(y)[x+1] - gray.ptr<uchar> (y)[x-1],
     gray.ptr<uchar>(y+1)[x] - gray.ptr<uchar>(y-1)[x]
         );
      if ( delta.norm() < 50 )
           continue;
      ushort d = depth.ptr<ushort> (y)[x];
      if ( d==0 )
           continue;
      Eigen::Vector3d p3d = project2Dto3D ( x, y, d, fx, fy, cx, cy, depth_scale );
      float grayscale = float ( gray.ptr<uchar> (y) [x] );
      measurements.push_back ( Measurement ( p3d, grayscale ) );
    }

稀疏特征点改成了带有明显梯度的像素,这些都会变化图优化中的边

优点
1.省去计算特征点,描述子的时间
2.有像素梯度即可,无需计算特征点
3.少量的渐变图片也可以使用直接法

缺点
1.计算梯度,但图像是强烈的非凸函数,除非运动很小,否则容易得出极小值点
2.灰度不变的假设并不一定成立
3.单个像素没有区分度,每个像素对相机方向的调整想法不一样,只能少数服从多数,数量代替质量

   
            for ( int x=10; x<gray.cols-10; x++ )
                for ( int y=10; y<gray.rows-10; y++ )
                {
                    Eigen::Vector2d delta (
                        gray.ptr<uchar>(y)[x+1] - gray.ptr<uchar>(y)[x-1],
                        gray.ptr<uchar>(y+1)[x] - gray.ptr<uchar>(y-1)[x]
                    );
                    if ( delta.norm() < 50 )
                        continue;
                    ushort d = depth.ptr<ushort> (y)[x];
                    if ( d==0 )
                        continue;
                    Eigen::Vector3d p3d = project2Dto3D ( x, y, d, fx, fy, cx, cy, depth_scale );
                    float grayscale = float ( gray.ptr<uchar> (y) [x] );
                    measurements.push_back ( Measurement ( p3d, grayscale ) );
                }

发布了30 篇原创文章 · 获赞 1 · 访问量 2423

猜你喜欢

转载自blog.csdn.net/McEason/article/details/103199100