ros2 hunmle bag 数据包转为图片数据 python版

import os

import cv2

import rosbag2_py

from cv_bridge import CvBridge

from rclpy.serialization import deserialize_message

from rosidl_runtime_py.utilities import get_message

def save_image(image_data, image_count, output_dir):

"""将图像数据保存为文件"""

filename = os.path.join(output_dir, f"image_{image_count:05d}.jpg")

cv2.imwrite(filename, image_data)

print(f"保存图片:{filename}")

def parse_ros2_bag(bag_path, topic_name, output_dir):

"""解析ROS 2 bag文件中的图像数据"""

if not os.path.exists(output_dir):

os.makedirs(output_dir)

创建rosbag2读取器

reader = rosbag2_py.SequentialReader()

storage_options = rosbag2_py.StorageOptions(uri=bag_path, storage_id='sqlite3')

converter_options = rosbag2_py.ConverterOptions('', '')

reader.open(storage_options, converter_options)

获取话题列表

topic_types = reader.get_all_topics_and_types()

type_map = {topic.name: topic.type for topic in topic_types}

print(f"Bag文件中的话题:{list(type_map.keys())}")

检查话题是否存在

if topic_name not in type_map:

print(f"错误:话题 {topic_name} 不存在于 bag 文件中")

return

获取消息类型

msg_type = type_map[topic_name]

msg_class = get_message(msg_type)

初始化CvBridge

bridge = CvBridge()

image_count = 0

while reader.has_next():

(topic, data, timestamp) = reader.read_next()

if topic == topic_name:

try:

反序列化字节流为Image消息

image_msg = deserialize_message(data, msg_class)

使用cv_bridge转换消息为OpenCV格式图像

image = bridge.imgmsg_to_cv2(image_msg, "bgr8")

save_image(image, image_count, output_dir)

image_count += 1

except Exception as e:

print(f"跳过无法解析的消息:{e}")

print(f"图片提取完成,共提取 {image_count} 张图片。")

if name == "main":

bag_path = "/home/yao/workspace/data/yao_bag/" # ROS 2 bag 文件路径

topic_name = "/pic_hsp004" # 图像话题名称

output_dir = "output_images" # 保存图片的文件夹

parse_ros2_bag(bag_path, topic_name, output_dir)

相关推荐
工业互联网专业12 小时前
基于协同过滤算法的小说推荐系统_django+spider
python·django·毕业设计·源码·课程设计·spider·协同过滤算法
星星的月亮叫太阳12 小时前
large-scale-DRL-exploration 代码阅读 总结
python·算法
玉树临风江流儿12 小时前
C++左值、右值、move移动函数
开发语言·c++
Q_Q196328847512 小时前
python+django/flask基于Echarts+Python的图书零售监测系统设计与实现(带大屏)
spring boot·python·django·flask·node.js·php
拾荒的小海螺12 小时前
JAVA:Spring Boot3 新特性解析的技术指南
java·开发语言·spring boot
深度学习lover13 小时前
<数据集>yolo航拍交通目标识别数据集<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·航拍交通目标识别
程序猿202313 小时前
Python每日一练---第二天:合并两个有序数组
开发语言·python
椰羊sqrt13 小时前
CVE-2025-4334 深度分析:WordPress wp-registration 插件权限提升漏洞
android·开发语言·okhttp·网络安全
权泽谦13 小时前
用 Flask + OpenAI API 打造一个智能聊天机器人(附完整源码与部署教程)
python·机器人·flask
Js_cold13 小时前
Verilog任务task
开发语言·fpga开发·verilog