Extract pictures and point cloud data from the bag

Some lidar point cloud data and binocular camera image data were recorded before, which need to be extracted from the bag for subsequent use.

Extract point cloud from bag

Extract point cloud data from bag

Extract pictures from the bag

View bag information

rosbag info yourname.bag

You can see the bag package information in the following figure:
point cloud data has 663 frames, point cloud topic is /rfans_driver/rfans_points
image data has 1984 frames, topic is /mynteye/left/image_color and /mynteye/right/image_color
Insert picture description here

Extract picture

I have tried some methods to find on the Internet, which is to write a langch file and run the ros node to get it, but the picture does not have a timestamp, and the picture is not fully extracted. So I found the following code form to get pictures, the number of pictures is accurate and time-stamped. python code:

#coding:utf-8

import roslib;  
import rosbag
import rospy
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from cv_bridge import CvBridgeError

path='/home/hyper/Downloads/image/' #存放图片的位置
class ImageCreator():

    def __init__(self):
        self.bridge = CvBridge()
        with rosbag.Bag('2020-10-13-10-31-59.bag', 'r') as bag:   #要读取的bag文件;
            for topic,msg,t in bag.read_messages():
                if topic == "/mynteye/left/image_color":  #图像的topic;
                        try:
                            cv_image = self.bridge.imgmsg_to_cv2(msg,"bgr8")
                        except CvBridgeError as e:
                            print e
                        timestr = "%.6f" %  msg.header.stamp.to_sec()
                        #%.6f表示小数点后带有6位,可根据精确度需要修改;
                        image_name = timestr+ ".jpg" #图像命名:时间戳.jpg
                        cv2.imwrite(path+image_name, cv_image)  #保存;


if __name__ == '__main__':

    try:
        image_creator = ImageCreator()
    except rospy.ROSInterruptException:
        pass

Just modify the corresponding path. Put the code and bag in the same folder, then open the terminal, run the code and wait, and you can see the continuously extracted pictures in the image folder during the operation.
The running code is as follows:

python get_image.py

Guess you like

Origin blog.csdn.net/qq_38337524/article/details/109144187