调了一天多的bug终于解决了…,发出来供大家参考
1.针对vins-mono和vins-fusion运行opencv4.6和cv_bridge调用4.2版本冲突的问题记录调试过程如下:
感谢这位大佬的代码,内容全都可以正确应用:ubuntu20.04配置vins-fusion+opencv4.6
这篇文章也很棒,可参考 :ubuntu20.04跑lvi-sam报错!Opencv库与cv_bridge包冲突问题
一、安装OpenCV 4.6.0
1.下载 opencv 源码,选择所需要的版本 opencv 4.6.0,相应的扩展opencv_contrib 4.6.0,以及用于桥接 ROS 和 opencv 的 cv_bridge
Release OpenCV 4.6.0 · opencv/opencv · GitHub
Release 4.6.0 · opencv/opencv_contrib · GitHub
https://github.com/ros-perception/vision_opencv/tree/
1.1将 opencv 安装在 /usr/local/opencv-4.6.0下,移动opencv及其扩展模块并进入工作目录
sudo mv ./opencv-4.6.0//usr/local/
sudo mv ./opencv_contirb-4.6.0//usr/local/
cd /usr/local/opencv-4.6.0/
1.2编译前现确定 opencv 的安装路径,我的安装路径是CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.6.0
1.3 编译 opencv 4.6.0,其中我把cuda相关的都选的off,暂时不使用gpu
sudo mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.6.0 \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.6.0/modules \
-D WITH_CUDA=OFF \
-D ENABLE_FAST_MATH=OFF \
-D CUDA_FAST_MATH=OFF \
-D WITH_CUBLAS=ON \
-D WITH_LIBV4L=ON \
-D WITH_GSTREAMER=ON \
-D WITH_GSTREAMER_0_10=OFF \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D CUDA_NVCC_FLAGS="--expt-relaxed-constexpr" \
-D WITH_TBB=ON \
..
1.4 电脑性能好的话可以多核编译 make -j8,如果出现兼容性错误的花建议用 make,速度慢一点
make -j8
sudo make install
二、配置cv_bridge功能包
1 因为 opencv 的数据和 ROS 的数据需要转化,因此需要通过 cv_bridge 这个桥接工具,接下来我们介绍怎么自己下载安装并配置一个自己的 cv_bridge 功能包
2. 首先创建一个ros工作空间
mkdir -r ~/third/cv_bridge_ws/src/
cd ~/cv_bridge_ws/src/
3. 然后,下载对应版本的源码,因为是 Ubuntu 20.04 因此下载 noetic 版本
GitHub - ros-perception/vision_opencv at noetic
4. 下载完成后解压到 ~/third/cv_bridge_ws/src/ 目录下,因为我们只需要 cv_bridge,所以只需保留 cv_bridge 文件夹,将其放在 ~/third/cv_bridge_ws/src/然后修改一下 cv_bridge的配置文件。
4.1 修改CMakeLists.txt 文件:在 set(_opencv_version 4) 前面添加你的 opencv 安装路径,大概在第 20 行
set(Opencv_DIR /usr/local/opencv-4.6.0/build)set(_opencv_version 4)find_package(OpenCV 4 QUIET)
4.2另外由于是自己制作的 cv_bridge 对应于 opencv 4.6.0 版本的功能包,因此最好将包名该一下,我这里是改成 my_cv_bridge
4.3整个cmakelists文件内容如下;
cmake_minimum_required(VERSION 3.0.2)project(my_cv_bridge)find_package(catkin REQUIRED COMPONENTS rosconsole sensor_msgs)if(NOT ANDROID)find_package(PythonLibs)if(PYTHONLIBS_VERSION_STRING VERSION_LESS "3.8")#Debian Busterfind_package(Boost REQUIRED python37)else()#Ubuntu Focalfind_package(Boost REQUIRED python)endif()else()find_package(Boost REQUIRED)endif()set(Opencv_DIR /usr/local/opencv-4.6.0/build)set(_opencv_version 4)find_package(OpenCV 4 QUIET)if(NOT OpenCV_FOUND)message(STATUS "Did not find OpenCV 4, trying OpenCV 3")set(_opencv_version 3)endif()find_package(OpenCV ${_opencv_version} REQUIRED
COMPONENTS
opencv_core
opencv_imgproc
opencv_imgcodecs
CONFIG
)catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS rosconsole sensor_msgs
DEPENDS OpenCV
CFG_EXTRAS cv_bridge-extras.cmake
)catkin_python_setup()include_directories(include ${Boost_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})link_directories(${Boost_LIBRARY_DIRS})if(NOT ANDROID)add_subdirectory(python)endif()add_subdirectory(src)if(CATKIN_ENABLE_TESTING)add_subdirectory(test)endif()#installthe include folderinstall(
DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION})
4.5修改package.xml,改一下功能包名称
<package format="2">
# <name>cv_bridge</name><name>my_cv_bridge</name>
4.6编译cv_bridge功能包
cd ~/cv_bridge_ws/
catkin_make
4.7 如果编译成功,则 my_cv_bridge 功能包制作就完成了,这也说明 opencv 4.6.0 已经正确安装了,并且这个my_cv_bridge 功能包可以正确连接上自己安装的 opencv 版本,最后我们将这个功能包的路径添加到 ~/.bashrc 文件夹中,使得我们在后面编译 vins-fusion 时可以调用这个 my_cv_bridge 功能包,打开 ~/.bashrc文件夹,在最后加入如下命令
source ~/cv_bridge_ws/devel/setup.bash
4.8 最后在终端 source 一下当前该路径
source ~/.bas
在这里插入代码片
hrc
三.编译vins-fusion
1. 进入vins-fusion工作空间 cd ~/vins-fusion
2. 修改 vins_estimator/CMakeLists.txt 文件
2.1 将 cv_bridge 改成 my_cv_bridge
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
geometry_msgs
nav_msgs
tf
cv_bridge_460
camera_models
image_transport)
2.2 添加 cv_bridge_460 功能包的路径和 opencv-4.6.0 的路径
set(my_cv_bridge_DIR "/home/lemontech/thirdlib/cv_bridge_ws/devel/share/my_cv_bridge/cmake")set(OpenCV_DIR "/usr/local/opencv-4.6.0/bulid/")
2.3 保证cv_bridge的库被正确安装和链接
include_directories(${catkin_INCLUDE_DIRS} ${CERES_INCLUDE_DIRS} ${my_cv_bridge_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
3 修改 loop_fusion
3.1 CMakeLists.txt 文件
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
nav_msgs
camera_models
my_cv_bridge
roslib
)
3.2 添加 cv_bridge_460 功能包的路径和 opencv-4.6.0 的路径
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}"/home/lemontech/thirdlib/cv_bridge_ws/devel")set(my_cv_bridge_DIR "/home/lemontech/thirdlib/cv_bridge_ws/devel/share/my_cv_bridge/cmake")
#-DEIGEN_USE_MKL_ALL")set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g")set(OpenCV_DIR "/usr/local/opencv-4.6.0/bulid/")
3.3 保证cv_bridge的库被正确安装和链接
include_directories(${catkin_INCLUDE_DIRS} ${CERES_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIR} ${my_cv_bridge_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
3.4 完整的cmakelist文件
cmake_minimum_required(VERSION 2.8.3)project(loop_fusion)set(CMAKE_BUILD_TYPE "Release")set(CMAKE_CXX_FLAGS "-std=c++17")set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}"/home/lemontech/thirdlib/cv_bridge_ws/devel")set(my_cv_bridge_DIR "/home/lemontech/thirdlib/cv_bridge_ws/devel/share/my_cv_bridge/cmake")
#-DEIGEN_USE_MKL_ALL")set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g")set(OpenCV_DIR "/usr/local/opencv-4.6.0/bulid/")find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
nav_msgs
camera_models
my_cv_bridge
roslib
)find_package(OpenCV)find_package(Ceres REQUIRED)set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)find_package(Eigen3)include_directories(${catkin_INCLUDE_DIRS} ${CERES_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIR} ${my_cv_bridge_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})catkin_package()add_executable(loop_fusion_node
src/pose_graph_node.cpp
src/pose_graph.cpp
src/keyframe.cpp
src/utility/CameraPoseVisualization.cpp
src/ThirdParty/DBoW/BowVector.cpp
src/ThirdParty/DBoW/FBrief.cpp
src/ThirdParty/DBoW/FeatureVector.cpp
src/ThirdParty/DBoW/QueryResults.cpp
src/ThirdParty/DBoW/ScoringObject.cpp
src/ThirdParty/DUtils/Random.cpp
src/ThirdParty/DUtils/Timestamp.cpp
src/ThirdParty/DVision/BRIEF.cpp
src/ThirdParty/VocabularyBinary.cpp
)target_link_libraries(loop_fusion_node ${catkin_LIBRARIES} ${OpenCV_LIBS} ${CERES_LIBRARIES})
四.编译成功
如果编译成功,能够正确识别find_package中的my_cv_bridge功能包,则在vins-fusion的devel文件中可以得到上述三个文件。和安装官方的cv_bridge一样,只不过官方的包在opt/ros/noetic/…中
五、但编译成功,不代表运行成功…
出现这个错误,嗯,是opencv4.2和4.6冲突了,但我明明已经把cv_bridge和opencv都换成了自己的呀!!然后。。。查看了my_cv_bridge/cmake下的cv_bridge-extras.cmake,发现里面写的还是4.2!!!
- 下面是修改后的正确版本 a. (1)把版本改为4.6 b. (2)opencv_config_path设置为/usr/local/opencv-4.6.0/lib/cmake/opencv4
- 然后,我又打开了/usr/local/opencv-4.6.0/lib/cmake/opencv4,发现下图的set(OpenCV_VERSION 4.2.0)写的是4.2,而不是4.6!!!改正,重新编译
rm -rf build
mkdir build && cd build
cmake ..
make -j8
- 之后再重新编译一下上面的cv_bridge_ws,再运行vins-fusion就可以正常运行了
六、关于opencv4.2版本卸载问题
- 为了确保不会误删其他版本的文件,可以使用通配符来删除所有4.2版本的库文件:
sudo rm /usr/local/lib/libopencv_*4.2*
然后可能/var中也有相关文件,采用同样的删除方法
2. 查看和opencv库有关的路径,库链接文件都在这个文件下:
sudo find /-iname "*opencv*"
- 更新动态链接库缓存
sudo ldconfig
- 卸载原cv_bridge sudo apt-get remove ros-melodic-cv-bridge,下载自己的ros对应版本的cv_bridge并链接到指定的opencv【这个可卸可不卸??我卸了】
七、关于cmakelist文件
set(CMAKE_CXX_FLAGS "-std=c++17")指定了c++17标准进行编译
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}"/home/lemontech/thirdlib/cv_bridge_ws/devel") 这个设置将/home/lemontech/thirdlib/cv_bridge_ws/devel路径添加到cmake的前缀路径中,以便CMake可以在路径中找到依赖项和包
set(my_cv_bridge_DIR "/home/lemontech/thirdlib/cv_bridge_ws/devel/share/my_cv_bridge/cmake")设置置顶了my_cv_bridge包的CMake配置文件路径,以便CMake可以找到并使用这个包
find_package(catkin REQUIRED COMPONENTS
my_cv_bridge
roscpp
)用于查找并配置指定的catkin包和组件,catkin是ros的构建系统,REQUIRED表示这些组件是必须的,如果缺少,会报错)
版权归原作者 weixin-1111111 所有, 如有侵权,请联系我们删除。