How do I get readImage (ROS) to work?

12 visualizaciones (últimos 30 días)
Ulrik Söderström
Ulrik Söderström el 10 de Abr. de 2019
Editada: Cam Salzberger el 2 de En. de 2020
I have a file recorded with a RealSense-camera and it is saved as a rosbag file .bag.
I want to access the individual frames as images through Matlab and I have found information how to do from different forums. One example is from Matlab answers: https://se.mathworks.com/matlabcentral/answers/407981-access-images-from-within-a-rosbag
The problem is that that the function readImage doesn't work.
Depending on how I use it I get different error messages.
Either:
Array indices must be positive integers or logical values.
The error is generated with this code:
bag = rosbag('20190402_093734.bag');
bagInfo = rosbag('info', '20190402_093734.bag')
bSel = select(bag, 'Topic', '/device_0/sensor_1/Color_0/image/data');
msgs = readMessages(bSel,"DataFormat","struct");
img = readImage(msgs{1});
Or:
Undefined function or variable 'readImage'.
The error is generated with this code:
bag = rosbag('20190402_093734.bag');
bagselect1 = select(bag, 'Topic', '/device_0/sensor_0/Depth_0/image/data')
bagselect2 = select(bag, 'Time', [1 5], 'Topic', '/device_0/sensor_0/Depth_0/image/data')
bagselect2.AvailableTopics{1,end}
bagselect3 = select(bagselect2, 'Time', [1 5])
msgs = readMessages(bagselect3,"DataFormat","struct");
img = readImage(msgs{1});
I thought there was a problem with my installation of ROS so I tried using the online version of MATLAB but I got the same error message there.
I am assuming that there is a very simple, logical solution to this but I have no idea of what it might be. Could it be a problem with the .ros-file?

Respuesta aceptada

Cam Salzberger
Cam Salzberger el 11 de Abr. de 2019
Hello Ulrik,
It's good that you are experimenting with the 'DataFormat','struct' option for extracting messages from rosbags, since it is a big performance improvement. Unfortunately, the convenience functions (readImage, readPointCloud, etc.) don't yet support accepting structs as input, and are expecting ROS message objects. If you want to use readImage, I would suggest leaving off the 'DataFormat','struct' option, and passing the output to readImage. Otherwise, you can manually extract the image from the ROS message structure.
-Cam
  5 comentarios
Mohamed Atia
Mohamed Atia el 27 de Dic. de 2019
Could you please post the code that you used to "create an empty message and copy all the content (except Header) " so you can use readImage ? Thanks
Cam Salzberger
Cam Salzberger el 2 de En. de 2020
Editada: Cam Salzberger el 2 de En. de 2020
Something like this should work, though I haven't tested it:
msgStructs = readMessages(bagSel, 'DataFormat', 'struct');
msg = copyImage(msgStructs{1});
I = readImage(msg);
function msg = copyImage(msgStruct)
msg = rosmessage(msgStruct.MessageType);
fNames = fieldnames(msg);
for k = 1:numel(fNames)
if ~any(strcmp(fNames{k}, {'MessageType', 'Header'}))
msg.(fNames{k}) = msgStruct.(fNames{k});
end
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Specialized Messages en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by