代码链接:GitHub - gaoxiang12/slambookContribute to gaoxiang12/slambook development by creating an account on GitHub.https://github.com/gaoxiang12/slambook今天在编译高博视觉SLAM十四讲第一版的第八章代码时候出现了两个错误,虽然解决了,但是不是道为什么会这样,本来是买的第二版的书,但是在B站上看到的视频和第二版不一样,于是找出来编译看看,里面有代码需要修改,修改后的代码如下:

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上的半稠密直接法 ********************************************/// 一次测量的值,包括一个世界坐标系下三维点与一个灰度值
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
// an unary edge with one vertex SE3Expmap (the pose of camera)
class EdgeSE3ProjectDirect: public BaseUnaryEdge< 1, double, VertexSE3Expmap>
{
public:EIGEN_MAKE_ALIGNED_OPERATOR_NEWEdgeSE3ProjectDirect() {}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] );Eigen::Vector3d x_local = v->estimate().map ( x_world_ );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 imageif ( 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;}}// plus in manifoldvirtual 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 bookdouble 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 translationEigen::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 ){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]);}
public:Eigen::Vector3d x_world_;   // 3D point in world framefloat cx_=0, cy_=0, fx_=0, fy_=0; // Camera intrinsicscv::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;// 我们以第一个图像为参考,对后续图像和参考图像做直接法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++ ){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 ) );}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 pointscv::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 )
{// 初始化g2otypedef 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-Mstd::unique_ptr<DirectBlock::LinearSolverType> linearSolver ( new g2o::LinearSolverDense<DirectBlock::PoseMatrixType>());std::unique_ptr<DirectBlock> solver_ptr ( new DirectBlock ( std::move(linearSolver)));g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( std::move(solver_ptr));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();
}

direct_sparse.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上的半稠密直接法 ********************************************/// 一次测量的值,包括一个世界坐标系下三维点与一个灰度值
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
// an unary edge with one vertex SE3Expmap (the pose of camera)
class EdgeSE3ProjectDirect: public BaseUnaryEdge< 1, double, VertexSE3Expmap>
{
public:EIGEN_MAKE_ALIGNED_OPERATOR_NEWEdgeSE3ProjectDirect() {}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] );Eigen::Vector3d x_local = v->estimate().map ( x_world_ );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 imageif ( 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;}}// plus in manifoldvirtual 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 bookdouble 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 translationEigen::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 ){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]);}
public:Eigen::Vector3d x_world_;   // 3D point in world framefloat cx_=0, cy_=0, fx_=0, fy_=0; // Camera intrinsicscv::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;// 我们以第一个图像为参考,对后续图像和参考图像做直接法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++ ){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 ) );}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 pointscv::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 )
{// 初始化g2otypedef 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-Mstd::unique_ptr<DirectBlock::LinearSolverType> linearSolver ( new g2o::LinearSolverDense<DirectBlock::PoseMatrixType>());std::unique_ptr<DirectBlock> solver_ptr ( new DirectBlock ( std::move(linearSolver)));g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( std::move(solver_ptr));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();
}

CMakeLists.txt:

cmake_minimum_required( VERSION 2.8 )
project( directMethod )#set( CMAKE_BUILD_TYPE Release )
#set( CMAKE_CXX_FLAGS "-std=c++14 -O3" )
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++14 ${SSE_FLAGS} -g -O3 -march=native")
#set( CMAKE_CXX_FLAGS "-std=c++14 -O3" )# 添加cmake模块路径
list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules )find_package( OpenCV )
include_directories( ${OpenCV_INCLUDE_DIRS} )find_package( G2O )
include_directories( ${G2O_INCLUDE_DIRS} ) include_directories(${OpenCV_INCLUDE_DIRS}${G2O_INCLUDE_DIRS}${Sophus_INCLUDE_DIRS}"/usr/include/eigen3/"${Pangolin_INCLUDE_DIRS}
)set( G2O_LIBS g2o_core g2o_types_sba g2o_solver_csparse g2o_stuff g2o_csparse_extension
)add_executable( direct_sparse direct_sparse.cpp )
target_link_libraries( direct_sparse ${OpenCV_LIBS} ${G2O_LIBS})add_executable( direct_semidense direct_semidense.cpp )
target_link_libraries( direct_semidense ${OpenCV_LIBS} ${G2O_LIBS} )

double free or corruption (out)Aborted (core dumped)

解决办法:将set(CMAKE_CXX_FLAGS "-std=c++14 ${SSE_FLAGS} -g -O3 -march=native")
改为set( CMAKE_CXX_FLAGS "-std=c++14 -O3" )就不会报错了,也不知道为什么会这样。

然后在这台电脑(AMD)电脑上执行时,

又出现了 Segmentation fault (core dumped)这个错误

然后我换了个电脑,是i7的电脑,执行时问题就解决了,也不知道为什么会这样,有可能是平台的原因。

Segmentation fault (core dumped) 和double free or corruption (out)Aborted (core dumped)相关推荐

  1. OpenCV (c++)使用KDTree时,得到正确结果后报Segmentation fault (core dumped)

    一.给出问题 构建包含以下文件的工程(opencv_test) 各文件内容给出如下: # CMakeLists.txt cmake_minimum_required(VERSION 2.18) pro ...

  2. c++ Segmentation fault (core dumped) 的一个实例

    说明:找到vector前一半元素中等于some_val的那些,然后在这些元素的前面插入它的2倍. #include <iostream> #include <string> # ...

  3. 人工机器:jetsonnano推理时出现 Segmentation fault(core dumped)

    前期配置:tensorrt+tensorflow+object_detection编译安装填坑教程!! 1.在使用此工程:https://github.com/dusty-nv/jetson-infe ...

  4. Segmentation fault (core dumped) -llinux系统内存错误报错信息

    目录 1 问题原因 (1)内存访问越界 (2)多线程程序使用了线程不安全的函数. (3)多线程读写的数据未加锁保护. (5)堆栈溢出. 2 使用GDB查看core文件 3 使用GDB调试程序 返回目录 ...

  5. yum 出错,提示Segmentation Fault (core Dumped) 的解决办法

    CentOS5.5部署Zlib导致yum使用不了,报错Yum Segmentation Fault (core Dumped) . 在一台CentOS.5.5的机器上使用Yum时突然报错,提示Yum ...

  6. labelImg(pyqt4 )出现错误(segmentation fault(core dumped) )

    在配置labelImg的过程中,最后执行时候出现了一个错误segmentation fault(core dumped) . 解决方法: 将安装的pyqt4的版本从4.11.4降到4.11.3版本,会 ...

  7. mysql core dumped_关于Segmentation fault (core dumped)几个简单问题的整理

    有的程序可以通过编译,但在运行时会出现Segment fault(段错误).这通常都是指针错误引起的.但这不像编译错误一样会提示到文件一行,而是没有任何信息.一种办法是用gdb的step, 一步一步寻 ...

  8. 【ARM】程序快速定位segmentation fault core dumped错误

    1.应用场景 ARM开发过程中经常进程运行着出现段错误,这时候单纯靠加日志打log效率太低.使用gdb的话,由于APP进程太多,生成的core的文件特别大,而且gdb在arm板子也不好单步调试,不太友 ...

  9. dmrman恢复数据报错Segmentation fault (core dumped)

    达梦技术社区:https://eco.dameng.com [场景说明] 使用dmrman恢复数据库,但是这种报错不限于DM数据库场景,其他应用也可能出现 [报错信息] Segmentation fa ...

最新文章

  1. 最详细的----->一维数组实现杨辉三角
  2. 卷积神经网络基础:(8)递归神经网络RNN
  3. mysql unsigned zerofill_Mysql中Unsigned和Zerofill数据型的使用(细节也很重要啊)
  4. Java学习之while语句
  5. Sublime配置与各种插件
  6. 如何改变XCode的默认设置
  7. python怎么命名未知数_Python4要来了?快来看看Python之父怎么说
  8. KVM,QEMU,libvirt入门学习笔记【转】
  9. SQOOP --hive-import 错误(Sqoop Hive exited with status 1)及解决
  10. vivaldi浏览器_两款可以提升效率的网络浏览器
  11. matlab xls转csv,使用python或Matlab将csv文件中的数据转换为csv文件
  12. 动态心电图信息存储(三)
  13. 找不到好看的电影就看《IMDB排名前500电影》
  14. 苹果手机备忘录html转pdf,苹果手机自带的扫描仪,一键便能转成PDF文件,你不会才知道吧...
  15. vscode 是干什么用的_vscode是干嘛用的
  16. 阿里云代码管理平台 Teambition Codeup(行云)亮相,为企业代码安全护航
  17. 西瓜书第四章习题及答案
  18. win10 EFI分区迁移至新盘的方法
  19. 钢铁侠材质制作——3、基础光照模型实现
  20. 用JSP/Servlet应用开发一个简单的考试报名系统

热门文章

  1. Android自定义人脸识别框,android自定义虹软人脸识别框/人脸抓拍框/人脸追踪框...
  2. python自相关函数提取基音周期_Python语音基础操作--4.2基音周期检测
  3. fabric8镜像的deployments脚本
  4. 阿里云对象存储以及api
  5. 《Gartner2016年度新兴技术成熟度曲线》全解读
  6. 国内外最顶级的12大看板工具
  7. 关于python3的input函数和int()强制转换
  8. Muti-Similarity Loss:考虑了batch中整体距离分布的对比损失函数
  9. 【传感器大赏】压电薄膜震动传感器
  10. 歌词欣赏《一程山水一程歌》