背景:
使用ros自带的image_view可以将rosbag中的rgb图片提取出来并保存为指定格式图片,但是当提取深度图像,即depth_registered话题时,会报错:
Unable to convert "32FC1" image to bgr8
线索:
用matlab从rosbag中提取rgb图片和depth图片
解决:
bag = rosbag(fullfile(bag_path,bag_name));
bSel_depth = select(bag,'Topic',topic_name);
depth_message=select(bSel_depth,'MessageType','sensor_msgs/Image');
data_depth=readMessages(depth_message);
L = length(data_depth);
for i=1:L
single_image = data_depth{i,1};
single_image_matlab =readImage(single_image);
figure(i);
imshow(single_image_matlab,[],'Border','tight'); % 'Border','tight'用来去除边缘
image_name = [num2str(i-1,'%04d'),'.jpg'];
saveas(i,fullfile(output_path,image_name));
pause(0.2);
close(i);
end
关键点:
1. 需要用到readImage才能将ros image格式的single_image转化为matlab能够处理的数据;
2. 使用figure+imshow的形式,是因为imshow有DisplayRange特性,可以选择根据图片中的深度最大最小值来显示图片;而不使用imwrite的原因是,对于单像素类型为single的图像,输出默认动态范围是[0,1]无法修改,只能显示最大到“1”的深度。
3. imshow使用'Border','tight'特性是为了去除figure窗口填充的边框,这样saveas就不会连边缘的空白也保存下来。