首页 > 编程语言 >python解析rosbag

python解析rosbag

时间:2022-12-26 19:56:13浏览次数:46  
标签:right python image topic import path 解析 rosbag left

 

 

# coding:utf-8
#!/usr/bin/python

# Extract images from a bag file.

import sys
import os

import roslib   #roslib.load_manifest(PKG)
import rosbag
import rospy
import decimal
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from cv_bridge import CvBridgeError


left_path = str(sys.argv[2])+'/left/'   # 左目图像的路径,需提前手动创建,也可以使用程序自动创建
right_path = str(sys.argv[2])+'/right/'
depth_path = str(sys.argv[2])+'/depth/'

class ImageCreator():
    def __init__(self):

        print(sys.argv[1])

        # 创建文件夹
        if not os.path.exists(left_path):
            os.makedirs(left_path)
            print('创建文件夹',left_path)
        if not os.path.exists(right_path):
            os.makedirs(right_path)
            print('创建文件夹',right_path)
        if not os.path.exists(depth_path):
            os.makedirs(depth_path)
            print('创建文件夹',depth_path)


        self.bridge = CvBridge()
        print('提取中...')
        with rosbag.Bag(sys.argv[1], 'r') as bag:  # 读取bag文件,注意设置正确的bag文件路径
            for topic,msg,t in bag.read_messages():
                if topic == "/mynteye/left/image_color/compressed": # 左目图像的topic  /left /mynteye/left/image_color
                # if topic == "/left":
                        try:
                            # cv_image = self.bridge.imgmsg_to_cv2(msg,"bgr8")   # 不压缩的
                            cv_image = self.bridge.compressed_imgmsg_to_cv2(msg,"bgr8") # /compressed 压缩的
                        except CvBridgeError as e:
                            print(e)
                        # %.6f表示小数点后带有6位,可根据精确度需要修改
                        timestr = "%.6f" % msg.header.stamp.to_sec()
                        image_name = timestr + ".jpg" #图像命名:时间戳.png
                        cv2.imwrite(left_path + image_name, cv_image)  # 保存图像
                elif topic == "/mynteye/right/image_color/compressed": # 右目图像的topic  /right
                # elif topic == "/right":
                        try:
                            cv_image = self.bridge.compressed_imgmsg_to_cv2(msg,"bgr8")
                        except CvBridgeError as e:
                            print(e)
                        # %.6f表示小数点后带有6位,可根据精确度需要修改
                        timestr = "%.6f" % msg.header.stamp.to_sec()
                        image_name = timestr + ".jpg" #图像命名:时间戳.png
                        cv2.imwrite(right_path + image_name, cv_image)  # 保存图像
                elif topic == "/mynteye/depth/image_raw/compressed": # 深度图像的topic  /depth
                # elif topic == "/depth":
                        try:
                            cv_image = self.bridge.compressed_imgmsg_to_cv2(msg,"bgr8")
                        except CvBridgeError as e:
                            print(e)
                        # %.6f表示小数点后带有6位,可根据精确度需要修改
                        timestr = "%.6f" % msg.header.stamp.to_sec()
                        image_name = timestr + ".png" #图像命名:时间戳.png
                        cv2.imwrite(depth_path + image_name, cv_image)  # 保存图像
        print('提取结束')
  
if __name__ == '__main__': 
    try:
        image_creator = ImageCreator()
    except rospy.ROSInterruptException:
        pass

  

标签:right,python,image,topic,import,path,解析,rosbag,left
From: https://www.cnblogs.com/gooutlook/p/17006723.html

相关文章

  • Python常用库的使用
    文章目录​​一.Python计算生态​​​​二.如何导入第三方库​​​​方式一:pip命令行直接安装​​​​方法二:手动下载第三方库,再使用pip安装(适用部分库直接pip安装失败)​......
  • Python拷贝文件,拷贝指定目录下的所有pdf文件到另一个目录
    importosimportshutildata=os.walk(r'D:\a\b')DPATH=r'D:\pdf_file'forpath,file_dir,file_listindata:#print(path,file_list)forfile_nameinfile_li......
  • Python3入门之一
    Python3入门之一Python标识符大小写敏感。 基本数据类型:int类型:Python所能表示的整数大小只受限于机器内存,而非固定数量的字节数。str类型Pyt......
  • python将字符串作为变量去赋值
    exec可将字符串作为变量去赋值,以下两个方式,value不能生效。原因不清楚,有大佬了解还请告知self.temp和value初始值都为-999以下代码执行结果为:1.0-999 ......
  • ArchLinux 使用不同版本的 python
    前提使用pyenv来管理不同版本的python,可以在不改变系统的情况下更换使用其他python版本安装pyenvsudopacman-Spyenv为zsh配置pyenv环境echo'exportPYE......
  • Python之PyMysql库
    PyMysql库导入PyMysql库:importpymysqlPyMysql连接对象连接数据库,获得一个PyMysql连接对象connconn=pymysql.connect(host=None,port=0,user=None,......
  • SQL解析调研
    概述在做大数据产品时,经常遇到的一个需求,页面上有一个输入框,可以输入SQL语句,然后获得执行结果。如adhoc。注:本文局限于Java语言。调研Druid阿里的Druid,开源作者推广时,称其为......
  • .NET中国开发者峰会11.9 下午分会场1 内容解析
    China.NETConf2019中国.NET开发者峰会即将在上海召开,这次大会是一届完全由社区组织举办的中国.NET开发者盛会,我们筹备大会之初就定下了大会的主题是“开源、共享、创......
  • python 自动点击微信 文件传输助手链接
    fromwxautoimportWeChatimportpywinauto,time,sys,re,random#获取当前微信客户端wx=WeChat()#获取会话列表wx.GetSessionList()#输出当前聊天窗口聊天......
  • Python基础 | 01 走进Python的世界
    导读“人生苦短,我用Python”,Python语言越发火热,科技飞速发展的现在,何不使用Python快速解决问题。本篇文章,让我们一起来了解什么是Python,Python有哪些优缺点?Python作为解释......