备注:2023.7.4修改,如果是ros空间,可以在工作空间中使用单独cv_bridge的方式!!!比较简单,是我目前常用的方式。放在文章最后。
由于ROS Melodic自带的是Opencv3.2.0,而我自己下载的是opencv3.4.5,所以需要将cv_bridge的指向改为我自己安装的opencv。
全篇很长,建议看完后操作,不要跟着做。
一开始我参考了这个博主的文章
Ubuntu18.04 ros-melodic opencv版本冲突问题解决_一抹烟霞的博客-CSDN博客_rosopencv版本
但是在编译ORB-SLAM3(beta0.4)时报错,在/usr/lib/x86_64-linux-gnu/中找不到libopencv_core.so.3.4.5、libopencv_imgproc.so.3.4.5和libopencv_imgcodecs.so.3.4.5
于是我在上面博主修改的基础上做了如下修改:(没有加入/usr/include)
sudo gedit /opt/ros/melodic/share/cv_bridge/cmake/cv_bridgeConfig.cmake
###作出如下修改
if(NOT "include;/usr/include;/usr/include/opencv " STREQUAL " ")
set(cv_bridge_INCLUDE_DIRS "")
#set(_include_dirs "include;/usr/include;/usr/include/opencv")
set(_include_dirs "include;/usr/local/lib;/usr/local/include/opencv;/usr/local/include/opencv2;/usr/local/include;")
以及
set(libraries "cv_bridge;/usr/local/lib/libopencv_core.so.3.4.5;/usr/local/lib/libopencv_imgproc.so.3.4.5;/usr/local/lib/libopencv_imgcodecs.so.3.4.5")
然后再修改文件
sudo gedit /opt/ros/melodic/share/cv_bridge/cmake/cv_bridge-extras.cmake
###作出如下修改
set(OpenCV_VERSION 3.4.5)
set(OpenCV_VERSION_MAJOR 3)
set(OpenCV_VERSION_MINOR 4)
set(OpenCV_VERSION_PATCH 5)
以及
cd /opt/ros/melodic/lib/
sudo gedit pkgconfig/cv_bridge.pc
##做如下修改
Libs: -L${prefix}/lib -lcv_bridge /usr/local/lib/libopencv_core.so.3.4.5 /usr/local/lib/libopencv_imgproc.so.3.4.5 /usr/local/lib/libopencv_imgcodecs.so.3.4.5
这个时候编译ORB-SLAM3可以通过了,但是运行rosrun ORB-SLAM3时候会出现段错误,然后退出。我将所有的cmakelist中的opencv都改成opencv 3.4.5后重新编译依旧不行。
接着我把他放一边尝试使用Vins-fusion,编译到最后的时候报警告
/usr/bin/ld: warning: libopencv_imgcodecs.so.3.2, needed by /opt/ros/melodic/lib/libcv_bridge.so, may conflict with libopencv_imgcodecs.so.3.4
/usr/bin/ld: warning: libopencv_core.so.3.2, needed by /opt/ros/melodic/lib/libcv_bridge.so, may conflict with libopencv_core.so.3.4
同时rosrun vins_node 运行闪退,根据警告是动态连接库libcv_brdge.so问题
然后我在vins_fusion的catkin_ws文件夹中运行查看连接库
ldd devel/lib/libvins_lib.so | grep opencv
输出:
libopencv_core.so.3.4 => /usr/local/lib/libopencv_core.so.3.4 (0x00007f7b2d122000)
libopencv_imgproc.so.3.4 => /usr/local/lib/libopencv_imgproc.so.3.4 (0x00007f7b2a304000)
libopencv_imgcodecs.so.3.4 => /usr/local/lib/libopencv_imgcodecs.so.3.4 (0x00007f7b29a6e000)
libopencv_video.so.3.4 => /usr/local/lib/libopencv_video.so.3.4 (0x00007f7b27ce3000)
libopencv_calib3d.so.3.4 => /usr/local/lib/libopencv_calib3d.so.3.4 (0x00007f7b27955000)
libopencv_imgcodecs.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so.3.2 (0x00007f7b262d8000)
libopencv_imgproc.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.3.2 (0x00007f7b25a7f000)
libopencv_core.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_core.so.3.2 (0x00007f7b25644000)
libopencv_features2d.so.3.4 => /usr/local/lib/libopencv_features2d.so.3.4 (0x00007f7b21d25000)
libopencv_flann.so.3.4 => /usr/local/lib/libopencv_flann.so.3.4 (0x00007f7b21acd000)
可以看到其中有三个连接库出问题了。
cd /opt/ros/melodic/lib/
ldd libcv_bridge.so | grep opencv
##如下输出
libopencv_imgcodecs.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so.3.2 (0x00007fd8d2bed000)
libopencv_imgproc.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.3.2 (0x00007fd8cfac7000)
libopencv_core.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_core.so.3.2 (0x00007fd8ce7a9000)
原来是这个出问题了,我只好下载了vision_opencv,链接如下:
GitHub - ros-perception/vision_opencv
进入cv_bridge后
mkdir build && cd build
cmake ..
make
cd devel/lib/
sudo rm /opt/ros/melodic/lib/libcv_bridge.so
sudo cp libcv_bridge.so /opt/opt/ros/melodic/lib/
终于一切恢复了正常,经过了一番折腾,不知道后续是否还会有问题。所以我还是用源码的cv_bridge
cd cv_bridge/build/
sudo make install
然后将vins和orb中需要opencv的CMakerlist中在find_package之前加入
set(cv_bridge_DIR /usr/local/share/cv_bridge/cmake)
然后重新编译工程,完美通过。使用也没问题。然后删除原来的cv_bridge
sudo apt remove ros-melodic-cv-bridge
然后使用ORB和vins没有问题。
后记:
后来我尝试不做上述修改(删除ros重新装了,好累。。。),然后删除了工程中的build文件夹重新尝试了一次,编译ORB-SLAM3的ros包时候报错
[rosbuild] Building package ORB_SLAM3
Failed to invoke /opt/ros/melodic/bin/rospack deps-manifests ORB_SLAM3
[rospack] Error: package 'ORB_SLAM3' depends on non-existent package 'cv_bridge' and rosdep claims that it is not a system dependency. Check the ROS_PACKAGE_PATH or try calling 'rosdep update'
CMake Error at /opt/ros/melodic/share/ros/core/rosbuild/public.cmake:129 (message):
Failed to invoke rospack to get compile flags for package 'ORB_SLAM3'.
Look above for errors from rospack itself. Aborting. Please fix the
broken dependency!
Call Stack (most recent call first):
/opt/ros/melodic/share/ros/core/rosbuild/public.cmake:207 (rosbuild_invoke_rospack)
CMakeLists.txt:4 (rosbuild_init)
大概就是找不到ros的cv_bridge,也找不到我安装的cv_bridge,我也尝试rosdep update,但是没什么用,所以我将在/usr/local/share/中的cv_bridge文件夹复制到/opt/ros/melodic/share/中,然后再次./build_ros.sh就可以通过了。
分析:我觉得一开始我使用的是原本的cv_bridge来进行编译的,经过修改后可以使用,然后我将cv_bridge用apt删除后,系统自动在/usr/local/中找到了我安装的cv_bridge,所以也可以正常使用。但是后来尝试从新开始,ros中不做修改,编译ORB-SLAM3会有警告,说明用的还是ros安装时候自带的cv_bridge,删除自带的以后编译又找不到我自己安装的,就会报找不到cv_bridge,这个时候我把我安装的cv_bridge复制到ros中就可以编译通过了。
总结方法:
1、修改ros的cv_bridge
2、不删除原来的cv_bridge,安装源码的cvbridge,然后在cmakelist中修改,编译完成后apt删除原来的bridge。
3、删除原来的cv_bridge,安装源码bridge,然后cmakelist中修改,并复制/usr/local/share中的bridge到ros路径中(上面有写)。
因为opencv和cv_bridge指向的问题,如果是一个ros工程,有一个ros空间,那么我们可以下载cv_bidge到工作空间的src文件夹中。其中opencv3用melodic,opencv4用noetic。一个工程一个cv_bridge(井水不犯河水)
GitHub - ros-perception/vision_opencv
1、为了防止冲突将cv_bridge下 CMakeLists.txt 文件中 project 名称修改为 my_cv_bridge:
project(my_cv_bridge)
修改:
find_package(OpenCV 4.2.0 QUIET)
并修改 package.xml 中包的名称:
<name>my_cv_bridge</name>
2、编译cv_bridge
到工作空间的根目录,执行 catkin_make
可能会报错:
Could not fild the following Boost libraries:
boost_python37
解决:
修改cv_bridge的CMAKELIST中
...
if(PYTHONLIBS_VERSION_STRING VERSION_LESS "3.8")
# Debian Buster
find_package(Boost REQUIRED python36)
...
还有错:
/usr/include/python2.7/numpy/__ multiarray_api.h:1537:144:error:return-statement with ∩o
value,in function returning 'void*' [ -fpermissive ]
解决:
修改 cv_bridge/src/module.hpp 文件下
void* do_numpy_import()函数改为:
static void do_numpy_import( )
{
import_array( );
}
编译通过了。
3、使用文章来源:https://www.toymoban.com/news/detail-605273.html
在你的工程cmakelist中
find_package(my_cv_bridge)
同时修改 package.xml 文件:
...
<build_depend>my_cv_bridge</build_depend>
...
<exec_depend>my_cv_bridge</exec_depend>
...
再次编译即可了。文章来源地址https://www.toymoban.com/news/detail-605273.html
到了这里,关于Ubuntu18.04 ROS Melodic的cv_bridge指向问题(四种方式,包括opencv4)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!