解决ROS配置中cv_bridge问题

cv_bridge — opencv和ros连接起来的桥

问题描述

在Jetson Nano+ROS环境上catkin_make时出现cv_bridge问题如下

CMake Error at /home/nano/catkin_ws/devel/share/cv_bridge/cmake/cv_bridgeConfig.cmake:113 (message):
  Project 'cv_bridge' specifies
  '/home/nano/catkin_ws/src/vision_opencv/cv_bridge/include' as an include
  dir, which is not found.  It does neither exist as an absolute directory
  nor in
  '/home/nano/catkin_ws/src/vision_opencv/cv_bridge//home/nano/catkin_ws/src/vision_opencv/cv_bridge/include'.
  Check the issue tracker
  'https://github.com/ros-perception/vision_opencv/issues' and consider
  creating a ticket if the problem has not been reported yet.
Call Stack (most recent call first):
  /opt/ros/melodic/share/catkin/cmake/catkinConfig.cmake:76 (find_package)
  tx2_fcnn_node/CMakeLists.txt:57 (find_package)

解决方法

  1. 卸载旧版本cv_bridge
sudo apt-get remove ros-indigo-cv-bridge
  1. 下载最新版cv_bridge
git clone https://github.com/ros-perception/vision_opencv.git
  1. 【可以省略】修改vision_opencv/cv_bridge/CMakeLists.txt,在20行左右的位置修改opencv版本
set(_opncv_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()

此步我并没有修改,也正确的找到opencv
但jetson nano默认安装的opencv环境是opencv4,可以参照 Trouble with catkin_make hector slam - #3 by hwlim3unws - Jetson TX2 - NVIDIA Developer Forumshwlim3unws的方法 将opencv环境修改到3

  1. sudo apt-get purge ’ * libopencv * ’ (’ and * should have not space in between. Don’t know why but this website does not reflect when ’ and * is next to each other)
  2. sudo apt-get remove ros-*
  3. System Settings —> Software & Updates —> ‘Other Software’ tab —> uncheck nvidia related repos
  4. reinstall ROS using ROS website’s tutorial
  5. ~/catkin_ws$ source devel/setup.bash
  6. ~/catkin_ws$ catkin_make
  1. vision_opencv中的cv_bridge/拿出来,放到工作空间(catkin_wa/src)中,重新编译:
cd ~/catkin_ws/src/cv_bridge
mkdir build
cd build
cmake ..
make
sudo make install

这里我才用传统make的方法,网上也有人直接catkin_make --pkg cv_bridge

【报错:make 66%时错误】

[ 66%] Building CXX object src/CMakeFiles/cv_bridge_boost.dir/module.cpp.o
In file included from /usr/include/python2.7/numpy/ndarrayobject.h:27:0,
                 from /home/nano/catkin_ws/src/cv_bridge/src/module.hpp:26,
                 from /home/nano/catkin_ws/src/cv_bridge/src/module.cpp:35:
/home/nano/catkin_ws/src/cv_bridge/src/module.hpp: In function ‘int do_numpy_import()’:
/usr/include/python2.7/numpy/__multiarray_api.h:1537:144: error: return-statement with no value, in function returning ‘int’ [-fpermissive]
 efine import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } }
                                                                                                                                              ^
/usr/include/python2.7/numpy/__multiarray_api.h:1537:144: note: in definition of macro ‘import_array’
 efine import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } }
                                                                                                                                              ^~~~~~
src/CMakeFiles/cv_bridge_boost.dir/build.make:62: recipe for target 'src/CMakeFiles/cv_bridge_boost.dir/module.cpp.o' failed
make[2]: *** [src/CMakeFiles/cv_bridge_boost.dir/module.cpp.o] Error 1
CMakeFiles/Makefile2:930: recipe for target 'src/CMakeFiles/cv_bridge_boost.dir/all' failed
make[1]: *** [src/CMakeFiles/cv_bridge_boost.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2

解决方法是,修改/home/nano/catkin_ws/src/cv_bridge/src/module.hpp(根据自己的路径),将最后一段(36~40)行改为

static void do_numpy_import( )
{
	import_array( );
}

Resources

ROS (Robot Operating System) ,`cv_bridge`是一个非常有用的库,它允许在ROS节点之间处理OpenCV (计算机视觉库) 和ROS的标准消息格式之间的转换。如果你想在CMakeLists.txt文件添加对`cv_bridge`的支持以便构建包含它的项目,可以按照以下步骤操作: 1. **添加依赖**: 首先,你需要将`cv_bridge`作为项目的直接依赖添加到CMakeLists.txt。如果使用ROS Noetic及以后的版本,通常会自动包含这个包,但在某些早期版本需要手动指定。例如: ```cmake find_package(cv_bridge REQUIRED) ``` 2. **链接库**: 确保在target_link_libraries部分链接`cv_bridge`及其所需的OpenCV和其他依赖库,例如: ```cmake target_link_libraries(your_target ${catkin_LIBRARIES} cv_bridge ${OpenCV_LIBS}) ``` `your_target` 应替换为你实际的目标名称。 3. **包含头文件**: 在source_files列表添加使用`cv_bridge`功能的源文件,并确保包含必要的头文件: ```cmake add_executable(your_node src/your_node.cpp) include_directories(include/${PROJECT_NAME}) include_directories(include/opencv2) include_directories(include/cv_bridge) ``` 这里假设src/your_node.cpp是你的源文件,并且`include`目录下有相关的头文件。 4. **运行时设置**: 如果你的程序需要在运行时动态加载`cv_bridge`,可能会用到`find_packageroviz REQUIRED`)这样的指令,因为`cv_bridge`默认会在`roscpp`包查找。 完整的例子可能是这样的: ```cmake # ...其他CMake配置... find_package(catkin REQUIRED COMPONENTS roscpp std_msgs image_transport cv_bridge) add_executable(your_node src/your_node.cpp) catkin_package( INCLUDE_DIRS include LIBRARIES your_node CATKIN_DEPENDS roscpp std_msgs image_transport cv_bridge ) target_link_libraries(your_node ${catkin_LIBRARIES} cv_bridge ${OpenCV_LIBS}) # 如果你需要在运行时动态加载... if(BUILD_SHARED_LIBS) find_package(rostest REQUIRED) add_rostest(tests your_test src/your_test.cpp DEPENDENCIES your_node) endif() ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

doubleZ0108

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值