已安装ROS Noetic与cv_bridge仍报ModuleNotFoundError,如何解决?
解决cv_bridge.boost.cv_bridge_boost模块找不到的问题
问题背景
已安装ROS Noetic发行版,并通过sudo apt-get install ros-noetic-cv-bridge完成cv_bridge包安装,但在PyCharm的Anaconda虚拟环境(hsi-env)中运行提取rosbag图像帧的代码时,出现ModuleNotFoundError: No module named 'cv_bridge.boost.cv_bridge_boost'错误。
运行代码
import os import argparse import pdb import cv2 import rosbag from sensor_msgs.msg import Image from cv_bridge import CvBridge bag_file = './bag_files/20230707_152832.bag' output_dir= './frames/rgb_bag_output' image_topic = '/device_0/sensor_1/Color_0/image/data' # image_topic ='sensor_msgs/Image' bag = rosbag.Bag(bag_file, "r") bridge = CvBridge() #gg =bag.read_messages(topics = '/device_0/sensor_1/Color_0/image/data') # bag.get_message_count() count = 0 for topic, msg, t in bag.read_messages(topics=image_topic): cv_img = bridge.imgmsg_to_cv2(msg, desired_encoding= "rgb8")#"passthrough") cv2.imwrite(os.path.join(output_dir, "frame%06i.png" % count), cv_img) print("Wrote image %i" % count) count += 1 bag.close()
错误信息
Traceback (most recent call last): File "/home/es/PycharmProjects/2-Process-RGBD/extract_bag_frame.py", line 32, in <module> cv_img = bridge.imgmsg_to_cv2(msg, desired_encoding= "rgb8")#"passthrough") File "/home/es/anaconda3/envs/hsi-env/lib/python3.8/site-packages/cv_bridge/core.py", line 163, in imgmsg_to_cv2 dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding) File "/home/es/anaconda3/envs/hsi-env/lib/python3.8/site-packages/cv_bridge/core.py", line 99, in encoding_to_dtype_with_channels return self.cvtype2_to_dtype_with_channels(self.encoding_to_cvtype2(encoding)) File "/home/es/anaconda3/envs/hsi-env/lib/python3.8/site-packages/cv_bridge/core.py", line 91, in encoding_to_cvtype2 from cv_bridge.boost.cv_bridge_boost import getCvType ModuleNotFoundError: No module named 'cv_bridge.boost.cv_bridge_boost'
错误原因
通过sudo apt安装的ros-noetic-cv-bridge是针对ROS自带的系统Python环境(Noetic对应Python3.8)编译的,其boost绑定模块仅存在于系统Python路径中。而你使用的Anaconda虚拟环境有独立的Python路径,无法找到系统cv_bridge的boost依赖;如果虚拟环境中通过pip安装了cv_bridge,该版本通常缺少boost编译的绑定模块,导致报错。
解决方法
方法一:切换到ROS自带的Python解释器
- 打开PyCharm,进入
Settings → Project: [你的项目名] → Python Interpreter - 点击右上角齿轮图标,选择
Add → System Interpreter - 找到ROS Noetic对应的Python路径,一般为
/usr/bin/python3.8 - 选中该解释器并应用,重新运行代码即可
方法二:在conda环境中重新编译cv_bridge
- 创建并进入编译工作空间:
mkdir -p ~/cv_bridge_ws/src && cd ~/cv_bridge_ws/src git clone https://github.com/ros-perception/vision_opencv.git cd vision_opencv && git checkout noetic cd ../.. - 激活你的conda虚拟环境:
conda activate hsi-env - 指定当前conda环境的Python路径编译:
catkin_make -DPYTHON_EXECUTABLE=$(which python) -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")/libpython3.8.so - 在PyCharm中添加编译后的cv_bridge路径:
- 进入
Settings → Python Interpreter → Show All - 选中你的conda环境,点击
Show paths for the selected interpreter - 添加路径
~/cv_bridge_ws/devel/lib/python3.8/dist-packages,保存后重新运行代码
- 进入
方法三:手动解析图像数据(无需cv_bridge)
如果只是临时提取图像帧,可以绕过cv_bridge,直接解析sensor_msgs/Image的原始数据:
import os import argparse import pdb import cv2 import rosbag import numpy as np from sensor_msgs.msg import Image bag_file = './bag_files/20230707_152832.bag' output_dir= './frames/rgb_bag_output' image_topic = '/device_0/sensor_1/Color_0/image/data' bag = rosbag.Bag(bag_file, "r") count = 0 for topic, msg, t in bag.read_messages(topics=image_topic): # 根据图像编码调整dtype和通道数,这里假设是rgb8 dtype = np.uint8 channels = 3 # 从data缓冲区转换为numpy数组 cv_img = np.frombuffer(msg.data, dtype=dtype).reshape(msg.height, msg.width, channels) # 如果是rgb编码,cv2.imwrite默认用bgr,需要转换 cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR) cv2.imwrite(os.path.join(output_dir, "frame%06i.png" % count), cv_img) print("Wrote image %i" % count) count += 1 bag.close()
注意:需根据实际图像编码修改dtype和channels,比如mono8对应单通道uint8,16UC1对应单通道uint16。
内容的提问来源于stack exchange,提问作者S.EB
相关产品推荐
相关产品推荐

