Main Content

Image

Create image message

Since R2019b

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

readImageConvert ROS image data into MATLAB image
writeImageWrite 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)

Figure contains an axes object. The axes object contains an object of type image.

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