从 Rosbag 文件读取激光雷达和相机数据
R2026b此示例说明如何从 rosbag 文件中读取并保存图像和点云数据。此示例还说明如何为激光雷达相机标定准备数据。
使用此示例末尾定义的 helperDownloadRosbag 辅助函数下载 rosbag 文件。
path = helperDownloadRosbag;
从该 bag 文件中检索信息。
bag = rosbag(path);
使用相应的主题名称,从 rosbag 中选择图像和点云消息,并从该文件中选择消息子集。您还可以使用时间戳来筛选数据。有关详细信息,请参阅 select (ROS Toolbox) 函数。
imageBag = select(bag,'Topic','/camera/image/compressed'); pcBag = select(bag,'Topic','/points');
读取所有消息。
imageMsgs = readMessages(imageBag); pcMsgs = readMessages(pcBag);
为激光雷达相机标定准备数据,两个传感器之间的数据必须时间同步。为所选主题创建 timeseries (ROS Toolbox) 对象并提取时间戳。
ts1 = timeseries(imageBag); ts2 = timeseries(pcBag); t1 = ts1.Time; t2 = ts2.Time;
为确保准确标定,图像和点云必须在相同时间戳捕获。根据时间戳匹配来自两个传感器的对应数据。考虑到不确定性,请使用 0.1 秒的裕度。
k = 1; if size(t2,1) > size(t1,1) for i = 1:size(t1,1) [val,indx] = min(abs(t1(i) - t2)); if val <= 0.1 idx(k,:) = [i indx]; k = k + 1; end end else for i = 1:size(t2,1) [val,indx] = min(abs(t2(i) - t1)); if val <= 0.1 idx(k,:) = [indx i]; k = k + 1; end end end
创建目录以保存有效图像和点云。
pcFilesPath = fullfile(tempdir,'PointClouds'); imageFilesPath = fullfile(tempdir,'Images'); if ~exist(imageFilesPath,'dir') mkdir(imageFilesPath); end if ~exist(pcFilesPath,'dir') mkdir(pcFilesPath); end
提取图像和点云。在各自文件夹中命名并保存文件。将对应的图像和点云以相同编号保存。
for i = 1:length(idx) I = readImage(imageMsgs{idx(i,1)}); pc = pointCloud(readXYZ(pcMsgs{idx(i,2)})); n_strPadded = sprintf('%04d',i) ; pcFileName = strcat(pcFilesPath,'/',n_strPadded,'.pcd'); imageFileName = strcat(imageFilesPath,'/',n_strPadded,'.png'); imwrite(I,imageFileName); pcwrite(pc,pcFileName); end
启动Lidar Camera Calibrator,并使用其界面将数据加载到该 App 中。您也可以从 MATLAB® 命令行加载数据并启动该 App。
checkerSize = 81; %millimeters
padding = [0 0 0 0];
lidarCameraCalibrator(imageFilesPath,pcFilesPath,checkerSize,padding)
支持函数
function rosbagFile = helperDownloadRosbag() % Download the data set from the given URL. rosbagZipFile = matlab.internal.examples.downloadSupportFile( ... 'lidar','data/lccSample.zip'); [outputFolder,~,~] = fileparts(rosbagZipFile); rosbagFile = fullfile(outputFolder,'lccSample.bag'); if ~exist(rosbagFile,'file') unzip(rosbagZipFile,outputFolder); end end
另请参阅
App
函数
rosbag(ROS Toolbox) |timeseries(ROS Toolbox)