ORB SLAM 2 demo 復(fù)現(xiàn)(普通模式 + ROS 模式)

參考網(wǎng)址:https://github.com/raulmur/ORB_SLAM2

按照上述網(wǎng)址中的官方流程,ORB SLAM 2 demo 的復(fù)現(xiàn)還算比較順利,但是也遇到了一些小坑,本文把所有復(fù)現(xiàn)流程記錄下來,方便以后查閱,或許也可以幫助其他讀者解決 demo 復(fù)現(xiàn)中遇到的問題。

這里的普通模式是指直接運行編譯之后的可執(zhí)行文件,ROS 模式是以 ros node 的形式執(zhí)行,后者在多機通訊環(huán)境中很方便,例如多機器人協(xié)同建圖,機器人與 PC 機交互。

系統(tǒng)平臺

  • Ubuntu 18.04
    官網(wǎng)里提到:在 Ubuntu 12.04, 14.04, 16.04 平臺上測試過,沒有提到 18.04,根據(jù)我們的測試,用 18.04 也沒問題

安裝依賴

OpenCV

這里選擇安裝 OpenCV 3.2 版本,具體安裝流程可以參考我們的另外一篇文章

DBoW2 和 g2o

這兩個庫都在 Thirdparty 文件夾中,后邊會隨著 ORB SLAM 2 一起編譯,這里先不管它們

ROS

這里選擇了與 Ubuntu 18.04 比較匹配的 ROS melodic 版本。
按照官網(wǎng)步驟安裝即可。
在國內(nèi)網(wǎng)絡(luò)環(huán)境下安裝時,最后的兩步經(jīng)常會報錯:

sudo rosdep init

rosdep update

之前的文章中我們搜集了幾種解決方案,僅供參考。

Eigen3

推薦用 3.2 版本。
官網(wǎng) 下載源文件壓縮包并解壓

wget  https://gitlab.com/libeigen/eigen/-/archive/3.2.10/eigen-3.2.10.tar.gz

tar -xvzf eigen-3.2.10.tar.gz 

然后通過 cmake 方式安裝到指定文件夾中

  • 在源文件夾中建立 build 文件夾

  • 在 build 文件夾中 cmake ..

  • sudo make install

    默認安裝到 /usr/local/include/eigen3 路徑下。
    我們可以修改這一路徑,其基本結(jié)構(gòu)為 <CMAKE_INSTALL_PREFIX>/<INCLUDE_INSTALL_DIR>
    默認:CMAKE_INSTALL_PREFIX/usr/local,INCLUDE_INSTALL_DIRinclude/eigen3
    cmake 時可以修改參數(shù),例如
    cmake .. -DCMAKE_INSTALL_PREFIX=/usr
    就可以將安裝路徑設(shè)置為 /usr/include/eigen3

Pangolin

在 ORB SLAM 2 中那些很炫酷的實時建圖畫面是通過 Pangolin 實現(xiàn)的。
Pangolin 是一個輕量級的開發(fā)庫,控制 OpenGL 的顯示、交互等。
Pangolin 的核心依賴是 OpenGL 和 GLEW。
嚴格按照官網(wǎng)流程安裝即可(https://github.com/stevenlovegrove/Pangolin)。
為了方便查閱,這里摘錄了與 Ubuntu 系統(tǒng)對應(yīng)的安裝步驟:

# OpenGL
sudo apt install libgl1-mesa-dev

# Glew
sudo apt install libglew-dev

# CMake
sudo apt install cmake

# python 
sudo apt install libpython2.7-dev
sudo python -mpip install numpy pyopengl Pillow pybind11

# Wayland
sudo apt install pkg-config
sudo apt install libegl1-mesa-dev libwayland-dev libxkbcommon-dev wayland-protocols

# FFMPEG (For video decoding and image rescaling)
sudo apt install ffmpeg libavcodec-dev libavutil-dev libavformat-dev libswscale-dev libavdevice-dev

# DC1394 (For firewire input)
sudo apt install libdc1394-22-dev libraw1394-dev

# libjpeg, libpng, libtiff, libopenexr (For reading still-image sequences)
sudo apt install libjpeg-dev libpng-dev libtiff5-dev libopenexr-dev

# build Pangolin
git clone https://github.com/stevenlovegrove/Pangolin.git

cd Pangolin

mkdir build

cd build

cmake ..

cmake --build .    # 注意最后那個點

按照上述步驟安裝之后可以運行例子測試一下:

cd ~/Pangolin/build/examples/HelloPangolin
./HelloPangolin 

如果顯示一個彩色立方體,并且可以通過鼠標左、右鍵和滾輪按住拖拽,就表示 Pangolin 安裝成功了。
如果報錯,尤其是與 EGL 相關(guān)的錯誤,很可能是顯卡驅(qū)動的問題,可以嘗試更新顯卡驅(qū)動。

編譯 ORB-SLAM2 文件

從 github 下載源文件

git clone https://github.com/raulmur/ORB_SLAM2.git ORB_SLAM2

slam2 的作者將后續(xù)的編譯過程整理成了 .sh 可執(zhí)行文件,直接運行即可。

cd ORB_SLAM2

chmod +x build.sh

./build.sh

可以打開 build.sh 文件看一下,里面的內(nèi)容實際上就是依次編譯了 DBoW2、g2o,解壓縮 vocabulary,最后編譯 ORB_SLAM2. 如果在編譯過程中顯示 <usleep> 相關(guān)的錯誤,則需要在 ~/ORB_SLAM2/include/System.h 頭文件中添加 #include <unistd.h>.

如果要用 ROS 模式跑 demo,還要額外編譯 ROS 文件。
首先將 ROS 所在目錄加入 ROS_PACKAGE_PATH 環(huán)境變量中,具體操作是將下述命令添加到 .bashrc 文件末尾(別忘了替換下述命令中的 <PATH>):

export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:<PATH>/ORB_SLAM2/Examples/ROS

source 更新變量之后開始編譯:

chmod +x build_ros.sh

./build_ros.sh

在編譯過程中可能會出現(xiàn)一個錯誤:undefined reference to symbol '_ZN5boost6system15system_categoryEv'.
這是因為在編譯時沒有添加 boost_system 共享庫文件。
修改 Examples/ROS/ORB_SLAM2/CMakeLists.txt 文件中的如下內(nèi)容:

set(LIBS 
${OpenCV_LIBS} 
${EIGEN3_LIBS}
${Pangolin_LIBRARIES}
${PROJECT_SOURCE_DIR}/../../../Thirdparty/DBoW2/lib/libDBoW2.so
${PROJECT_SOURCE_DIR}/../../../Thirdparty/g2o/lib/libg2o.so
${PROJECT_SOURCE_DIR}/../../../lib/libORB_SLAM2.so
-lboost_system    
)

即只需要在最后加上 -lboost_system.

到此為止,整個系統(tǒng)的編譯部分就完成了,下邊在普通模式和 ROS 模式下分別運行 demo.

普通模式

單目

  1. TUM 數(shù)據(jù)集(https://vision.in.tum.de/data/datasets/rgbd-dataset/download

這里采用了第一個數(shù)據(jù)集 fr1/xyz,壓縮包為 0.47 GB,時長 30.09 秒,移動距離 7.112 米.

在后續(xù)測試中,所有數(shù)據(jù)集都放在與 Examples 同一目錄下的 datasets 文件夾中。

運行單目 SLAM 的命令格式如下:

./Examples/Monocular/mono_tum Vocabulary/ORBvoc.txt Examples/Monocular/TUMX.yaml PATH_TO_SEQUENCE_FOLDER

具體命令:

./Examples/Monocular/mono_tum Vocabulary/ORBvoc.txt Examples/Monocular/TUM1.yaml datasets/rgbd_dataset_freiburg1_xyz
mono_tum.png
  1. KITTI 數(shù)據(jù)集(http://www.cvlibs.net/datasets/kitti/eval_odometry.php

這里采用了 Download odometry data set (grayscale, 22 GB) 這個數(shù)據(jù)集,包含灰度圖,子文件夾序號從 00 到 21. 我們只測試了 00 號文件夾。

命令格式如下:

./Examples/Monocular/mono_kitti Vocabulary/ORBvoc.txt Examples/Monocular/KITTIX.yaml PATH_TO_DATASET_FOLDER/dataset/sequences/SEQUENCE_NUMBER

具體命令:

./Examples/Monocular/mono_kitti Vocabulary/ORBvoc.txt Examples/Monocular/KITTI00-02.yaml datasets/KITTI/sequences/00
monon_kitti.png
  1. EuRoC 數(shù)據(jù)集(https://projects.asl.ethz.ch/datasets/doku.php?id=kmavvisualinertialdatasets
    我們采用的是 Machine Hall 01,共 1.6GB.

命令格式:

./Examples/Monocular/mono_euroc Vocabulary/ORBvoc.txt Examples/Monocular/EuRoC.yaml PATH_TO_SEQUENCE/cam0/data Examples/Monocular/EuRoC_TimeStamps/SEQUENCE.txt 

具體命令:

./Examples/Monocular/mono_euroc Vocabulary/ORBvoc.txt Examples/Monocular/EuRoC.yaml datasets/MH_01_easy/mav0/cam0/data Examples/Monocular/EuRoC_TimeStamps/MH01.txt 
mono_euroc.png

雙目

  1. KITTI 數(shù)據(jù)集
    依然采用前述 KITTI 數(shù)據(jù)集,sequence 00.

命令格式:

./Examples/Stereo/stereo_kitti Vocabulary/ORBvoc.txt Examples/Stereo/KITTIX.yaml PATH_TO_DATASET_FOLDER/dataset/sequences/SEQUENCE_NUMBER

具體命令:

./Examples/Stereo/stereo_kitti Vocabulary/ORBvoc.txt Examples/Stereo/KITTI00-02.yaml datasets/KITTI/sequences/00
stereo_kitti.png
  1. EuRoC 數(shù)據(jù)集
    采用與單目時相同的數(shù)據(jù)集。

命令格式:

./Examples/Stereo/stereo_euroc Vocabulary/ORBvoc.txt Examples/Stereo/EuRoC.yaml PATH_TO_SEQUENCE/cam0/data PATH_TO_SEQUENCE/cam1/data Examples/Stereo/EuRoC_TimeStamps/SEQUENCE.txt

具體命令:

./Examples/Stereo/stereo_euroc Vocabulary/ORBvoc.txt Examples/Stereo/EuRoC.yaml datasets/MH_01_easy/mav0/cam0/data datasets/MH_01_easy/mav0/cam1/data Examples/Monocular/EuRoC_TimeStamps/MH01.txt
stereo_euroc.png

RGB-D 示例

依然采用之前的 TUM 數(shù)據(jù)集,這次加入深度信息。
這里需要對 rgb 圖和 depth 圖做一下匹配,官方提供了腳本程序 associate.py

格式如下:

python associate.py PATH_TO_SEQUENCE/rgb.txt PATH_TO_SEQUENCE/depth.txt > associations.txt

匹配之后得到 associations.txt 文件,然后運行 SLAM 程序,格式為:

./Examples/RGB-D/rgbd_tum Vocabulary/ORBvoc.txt Examples/RGB-D/TUMX.yaml PATH_TO_SEQUENCE_FOLDER ASSOCIATIONS_FILE

具體命令為:

./Examples/RGB-D/rgbd_tum Vocabulary/ORBvoc.txt Examples/RGB-D/TUM1.yaml datasets/rgbd_dataset_freiburg1_xyz  datasets/rgbd_dataset_freiburg1_xyz/associations.txt
rgbd_tum.png

ROS 模式

在 ROS 模式下,需要從 rosbag 里面發(fā)布數(shù)據(jù),因此要從上述數(shù)據(jù)網(wǎng)站上下載相應(yīng)的 rosbag 數(shù)據(jù)包。

在運行時,一個關(guān)鍵的設(shè)置是將 slam node 接收的 ros topic 和 rosbag 發(fā)布的 ros topic 匹配起來,也就是收、發(fā)雙方的 ros topic 名字必須相同.

topic 名字的轉(zhuǎn)化既可以在運行 slam node 時設(shè)置,格式為

rosrun  <package_name>  <node_name>  original_topic:=new_topic

也可以在 play rosbag 的時候設(shè)置,格式為

rosbag play <bag_name> original_topic:=new_topic

單目

這里采用 TUM 數(shù)據(jù)集對應(yīng)的 rosbag
包含的 rostopic 如下:

/camera/depth/camera_info     798 msgs    : sensor_msgs/CameraInfo        
/camera/depth/image           798 msgs    : sensor_msgs/Image             
/camera/rgb/camera_info       798 msgs    : sensor_msgs/CameraInfo        
/camera/rgb/image_color       798 msgs    : sensor_msgs/Image             
/cortex_marker_array         3034 msgs    : visualization_msgs/MarkerArray
/imu                        15158 msgs    : sensor_msgs/Imu               
/tf                          4242 msgs    : tf/tfMessage

其中要用到的 topic 是 /camera/rgb/image_color,而 rosnode ORB_SLAM2/Mono 接收的 topic 名字為 /camera/image_raw。我們可以在 play rosbag 時將 rosbag 中的/camera/rgb/image_color 轉(zhuǎn)換為 /camera/image_raw

完整命令如下:

roscore

rosrun ORB_SLAM2 Mono Vocabulary/ORBvoc.txt Examples/Monocular/TUM1.yaml 

rosbag play datasets/rgbd_dataset_freiburg1_xyz.bag /camera/rgb/image_color:=/camera/image_raw
ros_mono_tum.png

單目 AR

實現(xiàn)增強現(xiàn)實效果,可以向攝像頭中的世界插入一個虛擬立方體,通過攝像頭觀察,這個虛擬立方體與實際物體相仿。

運行命令與上邊幾乎完全相同,只是將 rosrun ORB_SLAM2 Mono 替換為 rosrun ORB_SLAM2 MonoAR.

ros_monoar_tum.png

雙目

這里我們用 EuRoC 數(shù)據(jù)集對應(yīng)的 rosbag,包含如下的 topic:

/cam0/image_raw    3682 msgs    : sensor_msgs/Image         
/cam1/image_raw    3682 msgs    : sensor_msgs/Image         
/imu0             36820 msgs    : sensor_msgs/Imu           
/leica/position    3099 msgs    : geometry_msgs/PointStamped

其中關(guān)鍵的 topic 是左右兩個攝像頭的數(shù)據(jù) /cam0/image_raw/cam1/image_raw。
而雙目 rosnode Stereo 接收的 topic 分別為 /camera/left/image_raw/camera/right/image_raw,因此在運行時也需要轉(zhuǎn)換一下 topic 名稱。

完整命令如下:

roscore

rosrun ORB_SLAM2 Stereo Vocabulary/ORBvoc.txt Examples/Stereo/EuRoC.yaml true

rosbag play --pause MH_01_easy.bag /cam0/image_raw:=/camera/left/image_raw /cam1/image_raw:=/camera/right/image_raw
ros_stereo_euroc.png

RGB-D

這里我們采用 TUM 的 rosbag,也就是在單目示例中用到的那個.
此時用到兩個 topic 數(shù)據(jù):

/camera/depth/image           798 msgs    : sensor_msgs/Image               
/camera/rgb/image_color       798 msgs    : sensor_msgs/Image           

rosnode RGBD 接收的 topic 分別是 /camera/depth_registered/image_raw/camera/rgb/image_raw,因此也需要轉(zhuǎn)換 topic.

完整命令如下:

roscore 

rosrun ORB_SLAM2 RGBD Vocabulary/ORBvoc.txt Examples/RGB-D/TUM1.yaml

rosbag play --pause rgbd_dataset_freiburg1_xyz.bag /camera/rgb/image_color:=/camera/rgb/image_raw /camera/depth/image:=/camera/depth_registered/image_raw
ros_rgbd_tum.png

這里的結(jié)果有點出乎意料,相機鏡頭一直固定點附近晃動,沒有形成之前我們常見的 SLAM 地圖。
經(jīng)過查找,發(fā)現(xiàn)是 Examples/RGB-D/TUM1.yaml 這個文件的參數(shù)設(shè)置有問題,其中

# Deptmap values factor 
DepthMapFactor: 5000.0

這個參數(shù)似乎是深度值的校正系數(shù)。在具體使用時,表達式為 Z = depth_image[v,u] / factor;

TUM 官網(wǎng)中明確指出普通運行模式和 ROS 運行模式中,這個數(shù)值是不同的:

factor = 5000 # for the 16-bit PNG files
OR: factor = 1 # for the 32-bit float images in the ROS bag files

因此之前普通模式運行時用 Examples/RGB-D/TUM1.yaml 沒有問題,但是在 ROS 模式下就需要修改一下參數(shù)

# Deptmap values factor 
DepthMapFactor: 1.0

修改之后,再次運行上邊的命令,就可以得到預(yù)期的效果。


ros_rgbd_tum_modified.png

總結(jié)

本文詳細記錄了 ORB SLAM 2 中 demo 的復(fù)現(xiàn)過程,以及其中可能存在的一些問題和解決方案。
在接下來的工作中,我們將參考高翔博士的工作 復(fù)現(xiàn) ORB SLAM 2 + 點云建圖,

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容