Extract Lidar and Camera Data from Rosbag File with MATLAB

ROS (Robot Operating System) provides libraries and tools to help software developers create robot applications. It provides hardware abstraction, device drivers, libraries, visualizers, message-passing, package management, and more. ROS is licensed under an open source, BSD license.


  1. Lidar Toolbox
  2. ROS Toolbox


  1. Example data: The data is collected from ssl_slam, which relates to the article <Lightweight 3-D Localization and Mapping for Solid-State LiDAR (Intel Realsense L515 as an example)>.

First, we load the rosbag file using the function rosbag. The file was load as a BagSelection in MATLAB. The BagSelection object is an index of the messages within a rosbag. You can use it to extract message data from a rosbag, select messages based on specific criteria, or create a timeseries of the message properties.

MATLAB rosbag Structure
The BagSelection object has the following properties related to the rosbag:
• FilePath: a character vector of the absolute path to the rosbag file.
• StartTime: a scalar indicating the time the first message was recorded
• EndTime: a scalar indicating the time the last message was recorded
• NumMessages: a scalar indicating how many messages are contained in the file
• AvailableTopics: a list of what topic and message types were recorded in the bag. This is stored as table data that lists the number of messages, message type, and message definition for each topic. For more information on table data types, see Access Data in Tables. Here is an example output of this table.
• MessageList: a list of every message in the bag with rows sorted by time stamp of when the message was recorded. This list can be indexed and you can select a portion of the list this way. Calling select allows you to select subsets based on time stamp, topic or message type.

[file,path] = uigetfile('*.bag');
if isequal(file,0)
   disp('User selected Cancel');
   disp(['User selected ', fullfile(path,file)]);
fileName = fullfile(path,file);
bag = rosbag(fileName);

Second, we call the select function to select “/camera/color/camera_info”, “/camera/color/image_raw”, “/camera/depth/camera_info”, and “/camera/depth/color/points”. The image_raw and points subsets are the data we process later.


bagColorCamera = select(bag,"Topic","/camera/color/camera_info"); 
bagImage = select(bag,"Topic","/camera/color/image_raw"); 
bagDepthCamera = select(bag,"Topic","/camera/depth/camera_info");  
bagPoints = select(bag,"Topic","/camera/depth/color/points");

Third, we use the readMessages function to read all the messages in “/camera/color/image_raw”. The readImage function converts the data into an appropriate MATLAB image and returns it in img.

imageMsgs = readMessages(bagImage);
imageData = readImage(imageMsgs{1});  % the first frame

The pointCloud function creates point cloud data from a set of points in 3-D coordinate system.

pcMsgs = readMessages(bagPoints);
pcData = pointCloud(readXYZ(pcMsgs{1}));  % the first frame
view([15.00 50.00])