当前位置:   article > 正文

【SLAM】从零开始在Ubuntu系统中复现ORB-SLAM3(version1.0)_orb-slam3复现

orb-slam3复现

相关依赖安装

必要依赖

sudo apt-get update
sudo apt-get install git gcc g++
sudo apt-get install vim make cmake
sudo apt-get install libssl-dev
  • 1
  • 2
  • 3
  • 4

Pangolin

请不要安装最新版本,建议选择0.6版本
下载地址
Required Dependencies

  • OpenGL (Desktop / ES / ES2)
sudo apt install libgl1-mesa-dev
  • 1
  • Glew
sudo apt install libglew-dev
  • 1

Recommended Dependencies

  • Python2 / Python3, for drop-down interactive console
sudo apt install libpython2.7-dev
  • 1
  • Wayland
sudo apt install pkg-config
sudo apt install libegl1-mesa-dev libwayland-dev libxkbcommon-dev wayland-protocols
  • 1
  • 2

Optional Dependencies for video input (可选)

  • FFMPEG (For video decoding and image rescaling)
sudo apt install ffmpeg libavcodec-dev libavutil-dev libavformat-dev libswscale-dev libavdevice-dev
  • 1
  • DC1394 (For firewire input)
sudo apt install libdc1394-22-dev libraw1394-dev
  • 1
  • libuvc (For cross-platform webcam video input via libusb)
git://github.com/ktossell/libuvc.git
  • 1
  • libjpeg, libpng, libtiff, libopenexr (For reading still-image sequences)
sudo apt install libjpeg-dev libpng12-dev libtiff5-dev libopenexr-dev
  • 1

** 编译与安装 **

cd Pangolin
mkdir build && cd build
cmake ..
make -j4
sudo make install
  • 1
  • 2
  • 3
  • 4
  • 5

** 验证 **

cd ..
cd examples/HelloPangolin
mkdir build && cd build
cmake ..
make
./HelloPangolin
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

Eigen3.3.4安装

Required by g2o (see below). Download and install instructions can be found at: Eigen官网. Required at least 3.1.0.

#安装
cd eigen-3.3.4
mkdir build
cd build
cmake ..
make
sudo make install
 
#安装后,头文件安装在/usr/local/include/eigen3/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Opencv3.4.5安装

We use OpenCV to manipulate images and features. Dowload and install instructions can be found at: Opencv官网. Required at leat 3.0. Tested with OpenCV 3.2.0 and 4.4.0.
官方的安装流程
在这里插入图片描述
请根据自己的版本选择

安装前置依赖

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
  • 1
  • 2
  • 3

编译

cd ~/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GTK=ON -D OPENCV_GENERATE_PKGCONFIG=YES ..
make 
sudo make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

CMAKE_BUILD_TYPE=RELEASE:表示编译发布版本
CMAKE_INSTALL_PREFIX:表示生成动态库的安装路径,可以自定义
WITH_GTK=ON:这个配置是为了防止GTK配置失败:即安装了libgtk2.0-dev依赖,还是报错未安装
OPENCV_GENERATE_PKGCONFIG=YES:表示自动生成OpenCV的pkgconfig文件,否则需要自己手动生成。

配置环境变量

sudo gedit  /etc/ld.so.conf.d/opencv.conf
  • 1

添加路径

/usr/local/lib
  • 1

使用如下命令生效设置

sudo ldconfig
  • 1

配置系统bash

sudo gedit /etc/bash.bashrc
  • 1

在末尾添加

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH 
  • 1
  • 2

生效

source /etc/bash.bashrc 
  • 1

查看

#opencv版本检测
pkg-config --modversion opencv

# 如果安装的是opencv4
pkg-config --modversion opencv4
  • 1
  • 2
  • 3
  • 4
  • 5

安装Boost

Boost 是一个功能强大、构造精巧、跨平台、开源并且完全免费的 C++ 程序库。
下载地址
选择:boost_1_77_0.tar.gz
解压后运行

sudo ./bootstrap.sh
sudo ./b2 install
  • 1
  • 2

ROS一键式安装

wget http://fishros.com/install -O fishros && . fishros
  • 1

来源:https://fishros.org.cn/forum/

ORB-SLAM3编译

这里本文没有选择官方的ORB-SLAM3,而是选择了Github中有详细解释信息的,同样都是version1.0版本
ORB_SLAM3_detailed_comments

cd ORB_SLAM3
chmod +x build.sh
./build.sh
  • 1
  • 2
  • 3

完成

在ROS中编译

# 添加环境变量
sudo gedit ~/.bashrc
# 文件中写入
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:/home/ubuntu/catkin_ws/src/ORB_SLAM3/Examples/ROS
source ~/.bashrc
echo ${ROS_PACKAGE_PATH}
chmod +x build_ros.sh
./build_ros.sh
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

报错:
(1)[rosbuild] rospack found package “ORB_SLAM3” at “”, but the current directory is …
解决:
都是环境变量中ROS_PACKAGE_PATH 没有更改:gedit ~/.bashrc添加或更改
(2)modulenotfounderror: no module named ‘rospkg’

pip install rospkg
  • 1

(3)[rospack] Error: the rosdep view is empty: call ‘sudo rosdep init’ and ‘rosdep update’

sudo rosdep init
rosdep update
  • 1
  • 2
  • 第一行命令出现错误
ERROR: cannot download default sources list from:
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list
Website may be down.
  • 1
  • 2
  • 3

解决如下:
1、打开terminal控制台,输入:sudo gedit /etc/hosts
这个命令中的nano是文本编辑器,也可以用vim、gedit等打开;
2、在打开的文件末尾一行添加: 151.101.84.133 raw.githubusercontent.com
3、保存退出,在terminal中输入: sudo rosdep init
4、执行完输入:rosdep update即可
在这里插入图片描述

  • 第二行命令如果出现错误
    可参考如下博文:rosdep update error
    亲测可用

(4)Found PythonInterp: /home/wxd/anaconda3/bin/python (found version “3.9.13”)
ROS和Conda的python冲突,这里退出

conda deactivate
  • 1

这里就算修改也没有用,最终发现是因为conda污染导致的,建议直接删除conda软件,后续也可能会造成污染!

# 查看python
which python3
  • 1
  • 2

(5)依旧报错

CMakeLists find_package(opencv3), error occurred like this:

[ 15%] Linking CXX executable ../Stereo_Inertial
/usr/bin/ld: warning: libopencv_imgproc.so.4.2, needed by /opt/ros/noetic/lib/libcv_bridge.so, may conflict with libopencv_imgproc.so.3.4
/usr/bin/ld: warning: libopencv_core.so.4.2, needed by /opt/ros/noetic/lib/libcv_bridge.so, may conflict with libopencv_core.so.3.4
/usr/bin/ld: CMakeFiles/Stereo_Inertial.dir/src/ros_stereo_inertial.cc.o: undefined reference to symbol '_ZNK2cv8FileNodecviEv'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libopencv_core.so.4.2.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

I changed the CMakeLists to find_package(opencv4),
[ 15%] Linking CXX executable ../Stereo_Inertial
/usr/bin/ld: warning: libopencv_highgui.so.3.4, needed by ../../../../lib/libORB_SLAM3.so, may conflict with libopencv_highgui.so.4.2
/usr/bin/ld: warning: libopencv_calib3d.so.3.4, needed by ../../../../lib/libORB_SLAM3.so, may conflict with libopencv_calib3d.so.4.2
/usr/bin/ld: warning: libopencv_imgproc.so.3.4, needed by ../../../../lib/libORB_SLAM3.so, may conflict with libopencv_imgproc.so.4.2
/usr/bin/ld: warning: libopencv_core.so.3.4, needed by ../../../../lib/libORB_SLAM3.so, may conflict with libopencv_core.so.4.2
afterwards every Linking process has warning like this.

Different versions libopencv shared libs are needed and inevitably conflict with another version.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

ROS 使用的版本和 ORBSLAM3 使用的版本不一样……如果不是从源代码构建 ROS,你就无法控制它使用的 OpenCV 版本……所以构建 ORBSLAM3 使用您的 ROS 使用的 OpenCV 版本…在您的情况下,ROS noetic 使用 Opencv 4.2 并且您已经使用 OpenCV 3.4 构建了 ORBSLAM3 。 尝试将 OpenCV 4.2 与 ORBSLAM3 结合使用。
解决方法

just change the CMakeLists to find_package(OpenCV 4) and add #include "opencv2/imgcodecs/legacy/constants_c.h" in all .cc file in Examples. It will work
  • 1

ORB_SLAM3/issues/279

经过测试,以上方法也没有用!!!

重新看错误信息:

libopencv_imgproc.so.3.4, needed by ../../../../lib/libORB_SLAM3.so, may conflict with libopencv_imgproc.so.4.2
  • 1

这个错误信息的意思是:编译orb-slam3的ros的时候,使用的是opencv4.2,但是lib文件夹下的libORB_SLAM3.so是使用opencv3.4编译的,会产生冲突。
如果你此时运行rosrun ORB-SLAM3 Mono ...时候,会无法运行,报错信息为:段错误 (核心已转储),这个问题就是由于opencv版本信息不对应导致的。
为什么Opencv版本存在问题?
这是因为我们安装ros的时候,使用一键式安装,导致ros自动安装了Opencv4,而我们在上面又自己手动安装了Opencv3.4.5,此时系统环境中存在两个版本的Opencv!当我们下载orb-slam3后,先运行了./build,sh,请打开build.sh查看,可以看到里面编译了以下内容:

cd Thirdparty/DBoW2
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j

cd ../../g2o

echo "Configuring and building Thirdparty/g2o ..."

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j

cd ../../Sophus

echo "Configuring and building Thirdparty/Sophus ..."

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

打开/Thirdparty/DBoW2/CMakeLists.txt

find_package(OpenCV 3.2 QUIET)
if(NOT OpenCV_FOUND)
   find_package(OpenCV 3.0 QUIET)
   if(NOT OpenCV_FOUND)
      message(FATAL_ERROR "OpenCV > 3.0 not found.")
   endif()
endif()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

也就是说OpenCV查找3.2版本后没找到选择了3.4.5,但是后面编译./build_ros.sh时候是基于ros环境的编译,而ros环境中使用的是OpenCV4.2.0,这就导致了OpenCV版本的不一致。

** 解决方案 **
根据上述分析,很容易想到解决方案,在相关CMakeLists.txt中明确使用ROS环境中的OpenCV4.2.0
~/ORB_SLAM3_detailed_comments-master/CMakeLists.txt~/ORB_SLAM3_detailed_comments-master/Thirdparty/DBoW2/CMakeLists.txt~/ORB_SLAM3_detailed_comments-master/Examples/ROS/ORB_SLAM3/CMakeLists.txt中所有的find_package(OpenCV 3.2 QUIET)都修改为find_package(OpenCV 4),此时再编译即可运行通过!

在这里插入图片描述

ORB-SLAM3测试

rocore

rosrun ORB_SLAM3 Stereo_Inertial /home/wxd/software/ORB_SLAM3_detailed_comments-master/Vocabulary/ORBvoc.txt /home/wxd/software/ORB_SLAM3_detailed_comments-master/Examples_old/Stereo-Inertial/EuRoC.yaml true

rosbag play --pause MH_03_medium.bag /cam0/image_raw:=/camera/left/image_raw /cam1/image_raw:=/camera/right/image_raw /imu0:=/imu
  • 1
  • 2
  • 3
  • 4
  • 5

注意:相机标定文件选择的是/Examples_old/Stereo-Inertial/EuRoC.yaml的,不是新的标定文件(估计Examples/ROS是从Examples_old/ROS复制过去的),不然会报标定信息错误。

更新

这orb-slam3最新版本bug真多,作者维护真慢
1、从github中拉取下来之后,先修改目录中的CMakeList.txt,因为本人电脑opencv版本4.2,CMakeLists中要求4.4

find_package(OpenCV 4)
  • 1

2、将examples_old中的ROS包复制到eamples中,因为build_ros.sh明明写的是cd Examples/ROS/ORB_SLAM3
3、修改ROS文件夹中的CMakeList.txt,一个是opencv版本;另一个是第三方库问题

fatal error: sophus/se3.hpp: 没有那个文件或目录
  • 1

添加

include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/../../../
${PROJECT_SOURCE_DIR}/../../../include
${PROJECT_SOURCE_DIR}/../../../include/CameraModels
${PROJECT_SOURCE_DIR}/../../../Thirdparty/Sophus
${Pangolin_INCLUDE_DIRS}
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

另外,AR包是有问题的,删除关于AR相关的内容,反正也不用,下面是删除修改后全部CMakeLists内容

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

rosbuild_init()

IF(NOT ROS_BUILD_TYPE)
  SET(ROS_BUILD_TYPE Release)
ENDIF()

MESSAGE("Build type: " ${ROS_BUILD_TYPE})

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  -Wall  -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall  -O3 -march=native")

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

LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../../../cmake_modules)

find_package(OpenCV 4)
if(NOT OpenCV_FOUND)
   find_package(OpenCV 2.4.3 QUIET)
   if(NOT OpenCV_FOUND)
      message(FATAL_ERROR "OpenCV > 2.4.3 not found.")
   endif()
endif()

find_package(Eigen3 3.1.0 REQUIRED)
find_package(Pangolin REQUIRED)

include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/../../../
${PROJECT_SOURCE_DIR}/../../../include
${PROJECT_SOURCE_DIR}/../../../include/CameraModels
${PROJECT_SOURCE_DIR}/../../../Thirdparty/Sophus
${Pangolin_INCLUDE_DIRS}
)

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_SLAM3.so
-lboost_system
)

# Node for monocular camera
rosbuild_add_executable(Mono
src/ros_mono.cc
)

target_link_libraries(Mono
${LIBS}
)

# Node for stereo camera
rosbuild_add_executable(Stereo
src/ros_stereo.cc
)

target_link_libraries(Stereo
${LIBS}
)

# Node for RGB-D camera
rosbuild_add_executable(RGBD
src/ros_rgbd.cc
)

target_link_libraries(RGBD
${LIBS}
)

# Node for monocular-inertial camera
rosbuild_add_executable(Mono_Inertial
src/ros_mono_inertial.cc
)

target_link_libraries(Mono_Inertial
${LIBS}
)

# Node for stereo-inertial camera
rosbuild_add_executable(Stereo_Inertial
src/ros_stereo_inertial.cc
)

target_link_libraries(Stereo_Inertial
${LIBS}
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

参考

[1] https://www.jianshu.com/p/59608e83becb
[2] https://aitechtogether.com/article/39266.html
[3] https://blog.csdn.net/qq_38364548/article/details/122220493

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号