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源码,用OpenCV 3.4.10进行编译,之后我们在ROS包中指定使用这个新的cv_bridge就好了。
将cv_bridge源码下载到ROS工作空间
cv_bridge下载地址:https://gitee.com/bingobinlw/cv_bridge或者 github自己寻找https://github.com/ros-perception/vision_opencv
修改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.10适配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})
可以正常使用了。
出现类似undefined_reference_to的问题都是没有链接库,所以注意target_link_libraries()