前言
ROS原装的cv_bridge位于/opt/ros/melodic/include/cv_bridge。它依赖于OpenCV 3.2
在当前ROS包中为了使用基于新的OpenCV 3.4.10的cv_bridge,网上有博客说修改原来cv_bridge的链接库到自己的新的OpenCV,其实我也试过。但可能会导致其他基于cv_bridge的ros工具不可用。此处不建议
步骤一: 下载源码
将cv_bridge源码下载到ROS工作空间
cv_bridge下载地址:https://gitee.com/bingobinlw/cv_bridge 或者 github自己寻找 https://github.com/ros-perception/vision_opencv,我先是试用了github的链接,发现编译无法通过。后来换用了gitee的文件,成功创建了/home/user/catkin_ws/devel/share/cv_bridge/cmake。
步骤二: 编译源码
修改cv_bridge的CMakeList.txt文件,在开头部分加入依赖的新的OpenCV位置,参照如下格式:
cmake_minimum_required(VERSION 2.8)
project(cv_bridge)
find_package(catkin REQUIRED COMPONENTS rosconsole sensor_msgs)
set(OpenCV_DIR "/usr/local/share/OpenCV")#使用opencv3.4.13适配cv_bridge
然后直接catkin_make
此时在/home/user/catkin_ws/devel/share/cv_bridge/cmake已经生成了cv_bridgeConfig.cmake文件。
步骤三: 引入使用
引入自己的ROS包使用
首先确定find_package()
命令可以找到刚才目录/home/user/catkin_ws/devel/share/cv_bridge/cmake
一般打开~/.bashrc
中查看
PKG_CONFIG_PATH=/home/user/catkin_ws/devel/lib/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH
自己可在终端 echo $PKG_CONFIG_PATH
确认路径能搜索的到。
我的CMakeList.txt
文件示例如下:
set(OpenCV_DIR /usr/local/share/OpenCV)
find_package(OpenCV 3 REQUIRED)
message(STATUS "OpenCV_INCLUDE_DIR: ${OpenCV_INCLUDE_DIRS}")
set(cv_bridge_DIR /home/user/catkin_ws/devel/share/cv_bridge/cmake)
find_package(cv_bridge)
message(STATUS "cv_bridge_LIBS: ${cv_bridge_LIBRARIES}")
add_executable(node src/node.cpp)
target_link_libraries(node ${OpenCV_LIBS} ${catkin_LIBRARIES} ${cv_bridge_LIBRARIES})
即可正常使用。
Tips:
出现类似 undefined_reference_to
的问题都是没有链接库,所以注意target_link_libraries()