本文是基于Ubuntu 20.04及OpenCV 4.6.0成功运行ORB-SLAM2,并在开源数据集上进行了测试。由于OpenCV和其他依赖库的版本较新,编译过程会出现一些问题,需要修改部分代码和CMakeLists.txt文件,这里做一个记录,也希望能帮到有需要的小伙伴。
开始尝试安装Eigen3.4.0和Pangolin-0.8版本,后经查阅资料发现因C++版本问题只能安装Pangolin-0.6,切换为Pangolin-0.6之后,测试可用。
一、ORB-SLAM2环境配置
1. ubuntu20.04安装
安装过程可以看之前的总结Ubuntu20.04+GeForce RTX 2080 SUPER+cuda11.1+cudnn8.0.4+openCV4.4.0编译,包括详细的ubuntu、cuda、opencv安装过程。
2. 依赖库安装
(1) Eigen3安装与卸载
- 安装
Eigen3可以通过apt仓库直接用命令安装,或者下载最新版源码自己编译安装,源码安装可以选择自己想要安装的版本
- 命令安装
sudo apt install libeieng3-dev # 命令安装的版本是 eigen-3.3.7
- 源码安装
从官网下载最新版的Eigen3并解压,这里安装的是eigen-3.4.0
cd eigen-3.4.0
mkdir build
cd build
cmake ..
make
sudo make install
查看Eigen版本
pkg-config --modversion eigen3
- 卸载
- 通过apt方式安装的卸载方法
通过apt安装的eigen库可以直接使用apt remove命令进行卸载,如果卸载不干净可以使用locate命令定位所有eigen目录,并手动删除/usr/local/和/usr/include/目录下的eigen目录,通常不需要。
sudo apt remove libeigen3
sudo updatedb
locate eigen
- 源码安装的卸载方法
源码安装就需要手动删除/usr/local/include/等目录下的eigen3目录
sudo updatedb
locate eigen3 # 查看eigen3的位置
sudo rm -rf /usr/include/eigen3
sudo rm -rf /usr/lib/cmake/eigen3
sudo rm -rf /usr/local//include/eigen3
sudo rm -rf /usr/share/doc/libeigen3-dev
sudo rm -rf /usr/local/share/pkgconfig/eigen3.pc /usr/share/pkgconfig/eigen3.pc /var/lib/dpkg/info/libeigen3-dev.list /var/lib/dpkg/info/libeigen3-dev.md5sums
如果找不到 updatedb命令,通过以下方式安装:
sudo apt install mlocate
重新查直eigen3的位置
sudo updatedb
locate eigen3 # 重新查看eigen3的位置,已经没有了
(2) Pangolin安装与卸载
- 安装Pangolin
- 安装依赖项
sudo apt install libglew-dev
sudo apt install libboost-dev libboost-thread-dev libboost-filesystem-dev
sudo apt install libboost-all-dev
sudo apt install libwayland-dev wayland-protocols
sudo apt install libxkbcommon-dev
sudo apt install libegl1-mesa-dev
sudo apt install ninja-build
- 下载Pangolin源码
可以直使用命令下载
git clone https://github.com/stevenlovegrove/Pangolin.git
或者进入github网站,从tags标签下载对应的版本并解压,本文下载的是v0.6版本。
- 编译安装
cd Pangolin-0.6
mkdir build
cd build
cmake ..
make
sudo make install
可以在/usr/local/include/和/usr/local/lib/目录下找到pangolin相关的目录及库文件,Pangolin安装成功。
- Pangolin测试
cd Pangolin-0.8/build/examples/HelloPangolin/
./HelloPangolin
得到下图所示画面,安装成功。
2. 卸载Pangolin
sudo updatedb
locate pangolin
sudo rm -rf /usr/local/include/pangolin
sudo rm /usr/local/lib/libpango_*.so
(3) OpenCV 4.6.0
-
安装OpenCV
安装过程可以看这篇文章Ubuntu20.04+GeForce RTX 2080 SUPER+cuda11.1+cudnn8.0.4+openCV4.4.0编译,里面有详细的步骤和一些报错的解决方法。有一些文件可能比较旧了,可以从这里下载,根据报错提示的版本切换到对应的分支进行下载就可以,其他的操作与上面文章的方法相同。 -
报错及解决方法
- make过程报错:
/home/ubuntu/software/opencv-4.6.0/modules/imgcodecs/src/grfmt_webp.cpp:47:10: fatal error: webp/decode.h: No such file or directory
47 | #include <webp/decode.h>
报错原因: 缺少webp库
解决方法: 安装缺少的库
sudo apt install wdbp libwebp-dev
- 卸载OpenCV
- 进入安装OpenCV时的build目录,并进行卸载操作
cd build
sudo make uninstall
- 删除/usr目录下OpenCV相关的文件
sudo rm -r /usr/local/include/opencv4/
sudo rm -r /usr/include/opencv4/
sudo rm -r /usr/local/share/opencv4/
sudo rm -r /usr/local/share/licenses/opencv4/
sudo rm -r /usr/share/opencv4/
sudo rm -r /usr/local/lib/cmake/opencv4/
如果安装了Anaconda,卸载OpenCV后重新安装时需要将.bashrc文件中Anaconda的环境变量。因为Anaconda的gcc版本比系统默认的版本要低,安装OpenCV时可能会出现报错。
gedit ~/.bashrc
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
#__conda_setup="$('/home/ubuntu/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
#if [ $? -eq 0 ]; then
# eval "$__conda_setup"
#else
# if [ -f "/home/ubuntu/anaconda3/etc/profile.d/conda.sh" ]; then
# . "/home/ubuntu/anaconda3/etc/profile.d/conda.sh"
# else
# export PATH="/home/ubuntu/anaconda3/bin:$PATH"
# fi
#fi
#unset __conda_setup
# <<< conda initialize <<<
(4) DBoW2和g2o
这两个库直接使用ORB-SLAM2工程中自带的就可以,不需要自己安装。
(5) ROS(可选)
如果准备做机器人相关工作,建议安装,本文安装的是noetic版本。ROS安装过程参考官网即可,也可以看我之间总结的这篇文章ROS Noetic完整安装及测试
3. ORB-SLAM2下载
git clone https://github.com/raulmur/ORB_SLAM2.git ORB_SLAM2
4. ORB-SLAM2编译
- 编译代码
cd ORB-SLAM2
chmod +x build.sh
./build.sh
以上是ORB-SLAM2官方提供的编译脚本,但是因为本文安装的依赖库要比ORB-SLAM2官方要求的版本新,所以会出现一些报错,可以根据build.sh脚本中的顺序单独进行编译,并解决报错。
- 报错及解决方法
(1) DBoW2 cmake error
CMake Error at CMakeLists.txt:31 (message):
OpenCV > 2.4.3 not found.
报错原因:OpenCV
解决方法:将DBoW2目录下的 CMakeLists.txt 文件中的 find_package(OpenCV 3.0 QUIET) 修改为
find_package(OpenCV 4.6.0 QUIET)
# 或者直接去掉版本号
find_package(OpenCV QUIET)
ORB_SLAM2 same error
(2) 关于 #include <opencv/cv.h> 的错误 可能会出现找不到"opencv/cv.h"的报错
ORB_SLAM2/include/ORBextractor.h:26:10: fatal error: opencv/cv.h: No such file or directory
26 | #include <opencv/cv.h>
错误原因: opencv2切换为opencv4的版本问题 新的OpenCV没有cv.h文件了
解决方法: 将ORBextractor.h头文件 opencv/cv.h 修改为
#include <opencv2/opencv.hpp>
/// 或
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/highgui/highgui_c.h>
修改ORBextractor.h文件中包含的头文件
将
#include <opencv/cv.h>
修改为
#include <opencv2/imgproc.hpp>
(3) usleep()未声明
error: ‘usleep’ was not declared in this scope
报错原因:缺少unistd.h头文件
解决方法:在报错对应的每个c++文件中或直接在system.h文件中加入
#include <unistd.h>
(4) CvMat 未声明 相关的错误
ORB_SLAM2/include/PnPsolver.h:96:15: error: ‘CvMat’ has not been declared
96 | void fill_M(CvMat * M, const int row, const double * alphas, const double u, const double v);
报错原因: 版本切换
解决方法: 添加头文件
#include <opencv2/core/types_c.h>
(5) cvtColor 参数相关错误
ORB_SLAM2/src/Tracking.cc:176:38: error: ‘CV_RGB2GRAY’ was not declared in this scope
176 | cvtColor(mImGray,mImGray,CV_RGB2GRAY);
ORB_SLAM2/src/FrameDrawer.cc:75:24: error: ‘CV_GRAY2BGR’ was not declared in this scope
75 | cvtColor(im,im,CV_GRAY2BGR);
报错原因: 版本切换
解决方法: 将Tracking.cc和FrameDrawer.cc中的相关参数 CV_RGB2GRAY 修改为 cv::COLOR_RGB2GRAY。有很多地方需要修改
(6) LoopClosing.h中的报错
/usr/include/c++/9/bits/stl_map.h: In instantiation of ‘class std::map<ORB_SLAM2::KeyFrame*, g2o::Sim3, std::less<ORB_SLAM2::KeyFrame*>, Eigen::aligned_allocator<std::pair<const ORB_SLAM2::KeyFrame*, g2o::Sim3> > >’:
ORB_SLAM2/src/LoopClosing.cc:438:21: required from here
/usr/include/c++/9/bits/stl_map.h:122:71: error: static assertion failed: std::map must have the same value_type as its allocator
122 | static_assert(is_same<typename _Alloc::value_type, value_type>::value,
报错原因: 使用std::map时模板参数类型不匹配
解决方法: 修改LoopClosing.h文件中的49和50行
将
typedef map<KeyFrame*,g2o::Sim3,std::less<KeyFrame*>,
Eigen::aligned_allocator<std::pair<const KeyFrame*, g2o::Sim3> > > KeyFrameAndPose;
修改为
typedef map<KeyFrame*,g2o::Sim3,std::less<KeyFrame*>,
Eigen::aligned_allocator<std::pair<KeyFrame *const, g2o::Sim3> > > KeyFrameAndPose;
(7) PnPsolver.cc中的相关错误
ORB_SLAM2/src/PnPsolver.cc:388:17: error: ‘cvCreateMat’ was not declared in this scope; did you mean ‘cvCreateSparseMat’?
388 | CvMat * PW0 = cvCreateMat(number_of_correspondences, 3, CV_64F);
ORB_SLAM2/src/PnPsolver.cc:399:3: error: ‘cvMulTransposed’ was not declared in this scope
399 | cvMulTransposed(PW0, &PW0tPW0, 1);
ORB_SLAM2/src/PnPsolver.cc:400:33: error: ‘CV_SVD_MODIFY_A’ was not declared in this scope; did you mean ‘CV_HAL_SVD_MODIFY_A’?
400 | cvSVD(&PW0tPW0, &DC, &UCt, 0, CV_SVD_MODIFY_A | CV_SVD_U_T);
ORB_SLAM2/src/PnPsolver.cc:400:51: error: ‘CV_SVD_U_T’ was not declared in this scope
400 | cvSVD(&PW0tPW0, &DC, &UCt, 0, CV_SVD_MODIFY_A | CV_SVD_U_T);
ORB_SLAM2/src/PnPsolver.cc:400:3: error: ‘cvSVD’ was not declared in this scope
400 | cvSVD(&PW0tPW0, &DC, &UCt, 0, CV_SVD_MODIFY_A | CV_SVD_U_T);
ORB_SLAM2/src/PnPsolver.cc:402:3: error: ‘cvReleaseMat’ was not declared in this scope
402 | cvReleaseMat(&PW0);
ORB_SLAM2/src/PnPsolver.cc:421:26: error: ‘CV_SVD’ was not declared in this scope; did you mean ‘CV_AVX’?
421 | cvInvert(&CC, &CC_inv, CV_SVD);
ORB_SLAM2/src/PnPsolver.cc:421:3: error: ‘cvInvert’ was not declared in this scope; did you mean ‘cvInvSqrt’?
421 | cvInvert(&CC, &CC_inv, CV_SVD);
ORB_SLAM2/src/PnPsolver.cc:596:3: error: ‘cvSetZero’ was not declared in this scope
596 | cvSetZero(&ABt);
ORB_SLAM2/src/PnPsolver.cc:681:3: error: ‘cvSolve’ was not declared in this scope
681 | cvSolve(&L_6x4, Rho, &B4, CV_SVD);
报错原因: opencv版本切换问题
解决方法: 在PnPsolver.cc文件中添加头文件
#include <opencv2/imgproc/types_c.h>
(8) Sim3Solver.cc文件中相关错误
ORB_SLAM2/src/Sim3Solver.cc:217:22: error: ‘CV_REDUCE_SUM’ was not declared in this scope
217 | cv::reduce(P,C,1,CV_REDUCE_SUM);
报错原因: CV_REDUCE_SUM 这个变量没定义,opencv3到opencv4切换带来的错误
解决方法: 在 Sim3Solver.cc 文件中添加头文件
#include <opencv2/core/core_c.h>
(9) imread CV_LOAD_IMAGE_UNCHANGED
ORB_SLAM2/Examples/Monocular/mono_euroc.cc:73:48: error: ‘CV_LOAD_IMAGE_UNCHANGED’ was not declared in this scope
73 | im = cv::imread(vstrImageFilenames[ni],CV_LOAD_IMAGE_UNCHANGED);
报错原因: opencv3到opencv4切换带来的错误,没有 CV_LOAD_IMAGE_UNCHANGED 的定义
解决方法: 将 mono_euroc.cc mono_kitti.cc mono_tum.cc rgbd_tum.cc stereo_euro.cc stereo_kitti.cc CV_LOAD_IMAGE_UNCHANGED 修改为 cv::IMREAD_UNCHANGED
(10) 由于Eigen版本较新,会出现以下警告信息:
ORB_SLAM2/Thirdparty/g2o/g2o/types/…/core/base_vertex.h:62:74: warning: ‘Eigen::AlignedBit’ is deprecated [-Wdeprecated-declarations]
62 | typedef Eigen::Map<Matrix<double, D, D>, Matrix<double,D,D>::Flags & AlignedBit ? Aligned : Unaligned > HessianBlockType;
这里因为在新版的Eigen中Eigen::AlignedBit常量将被弃用,不建议再使用。目前还未找到解决方法,但不影响编译,可以正常使用
(11) 加载canberra-gtk-module失败
Gtk-Message: 11:07:05.114: Failed to load module “canberra-gtk-module”
报错原因: 没有安装canberra-gtk-module
解决方法: 使用如下命令安装即可
sudo apt install libcanberra-gtk-module
(12) 运行时报段错误(原ORB-SLAM2和ORBSLAM2_with_pointcloud都可能报错)
Segmentation fault (core dumped)
解决方法: 删掉ORB_SLAM2主目录及DBoW2和g2o目录中CMakeLists.txt文件中的 -march=native
(13) 其他的报错这没有遇到,如果遇到可以看后面参考中的几篇文章
二、开源数据集测试
1、单目数据集
- TUM
./Example/Monocular/mono_tum Vocabulary/ORBvoc.txt Examples/Monocular/TUM2.yaml ../datasets/TUM/Handheld_SLAM/rgbd_dataset_freiburg2_desk
- KITTI
./Examples/Monocular/mono_kitti Vocabulary/ORBvoc.txt Examples/Monocular/KITTI00-02.yaml ../datasets/KITTI/data_odometry_gray/02
- EuRoC
./Examples/Monocular/mono_euroc Vocabulary/ORBvoc.txt Examples/Monocular/EuRoC.yaml ../datasets/EuRoC/vicon_room1/V1_01_easy/mav0/cam0/data Examples/Monocular/EuRoC_TimeStamps/V101.txt
2、RGB-D数据集
- TUM
- 下载Python脚本associate.py,用于关联RGB图像与depth图像
- 执行脚本生成关联文件
python associate.py rgb.txt depth.txt > associations.txt
由于Python2和Python3语法的差别,可能会报错:
File “associate.py”, line 119, in
matches = associate(first_list, second_list,float(args.offset),float(args.max_difference))
File “associate.py”, line 96, in associate
first_keys.remove(a)
AttributeError: ‘dict_keys’ object has no attribute ‘remove’
解决方法: 把dict强制转换为list,将86、87行的内容:
first_keys = first_list.keys()
second_keys = second_list.keys()
修改为:
first_keys = list(first_list.keys())
second_keys = list(second_list.keys())
或者也可以在94行for循环之前加入以下内容:
first_keys = list(first_keys)因
second_keys = list(second_keys)
两种方法任选一种都可以
- 运行ORB-SLAM2代码
./Examples/RGB-D/rgbd_tum Vocabulary/ORBvoc.txt Examples/RGB-D/TUM1.yaml ../datasets/TUM/Testing_and_Debugging/rgbd_dataset_freiburg1_xyz ../datasets/TUM/Testing_and_Debugging/rgbd_dataset_freiburg1_xyz/associations.txt
3、双目数据集
- KITTI
./Examples/Stereo/stereo_kitti Vocabulary/ORBvoc.txt Examples/Stereo/KITTI04-12.yaml ../datasets/KITTI/data_odometry_gray/07
- EuRoC
./Examples/Stereo/stereo_euroc Vocabulary/ORBvoc.txt Examples/Stereo/EuRoC.yaml ../datasets/EuRoC/vicon_room1/V1_01_easy/mav0/cam0/data ../datasets/EuRoC/vicon_room1/V1_01_easy/mav0/cam1/data Examples/Stereo/EuRoC_TimeStamps/V101.txt
三、ROS环境下运行
1、安装ROS
ROS安装过程参考官网即可,也可以看我之间总结的这篇文章ROS Noetic完整安装及测试。
2、编译ROS节点
- 将"ORB_SLAM2/Examples/ROS"添加到ROS环境变量
gedit ~/.bashrc # 打开.bashrc文件
# 添加以下内容
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:PATH/ORB_SLAM2/Examples/ROS # 注意将PATH替换为自己的路径
source ~/.bashrc # 很重要
- 编译
cd Example/ROS/ORB_SLAM2
mkdir build
cd build
cmake .. -DROS_BUILD_TYPE=Release
make -j
- 报错及解决方法
(1) cmake报错
CMake Error at CMakeLists.txt:37 (message):
OpenCV > 2.4.3 not found.
报错原因: OpenCV版本问题
解决方法: 修改"Examples/ROS/ORB_SLAM2"目录下的"CMakeLists.txt"文件
find_package(OpenCV 3.0 QUIET)
# 修改为
find_package(OpenCV 4.6.0 QUIET)
2、数据集测试
2.1 单目节点(Monocular Node)
- TUM
- TUM数据集可以直接下载数据集对应的rosbag包
- 查看rosbag包的详细信息
rosbag info rgbd_dataset_freiburg1_desk.bag
可以看到该数据包发布RGB图像的ROS话题为/camera/rgb/image_color。
- 修改ros_mono.cc源文件,默认订阅的RGB图像话题为/camera/image_raw,需要修改为rosbag数据包所发布的话题名
/// 原始订阅的话题
ros::Subscriber sub = nodeHandler.subscribe("/camera/image_raw", 1, &ImageGrabber::GrabImage, &igb);
/// 修改为
ros::Subscriber sub = nodeHandler.subscribe("/camera/rgb/image_color", 1, &ImageGrabber::GrabImage, &igb);
也可以将订阅的话题以命令行参数的形式传入,就不用每次修改代码得新编译了,需要修改以下内容:
/// 1. 增加一个参数
if(argc != 3)
{
cerr << endl << "Usage: rosrun ORB_SLAM2 Mono path_to_vocabulary path_to_settings" << endl;
ros::shutdown();
return 1;
}
/// 修改为
if(argc != 4)
{
cerr << endl << "Usage: rosrun ORB_SLAM2 Mono path_to_vocabulary path_to_settings ros_topic" << endl;
ros::shutdown();
return 1;
}
/// 2. 订阅话题
ros::Subscriber sub = nodeHandler.subscribe("/camera/rgb/image_color", 1, &ImageGrabber::GrabImage, &igb);
/// 修改为
ros::Subscriber sub = nodeHandler.subscribe(argv[3], 1, &ImageGrabber::GrabImage, &igb);
- 修改完成后重新编译代码,需要在./Example/ROS/ORB-SLAM/目录下进行
mkdir build
cd build
cmake .. -DROS_BUILD_TYPE=Release
make
- 回放ROS bag数据包,需要先运行roscore启动ROS Master
rosbag play --pause -l rgbd_dataset_freiburg1_desk.bag # 回放rosbag数据包
# --pause表示可以使用空格键控制启动/暂停回放
# -l表示循环回放
# 按”s“键可以使数据回退一步
- 启动单目ROS节点
roscd ORB_SLAM2 # 进入ORB_SLAM2节点目录
cd ../../..
rosrun ORB_SLAM2 Mono Vocabulary/ORBvoc.txt Examples/Monocular/TUM1.yaml /camera/rgb/image_color
- KITTI
- 创建ROS功能包,将KITTI的图像数据转为rosbag数据包
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdio>
#include <algorithm>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ros/ros.h>
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <sensor_msgs/Image.h>
#include <std_msgs/Time.h>
#include <std_msgs/Header.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/opencv.hpp>
std::vector<std::string> Dir(const char* path, const char* right, bool sorted) {
DIR *dirp;
struct dirent *entry;
std::vector<std::string> ret;
if((dirp = opendir(path)) != NULL) {
while((entry = readdir(dirp)) != NULL) {
std::string name(entry->d_name);
std::string r(right);
if((name.length() >= r.length()) && (name.substr(name.length() - r.length()).compare(r) == 0)) {
ret.push_back(std::string(path) + "/" + entry->d_name);
}
}
closedir(dirp);
}
if(sorted)
sort(ret.begin(), ret.end());
return ret;
}
int main(int argc, char** argv) {
ros::init(argc, argv, "ImageToBag");
if (argc != 5)
{
std::cerr << "Usage: rosrun kitti_pub_bag ImageToBag <path to image directory> <image extension .ext> \
<frequency> <path to output bag>" << std::endl;
return -1;
}
ros::start();
std::vector<std::string> filenames = Dir(argv[1], argv[2], true);
std::cout << "Images: " << filenames.size() << std::endl;
double freq = atof(argv[3]);
rosbag::Bag bag_out(argv[4], rosbag::bagmode::Write);
ros::Time t = ros::Time::now();
const float T = 1.0f / freq;
ros::Duration d(T);
for(size_t i = 0; i < filenames.size(); ++i) {
if(!ros::ok())
break;
cv::Mat left_img = cv::imread(filenames.at(i), cv::IMREAD_UNCHANGED);
cv_bridge::CvImage cvImage;
cvImage.image = left_img;
cvImage.encoding = sensor_msgs::image_encodings::MONO8; /// ROS中灰度图的编码为 Mono8
cvImage.header.stamp = t;
bag_out.write("/camera/image/left_gray", ros::Time(t), cvImage.toImageMsg());
t += d;
std::cout << i << " / " << filenames.size() << std::endl;
}
bag_out.close();
ros::shutdown();
return 0;
}
- 编译ROS功能包,生成bag数据包
cd catkin_ws
catkin_make
rosrun kitti_pub_bag image2bag /Your/Path/datasets/KITTI/data_odometry_gray/06/image_0 .png 10 /Your/Path/datasets/KITTI/data_odometry_gray/06/kitti_gray_06.bag
- 回放ROS bag数据包,需要先运行roscore启动ROS Master
rosbag play --pause -l /Your/Path/datasets/KITTI/data_odometry_gray/06/kitti_gray_06.bag
- 启动单目ROS节点
roscd ORB_SLAM2 # 进入ORB_SLAM2节点目录
cd ../../..
rosrun ORB_SLAM2 Mono Vocabulary/ORBvoc.txt Examples/Monocular/KITTI04-12.yaml /camera/image/left_gray
2.2 双目节点(Stereo Node)
2.3 RGB-D节点(RGB-D Node)
3、实时处理相机数据
四、ORB_SLAM2_modified环境配置
1、PCL库安装
sudo apt install libpcl-dev pcl-tools
2、ORB_SLAM2_modified编译
- 编译代码
Thirdparty/DBoW2
rm -rf build
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
Thirdparty/g2o
rm -rf build
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
从原 ORB_SLAM2 中将 Vocabulary 目录复制到 ORB_SLAM2_modified 目录
cd ../../..
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
- 报错及解决方法
(1) 与编译 ORB-SLAM2 时相同的错误与"一"中解决方法相同
(2) PCL相关问题
/usr/include/pcl-1.10/pcl/pcl_config.h:7:4: error: #error PCL requires C++14 or above
7 | #error PCL requires C++14 or above
报错原因: pcl需要c++14标准
解决方法: 在ORB_SLAM2_modified主目录下的CMakeLists.txt文件中加入以下内容
add_compile_options(-std=c++11)
add_compile_options(-std=c++14)
set(CMAKE_CXX_STANDARD 14)
(3) Example/RGB-D/rgbd_my.cc中OpenCV版本相关问题
ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified/Examples/RGB-D/rgbd_my.cc:60:56: error: ‘CV_LOAD_IMAGE_UNCHANGED’ was not declared in this scope
60 | imRGB = cv::imread( (fmt%argv[3]%index).str(), CV_LOAD_IMAGE_UNCHANGED);
的
报错原因: opencv3到opencv4切换带来的错误,没有 CV_LOAD_IMAGE_UNCHANGED 的定义
解决方法: 将 rgbd_my.cc 中的 CV_LOAD_IMAGE_UNCHANGED 修改为 cv::IMREAD_UNCHANGED
(4) 编译pcl1.12.0时的报错(实际没有使用自己编译的pcl1.12.0,直接用apt进行安装的版本为pcl1.10.0):
c++: fatal error: Killed signal terminated program cc1plus
c++: internal compiler error: 已杀死 (program cc1plus)
报错原因: 因为内存不足导致编译中断
解决方法: 创建临时交换空间
# count的大小就是增加的swap空间的大小,1G是块大小,所以交换空间大小为bs * count = 8G
sudo dd if=/dev/zero of=/swapfile bs=1G count=8
# 格式化交换空间
sudo mkswap /swapfile
# 使用创建的交换空间,激活交换空间
sudo swapon /swapfile
# (可选步骤)如果想要持久使用交换空间,打开/etc/fstab写入以下内容
sudo gedit /etc/fstab
# 写入以下内容
/swapfile swap swap defaults 0 0
# 查看交换空间是否激活
sudo swapon --show
sudo free -h
# 取消激活交换空间
sudo swapoff -v /swapfile
# (可选步骤)如果上面选择了持外使用,这里需要从 /etc/fstab 文件中移除交换空间条目 /swapfile swap swap defaults 0 0
# 删除交换空间文件
sudo rm /swapfile
3 运行ORB_SLAM2_modified并保存点云
(1) 运行
./bin/rgbd_tum Vocabulary/ORBvoc.txt ./Examples/RGB-D/TUM1.yaml ../../../datasets/TUM/Testing_and_Debugging/rgbd_dataset_freiburg1_xyz ../../../datasets/TUM/Testing_and_Debugging/rgbd_dataset_freiburg1_xyz/associations.txt
(2) 保存点云
高博原始的代码中并没有保存点云,为方便使用可以在pointcloudmapping.cc文件中加入以下内容保存生成的点云:
/// 包含头文件
#include <pcl/io/pcd_io.h>
PointCloudMapping::viewer()函数
/// 在PointCloudMapping::viewer()函数最后加入
pcl::io::savePCDFile("result.pcd", *tmp);
(3) 安装pcl-tools查看保存的点云
sudo apt install pcl-tools
pcl_viewer result.pcd
(4) 生成彩色点云
高博原始的代码中实时显示的是黑白的点云,看起来不是很直观,通过修改以下代码可以实时显示彩色点云:
- 修改 ORB_SLAM2_modified/include/Tracking.h 文件
// Current Frame
Frame mCurrentFrame;
cv::Mat mImRGB; /// 这里是新加内容,用于显示彩色点云
cv::Mat mImGray;
cv::Mat mImDepth; // adding mImDepth member to realize pointcloud view
- 修改 ORB_SLAM2_modified/src/Tracking.cc 文件,有两个地方需要修改
cv::Mat Tracking::GrabImageRGBD(const cv::Mat &imRGB,const cv::Mat &imD, const double ×tamp)
{
mImRGB = imRGB; /// 新加内容,用于显示彩色点云
mImGray = imRGB;
mImDepth = imD;
...
}
void Tracking::CreateNewKeyFrame()
{
...
mpLocalMapper->SetNotStop(false);
// insert Key Frame into point cloud viewer
/// mpPointCloudMapping->insertKeyFrame( pKF, this->mImGray, this->mImDepth );
mpPointCloudMapping->insertKeyFrame( pKF, this->mImRGB, this->mImDepth ); /// 修改内容,将原来的mImGray改为mImRGB
mnLastKeyFrameId = mCurrentFrame.mnId;
mpLastKeyFrame = pKF;
}
文章来源:https://www.toymoban.com/news/detail-773465.html
参考
[1] 在Ubuntu中安装Eigen3库
[2] ubuntu16.04卸载eigen
[3] ubuntu系统安装pangolin
[4] Ubuntu20.04安装Pangolin(视觉SLAM十四讲)
[5] ubuntu20.04装ORB-slam2 和ORB-slam2-ros
[6] ORB-SLAM2编译、安装等问题汇总大全(Ubuntu20.04、eigen3、pangolin0.5、opencv4.5.5
[7] 视觉SLAM十四讲 Ubuntu20.04 Pangolin 环境配置
[8] 【ROS】ORB-SLAM2配置安装及所遇到的问题解决
[9] _ZN5boost6system1
[10] orbslam2 安装与运行
[11] ORB_SLAM2编译及试运行(含ROS)
[12] 使用TUM的associate.py脚本报错
[13] Ubuntu 20.04配置ORB-SLAM2和ORB-SLAM3运行环境+ROS实时运行ORB-SLAM2+Gazebo仿真运行ORB-SLAM2+各种相关库的安装
[14] 高翔ORB-SLAM2稠密建图编译(添加实时彩色点云地图+保存点云地图)
[15] VSLAM框架:ORB_SLAM2采用ROS Bag获取数据
[16] ORB-SLAM2作者写的BagFromImages文章来源地址https://www.toymoban.com/news/detail-773465.html
到了这里,关于ORB-SLAM2环境配置及运行的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!