简介:slambook2中所需的OpenCV3与安装的OpenCV4不兼容,以及库的目录不对或者找不到,运行ch7报错。修改了CMakeLists.txt文件和ch7中的一些cpp程序,文章最后给出了最终的CMakeLists.txt。
环境:ubuntu20.04,OpenCV4.5.5
其中slam的环境已经按照博客配置好了。
参考博客:【SLAM实战篇】Ubuntu 20.04版本(OpenCV版本4.5.3)对于ORB-SLAM2安装运行,代码编译,自己的数据集构造_orbslam2 github-优快云博客
首先在ch7目录下输入
mkdir build && cd build/
cmake ..
报错
修改ch7的CMakeLists.txt,把find_package(OpenCV 3 REQUIRED)改为
find_package(OpenCV 4.5.5 QUIET)
重新cmake ..
编译
make -j4
报错
[ 8%] Building CXX object CMakeFiles/orb_cv.dir/orb_cv.cpp.o
[ 16%] Building CXX object CMakeFiles/orb_self.dir/orb_self.cpp.o
[ 25%] Building CXX object CMakeFiles/pose_estimation_2d2d.dir/pose_estimation_2d2d.cpp.o
[ 33%] Building CXX object CMakeFiles/triangulation.dir/triangulation.cpp.o
/home/rosnoetic/catkin_ws/src/slambook2/ch7/orb_cv.cpp: In function ‘int main(int, char**)’:
/home/rosnoetic/catkin_ws/src/slambook2/ch7/orb_cv.cpp:16:31: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
16 | Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
| ^~~~~~~~~~~~~~~~~~~
/home/rosnoetic/catkin_ws/src/slambook2/ch7/pose_estimation_2d2d.cpp: In function ‘int main(int, char**)’:
/home/rosnoetic/catkin_ws/src/slambook2/ch7/pose_estimation_2d2d.cpp:36:31: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
36 | Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
| ^~~~~~~~~~~~~~~~~~~
/home/rosnoetic/catkin_ws/src/slambook2/ch7/pose_estimation_2d2d.cpp: In function ‘void pose_estimation_2d2d(std::vector<cv::KeyPoint>, std::vector<cv::KeyPoint>, std::vector<cv::DMatch>, cv::Mat&, cv::Mat&)’:
/home/rosnoetic/catkin_ws/src/slambook2/ch7/pose_estimation_2d2d.cpp:143:61: error: ‘CV_FM_8POINT’ was not declared in this scope
143 | fundamental_matrix = findFundamentalMat(points1, points2, CV_FM_8POINT);
| ^~~~~~~~~~~~
make[2]: *** [CMakeFiles/orb_cv.dir/build.make:79:CMakeFiles/orb_cv.dir/orb_cv.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:102:CMakeFiles/orb_cv.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务....
make[2]: *** [CMakeFiles/pose_estimation_2d2d.dir/build.make:79:CMakeFiles/pose_estimation_2d2d.dir/pose_estimation_2d2d.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:164:CMakeFiles/pose_estimation_2d2d.dir/all] 错误 2
/home/rosnoetic/catkin_ws/src/slambook2/ch7/triangulation.cpp: In function ‘int main(int, char**)’:
/home/rosnoetic/catkin_ws/src/slambook2/ch7/triangulation.cpp:44:31: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
44 | Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
| ^~~~~~~~~~~~~~~~~~~
/home/rosnoetic/catkin_ws/src/slambook2/ch7/orb_self.cpp: In function ‘void BfMatch(const std::vector<std::vector<unsigned int> >&, const std::vector<std::vector<unsigned int> >&, std::vector<cv::DMatch>&)’:
/home/rosnoetic/catkin_ws/src/slambook2/ch7/orb_self.cpp:402:18: warning: narrowing conversion of ‘i1’ from ‘size_t’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
402 | cv::DMatch m{i1, 0, 256};
| ^~
make[2]: *** [CMakeFiles/triangulation.dir/build.make:79:CMakeFiles/triangulation.dir/triangulation.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:195:CMakeFiles/triangulation.dir/all] 错误 2
[ 41%] Linking CXX executable orb_self
[ 41%] Built target orb_self
make: *** [Makefile:91:all] 错误 2
即
根据报错提示,分别修改orb_cv的16、17行,pose_estimation_2d2d的36、37行,pose_estimation的143行,triangulation的44行,因为在OpenCV4里不再用CV_LOAD_IMAGE_COLOR等。
将 CV_LOAD_IMAGE_COLOR 更改为 cv::IMREAD_COLOR
将 CV_FM_8POINT 更改为 cv::FM_8POINT
重新编译 make -j4,报错
[ 8%] Building CXX object CMakeFiles/orb_cv.dir/orb_cv.cpp.o
[ 16%] Building CXX object CMakeFiles/pose_estimation_2d2d.dir/pose_estimation_2d2d.cpp.o
[ 33%] Built target orb_self
[ 41%] Building CXX object CMakeFiles/triangulation.dir/triangulation.cpp.o
[ 50%] Building CXX object CMakeFiles/pose_estimation_3d2d.dir/pose_estimation_3d2d.cpp.o
/home/rosnoetic/catkin_ws/src/slambook2/ch7/pose_estimation_3d2d.cpp:6:10: fatal error: Eigen/Core: 没有那个文件或目录
6 | #include <Eigen/Core>
| ^~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/pose_estimation_3d2d.dir/build.make:79:CMakeFiles/pose_estimation_3d2d.dir/pose_estimation_3d2d.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:226:CMakeFiles/pose_estimation_3d2d.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务....
[ 58%] Linking CXX executable orb_cv
[ 58%] Built target orb_cv
[ 66%] Linking CXX executable pose_estimation_2d2d
[ 66%] Built target pose_estimation_2d2d
[ 75%] Linking CXX executable triangulation
[ 75%] Built target triangulation
make: *** [Makefile:91:all] 错误 2
查看CMakeLists.txt文件,eigen3的目录不对,修改
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/local/include/eigen3" # /usr/include/eigen3/原来的
)
重新编译make -j4,报错
-- Configuring done (0.1s)
-- Generating done (0.0s)
-- Build files have been written to: /home/rosnoetic/catkin_ws/src/slambook2/ch7/build
[ 8%] Building CXX object CMakeFiles/orb_cv.dir/orb_cv.cpp.o
[ 25%] Building CXX object CMakeFiles/pose_estimation_2d2d.dir/pose_estimation_2d2d.cpp.o
[ 25%] Building CXX object CMakeFiles/orb_self.dir/orb_self.cpp.o
[ 33%] Building CXX object CMakeFiles/triangulation.dir/triangulation.cpp.o
[ 41%] Linking CXX executable orb_cv
[ 41%] Built target orb_cv
[ 50%] Building CXX object CMakeFiles/pose_estimation_3d2d.dir/pose_estimation_3d2d.cpp.o
/home/rosnoetic/catkin_ws/src/slambook2/ch7/pose_estimation_3d2d.cpp:7:10: fatal error: g2o/core/base_vertex.h: 没有那个文件或目录
7 | #include <g2o/core/base_vertex.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/pose_estimation_3d2d.dir/build.make:79:CMakeFiles/pose_estimation_3d2d.dir/pose_estimation_3d2d.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:226:CMakeFiles/pose_estimation_3d2d.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务....
/home/rosnoetic/catkin_ws/src/slambook2/ch7/orb_self.cpp: In function ‘void BfMatch(const std::vector<std::vector<unsigned int> >&, const std::vector<std::vector<unsigned int> >&, std::vector<cv::DMatch>&)’:
/home/rosnoetic/catkin_ws/src/slambook2/ch7/orb_self.cpp:402:18: warning: narrowing conversion of ‘i1’ from ‘size_t’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
402 | cv::DMatch m{i1, 0, 256};
| ^~
[ 58%] Linking CXX executable pose_estimation_2d2d
[ 58%] Built target pose_estimation_2d2d
[ 66%] Linking CXX executable orb_self
[ 66%] Built target orb_self
[ 75%] Linking CXX executable triangulation
[ 75%] Built target triangulation
make: *** [Makefile:91:all] 错误 2
在CMakeLists.txt中明确添加g2o的路径
"/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o"
重新编译make -j4,报错
-- Configuring done (0.1s)
-- Generating done (0.0s)
-- Build files have been written to: /home/rosnoetic/catkin_ws/src/slambook2/ch7/build
[ 16%] Built target orb_cv
[ 33%] Built target pose_estimation_2d2d
[ 50%] Built target orb_self
[ 66%] Built target triangulation
[ 75%] Building CXX object CMakeFiles/pose_estimation_3d3d.dir/pose_estimation_3d3d.cpp.o
[ 83%] Building CXX object CMakeFiles/pose_estimation_3d2d.dir/pose_estimation_3d2d.cpp.o
In file included from /home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/g2o/core/optimizable_graph.h:34,
from /home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/g2o/core/base_vertex.h:30,
from /home/rosnoetic/catkin_ws/src/slambook2/ch7/pose_estimation_3d2d.cpp:7:
/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/g2o/core/openmp_mutex.h:30:10: fatal error: g2o/config.h: 没有那个文件或目录
30 | #include "g2o/config.h"
| ^~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/pose_estimation_3d2d.dir/build.make:79:CMakeFiles/pose_estimation_3d2d.dir/pose_estimation_3d2d.cpp.o] 错误 1
没有config.h,查找g2o文件夹,显示在build文件下
复制config.h文件到g2o目录下
重新编译make -j4,报错,截取部分吧
根据提示,在pose_estimation_3d2d和pose_estimation_3d3d中
将 CV_LOAD_IMAGE_COLOR 更改为 cv::IMREAD_COLOR
将 CV_LOAD_IMAGE_UNCHANGED 更改为 cv::IMREAD_UNCHANGED
重新编译make -j4, 报错
[ 91%] Linking CXX executable pose_estimation_3d3d
/usr/bin/ld: 找不到 -lg2o_core
/usr/bin/ld: 找不到 -lg2o_stuff
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/pose_estimation_3d3d.dir/build.make:114:pose_estimation_3d3d] 错误 1
make[1]: *** [CMakeFiles/Makefile2:257:CMakeFiles/pose_estimation_3d3d.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务....
[100%] Linking CXX executable pose_estimation_3d2d
/usr/bin/ld: 找不到 -lg2o_core
/usr/bin/ld: 找不到 -lg2o_stuff
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/pose_estimation_3d2d.dir/build.make:114:pose_estimation_3d2d] 错误 1
make[1]: *** [CMakeFiles/Makefile2:226:CMakeFiles/pose_estimation_3d2d.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
重新修改CMakeLists.txt的pose_estimation_3d2d和pose_extimation_3d3d链接部分
add_executable(pose_estimation_3d2d pose_estimation_3d2d.cpp) # 新改的
target_link_libraries(pose_estimation_3d2d
/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/lib/libg2o_core.so
/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/lib/libg2o_stuff.so
${OpenCV_LIBS})
add_executable(pose_estimation_3d3d pose_estimation_3d3d.cpp)
target_link_libraries(pose_estimation_3d3d
/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/lib/libg2o_core.so
/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/lib/libg2o_stuff.so
${OpenCV_LIBS})
编译成功
在ch7目录下终端输入
build/orb_cv 1.png 2.png
得到结果
最终的CMakeLists.txt文件为:
cmake_minimum_required(VERSION 3.10) # 新改的
project(vo1)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++14 -O3 ${SSE_FLAGS} -msse4") # 新改的
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(OpenCV 4.5.5 QUIET) # 新改的
find_package(G2O REQUIRED)
find_package(Sophus REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/local/include/eigen3" # 原来的是/usr/include/eigen3/
"/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/" # 新加的g2o路径
)
add_executable(orb_cv orb_cv.cpp)
target_link_libraries(orb_cv ${OpenCV_LIBS})
add_executable(orb_self orb_self.cpp)
target_link_libraries(orb_self ${OpenCV_LIBS})
# add_executable( pose_estimation_2d2d pose_estimation_2d2d.cpp extra.cpp ) # use this if in OpenCV2
add_executable(pose_estimation_2d2d pose_estimation_2d2d.cpp)
target_link_libraries(pose_estimation_2d2d ${OpenCV_LIBS})
# # add_executable( triangulation triangulation.cpp extra.cpp) # use this if in opencv2
add_executable(triangulation triangulation.cpp)
target_link_libraries(triangulation ${OpenCV_LIBS})
# 以下都是删除原来两条,新改的,根据自己的g2o_core和g2o_stuff目录来
add_executable(pose_estimation_3d2d pose_estimation_3d2d.cpp)
target_link_libraries(pose_estimation_3d2d
/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/lib/libg2o_core.so
/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/lib/libg2o_stuff.so
${OpenCV_LIBS})
add_executable(pose_estimation_3d3d pose_estimation_3d3d.cpp)
target_link_libraries(pose_estimation_3d3d
/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/lib/libg2o_core.so
/home/rosnoetic/catkin_ws/src/slambook2/3rdparty/g2o/lib/libg2o_stuff.so
${OpenCV_LIBS})