Main Content

writeImage

(To be removed) Write MATLAB image to ROS image message

Since R2019b

writeImage will be removed in a future release. Use rosWriteImage instead. For more information, see ROS Message Structure Functions.

Description

example

writeImage(msg,img) converts the MATLAB® image, img, to a message object and stores the ROS compatible image data in the message object, msg. The message must be a 'sensor_msgs/Image' message. 'sensor_msgs/CompressedImage' messages are not supported. The function does not perform any color space conversion, so the img input needs to have the encoding that you specify in the Encoding property of the message.

writeImage(msg,img,alpha) converts the MATLAB image, img, to a message object. If the image encoding supports an alpha channel (rgba or bgra family), specify this alpha channel in alpha. Alternatively, the input image can store the alpha channel as its fourth channel.

Examples

collapse all

Read an image.

image = imread('imageMap.png');

Create a ROS image message. Specify the default encoding for the image. Write the image to the message.

msg = rosmessage('sensor_msgs/Image');
msg.Encoding = 'rgb8';
writeImage(msg,image);

Input Arguments

collapse all

'sensor_msgs/Image' ROS image message, specified as an Image object handle. 'sensor_msgs/Image' image messages are not supported.

Image, specified as a matrix representing a grayscale or RGB image or as an m-by-n-by-3 array, depending on the sensor image.

Alpha channel, specified as a uint8 grayscale image. Alpha must be the same size and data type as img.

Tips

You must specify the correct encoding of the input image in the Encoding property of the image message. If you do not specify the image encoding before calling the function, the default encoding, rgb8, is used (3-channel RGB image with uint8 values). The function does not perform any color space conversion, so the img input needs to have the encoding that you specify in the Encoding property of the message.

All encoding types supported for the readImage are also supported in this function. For more information on supported encoding types and their representations in MATLAB, see readImage.

Bayer-encoded images (bayer_rggb8, bayer_bggr8, bayer_gbrg8, bayer_grbg8, and their 16-bit equivalents) must be given as 8-bit or 16-bit single-channel images or they do not encode.

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