How can I convert a ROS Image topic into an matrix that I can process in Simulink?

2 ビュー (過去 30 日間)
Antti
Antti 2016 年 8 月 17 日
コメント済み: Antti 2018 年 2 月 23 日
I am trying to find a simple way of converting images received from ROS into matrix form that can then be processed in Simulink. On Matlab side there are the writeImage and readImage functions that can be used to convert ROS Image to matlab image but I am not sure what should be the correct way of doing this on Simulink side.
One option would be to get the raw data from Image topic and reorganize it into matrix form, but that processing is always dependent on the image size and format. Is there any simple way of doing this?

採用された回答

Swarooph
Swarooph 2016 年 8 月 23 日
You are correct that readImage and writeImage functions are for MATLAB use only since they work on ROS topic objects. However in Simulink we work with buses. The straight forward way that I could think of to deal with this is to unpack the image on your own in a MATLAB function block. So you could have a model like this:
And inside the MATLAB function block, you could write the following function:
function img = fcn(msg)
%#codegen
%Get msg details
imgData = msg.Data;
imgH = msg.Height;
imgW = msg.Width;
%Create blank image output with required size
img = zeros(msg.Width,msg.Height,3);
%Reshape image data
imgR = reshape(imgData(1:3:end),imgW,imgH)';
imgG = reshape(imgData(2:3:end),imgW,imgH)';
imgB = reshape(imgData(3:3:end),imgW,imgH)';
img(:,:,1) = imgR;
img(:,:,2) = imgG;
img(:,:,3) = imgB;
Note that this example assumes image is in the RGB color space.
  3 件のコメント
Michele Mondini
Michele Mondini 2018 年 2 月 21 日
How about greyscale images? Did you ever try to do the same with mono8 encoding?
Antti
Antti 2018 年 2 月 23 日
We had to use fixed size arrays instead of msg.Width. If someone knows how to make this work dynamically, I would like to know ;)
imgW = 1280;%msg.Width;
imgH = 720;%msg.Width;
The greyscale image work the same way but instead of parsing the RGB, you can use the whole array as it is. Depending on the data types, you might also need to converts the data to doubles.
img = zeros(imgH,imgW);
%Reshape image data
img = double(reshape(msg.Data(1:end),imgW,imgH)');

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeROS Log Files and Transformations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by