Main Content

Image

(To be removed) Create image message

Since R2019b

Image object will be removed in a future release. Use message structure format when you create ROS messages using the rosmessage function, by specifying the Dataformat name-value argument as "struct". You can use rosReadImage and rosWriteImage functions to read and write image data from ROS messages respectively. For more information, see ROS Message Structure Functions.

Description

The Image object is an implementation of the sensor_msgs/Image message type in ROS. The object contains the image and meta-information about the message. You can create blank Image messages and populate them with data, or subscribe to image messages over the ROS network. To convert the image to a MATLAB® image, use the readImage function.

Creation

Description

example

msg = rosmessage('sensor_msgs/Image') creates an empty Image object. To specify image data, use the msg.Data property. You can also get these image messages off the ROS network using rossubscriber.

Properties

expand all

This property is read-only.

Message type of ROS message, returned as a character vector.

Data Types: char

This property is read-only.

ROS Header message, returned as a Header object. This header message contains the MessageType, sequence (Seq), timestamp (Stamp), and FrameId.

Image height in pixels, specified as a scalar.

Image width in pixels, specified as a scalar.

Image encoding, specified as a character vector.

Example: 'rgb8'

Image byte sequence, specified as true or false.

  • true —Big endian sequence. Stores the most significant byte in the smallest address.

  • false —Little endian sequence. Stores the least significant byte in the smallest address.

Full row length in bytes, specified as an integer. This length depends on the color depth and the pixel width of the image. For example, an RGB image has 3 bytes per pixel, so an image with width 640 has a step of 1920.

Image data, specified as a uint8 array.

Object Functions

readImage(To be removed) Convert ROS image data into MATLAB image
writeImage(To be removed) Write MATLAB image to ROS image message

Examples

collapse all

Read and write a sample ROS Image message by converting it to a MATLAB image. Then, convert a MATLAB® image to a ROS message.

Load sample ROS messages and inspect the image message data. The img object is a sample ROS Image message object.

exampleHelperROSLoadMessages
img
img = 
  ROS Image message with properties:

    MessageType: 'sensor_msgs/Image'
         Header: [1x1 Header]
         Height: 480
          Width: 640
       Encoding: 'rgb8'
    IsBigendian: 0
           Step: 1920
           Data: [921600x1 uint8]

  Use showdetails to show the contents of the message

Create a MATLAB image from the Image message using readImage and display it.

I = readImage(img);
imshow(I)

Create a ROS Image message from a MATLAB image.

imgMsg = rosmessage('sensor_msgs/Image');
imgMsg.Encoding = 'rgb8'; % Specifies Image Encoding Type
writeImage(imgMsg,I)
imgMsg
imgMsg = 
  ROS Image message with properties:

    MessageType: 'sensor_msgs/Image'
         Header: [1x1 Header]
         Height: 480
          Width: 640
       Encoding: 'rgb8'
    IsBigendian: 0
           Step: 1920
           Data: [921600x1 uint8]

  Use showdetails to show the contents of the message

msg = rosmessage('sensor_msgs/Image')
msg = 
  ROS Image message with properties:

    MessageType: 'sensor_msgs/Image'
         Header: [1x1 Header]
         Height: 0
          Width: 0
       Encoding: ''
    IsBigendian: 0
           Step: 0
           Data: [0x1 uint8]

  Use showdetails to show the contents of the message

Version History

Introduced in R2019b

collapse all

R2021a: ROS Message Structure Functions

You can now create messages as structures with fields matching the message object properties. Using structures typically improves performance of creating, updating, and using ROS messages, but message fields are no longer validated when set. Message types and corresponding field values from the structures are validated when sent across the network.

To support message structures as inputs, new functions that operate on specialized ROS messages have been provided. These new functions are based on the existing object functions of message objects, but support ROS and ROS 2 message structures as inputs instead of message objects.

The object functions will be removed in a future release.

Message TypesObject Function NameNew Function Name

Image

CompressedImage

readImage

writeImage

rosReadImage

rosWriteImage

LaserScan

readCartesian

readScanAngles

lidarScan

plot

rosReadCartesian

rosReadScanAngles

rosReadLidarScan

rosPlot

PointCloud2

apply

readXYZ

readRGB

readAllFieldNames

readField

scatter3

rosApplyTransform

rosReadXYZ

rosReadRGB

rosReadAllFieldNames

rosReadField

rosPlot

QuaternionreadQuaternion

rosReadQuaternion

OccupancyGrid

readBinaryOccupanyGrid

readOccupancyGrid

writeBinaryOccupanyGrid

writeOccupanyGrid

rosReadOccupancyGrid

rosReadBinaryOccupancyGrid

rosReadOccupancyGrid

rosWriteBinaryOccupancyGrid

rosWriteOccupancyGrid

OctomapreadOccupancyMap3D

rosReadOccupancyMap3D

PointStamped

PoseStamped

QuaternionStamped

Vector3Stamped

TransformStamped

apply

rosApplyTransform

All messagesshowdetails

rosShowDetails