ORB-SLAM2实时点云地图构建
首先要安装PCL
sudo apt install libpcl-dev
编译
有几处需要改动:
CMakeLists.txt中 "-march=native"
cmake
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native")
Thirdparty/g2o/CMakeLists.txt 中 "-march=native"
cmake
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -march=native")
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -march=native")
在CMakeLists.txt中以下修改
cmake
# Check C++11 or C++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_definitions(-DCOMPILEDWITHC11)
message(STATUS "Using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_definitions(-DCOMPILEDWITHC0X)
message(STATUS "Using flag -std=c++0x.")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
# 添加下面一行(如果不添加c++11可以成功编译则不需要)
set(CMAKE_CXX_STANDARD 14)
...
...
...
之后将ORB_SLAM2目录下的build、/Thirdparty/DBoW2/build及Thirdparty/g2o/build文件夹删除,然后就可以编译了
bash
sudo chmod +x .build.sh
./build.sh
不出意外的话,至此就可以成功运行程序了
bash
./bin/rgbd_tum ./Vocabulary/ORBvoc.txt ./Examples/RGB-D/TUM1.yaml ../../../slam_data/rgbd_dataset_freiburg1_desk ../../../slam_data/rgbd_dataset_freiburg1_desk/associations.txt
至于vocabulary文件用txt或是bin都可以,高翔博士在这里有读取bin文件的改动
如何是完全按照高翔博士的改法,TUM1.yaml多了
PointCloudMapping.Resolution: 0.01
在System.cc中有读取此值
float resolution = fsSettings["PointCloudMapping.Resolution"];
...
mpPointCloudMapping = make_shared<PointCloudMapping>( resolution );
如果TUM1.yaml中没有PointCloudMapping.Resolution: 0.01,上面两行也可以直接改为,完全是可以的
mpPointCloudMapping = make_shared<PointCloudMapping>(0.01);
后面的数据集和rgb-depth关联文件路径改为自己的就可以了
此时得到的是黑白点云地图,并且没有保存
改善
将点云地图改为彩色,并保存
需进行以下修改:
1.Tracking.h
c++
// Current Frame
Frame mCurrentFrame;
cv::Mat mImRGB; //<added>添加这一行!
cv::Mat mImGray;
cv::Mat mImDepth; // adding mImDepth member to realize pointcloud view
c++
cv::Mat Tracking::GrabImageRGBD(const cv::Mat &imRGB,const cv::Mat &imD, const double ×tamp)
{
mImRGB = imRGB;//<added>添加这一行
mImGray = imRGB;
mImDepth = imD;
...
}
void Tracking::CreateNewKeyFrame()
{
...
// insert Key Frame into point cloud viewer
// mpPointCloudMapping->insertKeyFrame( pKF, this->mImGray, this->mImDepth );
mpPointCloudMapping->insertKeyFrame( pKF, this->mImRGB, this->mImDepth ); //将上面一行改为这一行
...
}
这里是保存点云地图的改动
首先是加入头文件
#include <pcl/io/pcd_io.h>
然后是调用地图保存函数
c++
void PointCloudMapping::viewer()
{
...
for ( size_t i=lastKeyframeSize; i<N ; i++ )
{
PointCloud::Ptr p = generatePointCloud( keyframes[i], colorImgs[i], depthImgs[i] );
*globalMap += *p;
}
//保存地图,这里为了管理,新建了pcd文件夹,将所有保存的pcd放入里面
pcl::io::savePCDFileBinary("/pcd/vslam.pcd", *globalMap);
...
}
参考:https://github.com/gaoxiang12/ORBSLAM2_with_pointcloud_map/issues