Main Content

Read Image

Extract image from ROS 2 Image message

Since R2021b

  • Read Image ROS 2

ROS Toolbox / ROS 2


The Read Image block extracts an image from a ROS 2 sensor_msgs/Image or sensor_msgs/CompressedImage message. You can select the message parameters of a topic active on a live ROS 2 network, or specify the message parameters separately. The ROS 2 messages are specified as a nonvirtual bus. Use the Subscribe block output to receive a message from a ROS 2 network and input the message to the Read Image block.


When reading ROS 2 image messages from the network, the Data property of the message can exceed the maximum array length set in Simulink®. Follow the steps below to increase the maximum array length for all message types in the model:

  1. Enable ROS options by selecting the Robot Operating System (ROS) app under the Apps tab and configure the ROS network parameters appropriately.

  2. From the Prepare section under Simulation tab, select ROS Toolbox > Variable Size Messages.

  3. Uncheck Use default limits for this message type and then in the Maximum length column, increase the length based on the number of pixels in the image.



expand all

ROS 2 sensor_msgs/Image or sensor_msgs/CompressedImage message, specified as a nonvirtual bus. You can use the Subscribe block to get a message from an active ROS 2 network.

Data Types: bus


expand all

Extracted image signal from a ROS 2 message, returned as an M-by-N-by-3 matrix for color images, and an M-by-N matrix for grayscale images. The matrix contains the pixel data from the Data property of the ROS 2 message.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16

Alpha channel for image, returned as an M-by-N matrix. This matrix is the same height and width as the image output. Each element has a value in the range [0,1] that indicates the opacity of the corresponding pixel, with a value of 0 being completely transparent.


For CompressedImage messages, the alpha channel returns all zeros if the Show Alpha output port is enabled.


Enable Show Alpha output port parameter.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16

Error code for image conversion, returned as a scalar. The error code values are:

  • 0 — Successfully converted the image message.

  • 1 — Incorrect image encoding. Check that the incoming message encoding matches the Image Encoding parameter.

  • 2 — The dimensions of the image message exceed the limits specified in the Maximum Image Size parameter.

  • 3 — The Data field of the image message was truncated. See Manage Array Sizes for ROS Messages in Simulink to increase the maximum length of the array.

  • 4 — Image decompression failed.

Data Types: uint8


expand all

Maximum image size, specified as a two-element [height width] vector.

Select Configure using ROS 2... to set this parameter automatically using an active topic on a ROS 2 network. You must be connected to the ROS 2 network.

Image encoding for the input ImageMsg. Select the encoding type that matches the Encoding property of the message. For more information about encoding types, see rosReadImage.

Toggle alpha channel output port on or off, if the selected encoding type supports alpha channels.


To enable this parameter, set the Image Encoding parameter to an encoding type that supports alpha channels.

Toggle the ErrorCode port on or off, to monitor errors.

Toggle whether to output a variable-size signal. Use variable-sized signals only if you expect the image size to change over time. For more information about variable-size signals, see Variable-Size Signal Basics (Simulink).

Extended Capabilities

Version History

Introduced in R2021b