Read A ROS Image Message In Simulink®

This example requires Computer Vision Toolbox® and Robotics System Toolbox®.

Start a ROS network.

rosinit
Initializing ROS master on http://ah-rhosea:11311/.
Initializing global node /matlab_global_node_45601 with NodeURI http://ah-rhosea:49292/

Load sample messages to send including a sample image message, img. Create a publisher to send a ROS Image message on the '/image_test' topic. Specify the message type as '/sensor_msgs/Image'. Send the image message.

imgcell = load('imgdata.mat','img');
img = imgcell.img;
pub = rospublisher('/image_test','sensor_msgs/Image');
send(pub,img)

Open the Simulink® model for subscribing to the ROS message and reading in the image from the ROS.

Ensure that the Subscribe block is subscribing to the '/image_test' topic. In the menu under Tools > Robot Operating System > Manage Array Lengths, verify the Data array has a maximum length greater than the sample image (921,600 pixels).

open_system('read_image_example_model.slx')

Run the model. The Video Viewer shows the sample image.

Stop the simulation and shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_45601 with NodeURI http://ah-rhosea:49292/
Shutting down ROS master on http://ah-rhosea:11311/.