解解压 .bag文件

# coding:utf-8

import rosbag

import roslib; # roslib.load_manifest(PKG)

import rospy

import cv2

from cv_bridge import CvBridge

from sensor_msgs.msg import Image

bag_file = "./person_1_2017-08-04-17-27-05.bag"

save_file = '/home/Data/x1000_x/office_person/person1'

bag = rosbag.Bag(bag_file, "r")

bridge = CvBridge()

bag_data = bag.read_messages()

for topic, msg, t in bag_data:

cv_image = bridge.compressed_imgmsg_to_cv2(msg, "bgr8")

cv2.imshow("Image window", cv_image)

# imshow可有可无只是为了检验是否在提取图像,并展示

timestr = "%.6f" % msg.header.stamp.to_sec()

# %.6f表示小数点后带有6位,可根据精确度需要修改;

image_name = timestr + ".jpg" # 图像命名:时间戳.jpg

cv2.imwrite('{}/{}'.format(save_file,image_name), cv_image) # 保存;

# cv2.waitKey(0)

你可能感兴趣的:(ubuntu,python,ros)