第八讲 direct_semidense.cpp

#include <iostream>

#include <fstream>

#include <list>

#include <vector>

#include <chrono>

#include <ctime>

#include <climits>

 

#include <opencv2/core/core.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

#include <opencv2/features2d/features2d.hpp>

 

#include <g2o/core/base_unary_edge.h>

#include <g2o/core/block_solver.h>

#include <g2o/core/optimization_algorithm_levenberg.h>

#include <g2o/solvers/dense/linear_solver_dense.h>

#include <g2o/core/robust_kernel.h>

#include <g2o/types/sba/types_six_dof_expmap.h>

 

using namespace std;

using namespace g2o;

 

/********************************************

 * 本节演示了RGBD上的半稠密直接法

 ********************************************/

 

// 一次测量的值,包括一个世界坐标系下三维点与一个灰度值

// 一次测量的值,包括一个三维点坐标和这个点对应到灰度图上的灰度值,这里注意仅是一个点,不是一张图

//测量空间点坐标pos_world和此点在图像上的灰度值grayscale

struct Measurement

{

    Measurement ( Eigen::Vector3d p, float g ) : pos_world ( p ), grayscale ( g ) {}

    Eigen::Vector3d pos_world;

    float grayscale;

};

 

inline Eigen::Vector3d project2Dto3D ( int x, int y, int d, float fx, float fy, float cx, float cy, float scale )

{

    float zz = float ( d ) /scale;

    float xx = zz* ( x-cx ) /fx;

    float yy = zz* ( y-cy ) /fy;

    return Eigen::Vector3d ( xx, yy, zz );

}

 

inline Eigen::Vector2d project3Dto2D ( float x, float y, float z, float fx, float fy, float cx, float cy )

{

    float u = fx*x/z+cx;

    float v = fy*y/z+cy;

    return Eigen::Vector2d ( u,v );

}

 

// 直接法估计位姿

// 输入:测量值(空间点的灰度),新的灰度图,相机内参; 输出:相机位姿

// 返回:true为成功,false失败

bool poseEstimationDirect ( const vector<Measurement>& measurements, cv::Mat* gray, Eigen::Matrix3f& intrinsics, Eigen::Isometry3d& Tcw );

 

 

// project a 3d point into an image plane, the error is photometric error

//将一个3d点投影到一个图像平面,误差是光度误差

// an unary edge with one vertex SE3Expmap (the pose of camera)

class EdgeSE3ProjectDirect: public BaseUnaryEdge< 1, double, VertexSE3Expmap>

{

public:

    EIGEN_MAKE_ALIGNED_OPERATOR_NEW

 

    EdgeSE3ProjectDirect() {}

 

    EdgeSE3ProjectDirect ( Eigen::Vector3d point, float fx, float fy, float cx, float cy, cv::Mat* image )

        : x_world_ ( point ), fx_ ( fx ), fy_ ( fy ), cx_ ( cx ), cy_ ( cy ), image_ ( image )

    {}

 

    virtual void computeError()

    {

        const VertexSE3Expmap* v  =static_cast<const VertexSE3Expmap*> ( _vertices[0] );

//从世界坐标系下坐标到像素坐标:

        //位姿估计值.map()函数即为乘上位姿T,这里其实为3d点世界坐标乘上相机位姿,计算出当前相机坐标系下的坐标

        Eigen::Vector3d x_local = v->estimate().map ( x_world_ );

//3d坐标投影到像素坐标

        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

//检查像素是否还在图像中,这里靠近边缘有4像素时就认为已经出了图像,将误差设置为0,此条边的Level设置为1,用于区分

        if ( x-4<0 || ( x+4 ) >image_->cols || ( y-4 ) <0 || ( y+4 ) >image_->rows )

        {

            _error ( 0,0 ) = 0.0;

            this->setLevel ( 1 );

        }

        else

        {//这里误差为标量(光度值的差值),用估计出来的(u.v)处灰度值,减去测量值。

            //这里的getPixelValue(u,v)相当于I(u,v)

            _error ( 0,0 ) = getPixelValue ( x,y ) - _measurement;

        }

    }

 

    // plus in manifold//计算线性增量,也就是雅克比矩阵J

    virtual void linearizeOplus( )

    {

        if ( level() == 1 )

        {

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

    }

 

    // dummy read and write functions because we don't care...

    virtual bool read ( std::istream& in ) {}

    virtual bool write ( std::ostream& out ) const {}

 

protected:

    // get a gray scale value from reference image (bilinear interpolated)

    inline float getPixelValue ( float x, float y )

 //取得变换后的图中对应像素坐标处的灰度值,这里并不是返回一张图像的灰度值,而是就是写死了,就是类构造里传入的那张图

    {

//这里先说一下各个参数的类型:

        //image_为Mat*类型,图像指针,所以调用data时用->符号,

        //data为图像矩阵首地址,支持数组形式访问,data[]就是访问到像素的值了,此处为像素的灰度值,类型为uchar

        //关于step有点复杂,data[]中括号的式子有点复杂,总的意思就是y行乘上每行内存数,定位到行,然后在加上x,定位到像素

        //step具体解释在最后面有一些资料

        //image_->data[int(y)*image_->step + int(x)]这一步读到了x,y处的灰度值,类型为uchar,

        //但是后面由于线性插值,需要定位这个像素的位置,而不是他的灰度值,所以取其地址,赋值给data_ptr,记住它的位置,后面使用

        uchar* data = & image_->data[ int ( y ) * image_->step + int ( x ) ];

//由于x,y这里有可能带小数,但是像素位置肯定是整数,所以,问题来了,(1.2, 4.5)像素坐标处的灰度值为多少呢?OK,线性插值!

        //说一下floor(),std中的cmath函数。向下取整,返回不大于x的整数。例floor(4.9)=4

        //xx和yy,就是取到小数部分。例:x=4.9的话,xx=x-floor(x)就为0.9。y同理

        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]

               );

    }

//这里说一下自定义边类型时的成员变量怎么来,不是随便写,而是误差需要哪些变量算出来,就定义哪些。

    //这里需要世界坐标系下的空间点坐标,相机内参,和第二帧图

    //这里说一下这个第二帧图:空间点经RT,经内参投影到第二帧图(image_)上,在这个image_上找像素的灰度值,这个灰度值是估计值

    //而测量值在前一帧上,也就是上面的_measurement,在main()函数中直接赋值给到。

public:

    Eigen::Vector3d x_world_;   // 3D point in world frame

    float cx_=0, cy_=0, fx_=0, fy_=0; // Camera intrinsics

    cv::Mat* image_=nullptr;    // reference image

};

 

int main ( int argc, char** argv )

{

    if ( argc != 2 )

    {

        cout<<"usage: useLK path_to_dataset"<<endl;

        return 1;

    }

    srand ( ( unsigned int ) time ( 0 ) );

    string path_to_dataset = argv[1];

    string associate_file = path_to_dataset + "/associate.txt";

 

    ifstream fin ( associate_file );

 

    string rgb_file, depth_file, time_rgb, time_depth;

    cv::Mat color, depth, gray;

    vector<Measurement> measurements;

    // 相机内参

    float cx = 325.5;

    float cy = 253.5;

    float fx = 518.0;

    float fy = 519.0;

    float depth_scale = 1000.0;

    Eigen::Matrix3f K;

    K<<fx,0.f,cx,0.f,fy,cy,0.f,0.f,1.0f;

 

    Eigen::Isometry3d Tcw = Eigen::Isometry3d::Identity();

 

    cv::Mat prev_color;

    // 我们以第一个图像为参考,对后续图像和参考图像做直接法

//跟稀疏的相比就是在循环中的第一帧取一些点的时候,又稀疏的特征点取成了有明显梯度的点,

//就是增加了一些点。同样,点的增加就会往g2o中增加一些边,在后面绘制对比图时,也没有了线,而只有点,因为太多了,画线看不清了。

    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 )

        {

            // select the pixels with high gradiants

            for ( int x=10; x<gray.cols-10; x++ )//双层循环遍历像素点,边缘的就不要了

                for ( int y=10; y<gray.rows-10; y++ )

                {//这里就是梯度向量delta,可以看一下,

                    //它是以(x,y)像素右减左像素灰度差为x方向梯度值,

                    //  以(x,y)像素下减上像素灰度差为y方向梯度值。

                    //发现(x,y)处的像素梯度跟(x,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 )//如果模长小于50,即任务就是梯度不明显,continue掉,其他的就开始对应深度和空间点,往measurements中push了

                    //说白了跟稀疏的比就是在第一帧中多取了一些点而已。稠密的就是不用说了,所有点全push进measurements

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

                }

            prev_color = color.clone();

            cout<<"add total "<<measurements.size()<<" measurements."<<endl;

            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;

 

        // plot the feature points

        cv::Mat img_show ( color.rows*2, color.cols, CV_8UC3 );

        prev_color.copyTo ( img_show ( cv::Rect ( 0,0,color.cols, color.rows ) ) );

        color.copyTo ( img_show ( cv::Rect ( 0,color.rows,color.cols, color.rows ) ) );

        for ( Measurement m:measurements )

        {

            if ( rand() > RAND_MAX/5 )

                continue;

            Eigen::Vector3d p = m.pos_world;

            Eigen::Vector2d pixel_prev = project3Dto2D ( p ( 0,0 ), p ( 1,0 ), p ( 2,0 ), fx, fy, cx, cy );

            Eigen::Vector3d p2 = Tcw*m.pos_world;

            Eigen::Vector2d pixel_now = project3Dto2D ( p2 ( 0,0 ), p2 ( 1,0 ), p2 ( 2,0 ), fx, fy, cx, cy );

            if ( pixel_now(0,0)<0 || pixel_now(0,0)>=color.cols || pixel_now(1,0)<0 || pixel_now(1,0)>=color.rows )

                continue;

 

            float b = 0;

            float g = 250;

            float r = 0;

            img_show.ptr<uchar>( pixel_prev(1,0) )[int(pixel_prev(0,0))*3] = b;

            img_show.ptr<uchar>( pixel_prev(1,0) )[int(pixel_prev(0,0))*3+1] = g;

            img_show.ptr<uchar>( pixel_prev(1,0) )[int(pixel_prev(0,0))*3+2] = r;

            

            img_show.ptr<uchar>( pixel_now(1,0)+color.rows )[int(pixel_now(0,0))*3] = b;

            img_show.ptr<uchar>( pixel_now(1,0)+color.rows )[int(pixel_now(0,0))*3+1] = g;

            img_show.ptr<uchar>( pixel_now(1,0)+color.rows )[int(pixel_now(0,0))*3+2] = r;

            cv::circle ( img_show, cv::Point2d ( pixel_prev ( 0,0 ), pixel_prev ( 1,0 ) ), 4, cv::Scalar ( b,g,r ), 2 );

            cv::circle ( img_show, cv::Point2d ( pixel_now ( 0,0 ), pixel_now ( 1,0 ) +color.rows ), 4, cv::Scalar ( b,g,r ), 2 );

        }

        cv::imshow ( "result", img_show );

        cv::waitKey ( 0 );

 

    }

    return 0;

}

 

bool poseEstimationDirect ( const vector< Measurement >& measurements, cv::Mat* gray, Eigen::Matrix3f& K, Eigen::Isometry3d& Tcw )

{

    // 初始化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 );

    // g2o::OptimizationAlgorithmGaussNewton* solver = new g2o::OptimizationAlgorithmGaussNewton( solver_ptr ); // G-N

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

}

 

猜你喜欢

转载自blog.csdn.net/qq_40213457/article/details/80995818