import roslib
import rosbag
import rospy
import cv2
import os
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from cv_bridge import CvBridgeError
depth_t_path='/home/lhd/code/Realsensor2Tum/depth.txt'
rgb_t_path='/home/lhd/code/Realsensor2Tum/rgb.txt'
rgb_path = './rgb/'# absolute path of extracted rgb images
depth_path = './depth/'# absolute path of extracted depth images
bridge = CvBridge()
with rosbag.Bag('20240122_Depth1024_RGB1280.bag', 'r') as bag: # path for the bag
for topic,msg,t in bag.read_messages():
if topic == "/device_0/sensor_0/Depth_0/image/data": #topic different from devices
cv_image = bridge.imgmsg_to_cv2(msg, "mono16") #Z16对应 mono16
timestr = "%.8f" % msg.header.stamp.to_sec()
image_name = timestr + '.png'
with open(depth_t_path,'a') as file:
file.write(timestr+" depth/"+timestr+".png"+"\n")
cv2.imwrite(depth_path + image_name, cv_image)
# print(depth_path + image_name)
if topic == "/device_0/sensor_1/Color_0/image/data": #topic different from devices
cv_image = bridge.imgmsg_to_cv2(msg, "bgr8") # 尤其注意这个编码
timestr = "%.8f" % msg.header.stamp.to_sec()
image_name = timestr + '.png'
with open(rgb_t_path,'a') as file:
file.write(timestr+" rgb/"+timestr+".png"+"\n")
# cv2.imwrite(rgb_path + image_name, cv_image)
读取realsense bag包中得rgbd图片
于 2024-01-23 13:58:26 首次发布