How to publish a CompressedImage in ROS2 Foxy?

asked 2021-09-01 10:16:55 -0500

stan-g gravatar image

I am rewriting some ROS1 code and I cannot find a ROS2 way to do the same thing I did before

Basically imagine we have a device driver that already sends compressed JPEG images. I want to publish those on a compressed topic.

Here's a related ROS1 question : link

And here a snippet that works in ROS1

image_pub_ =  node_handle_.advertise<sensor_msgs::CompressedImage>("image/compressed", 10);
cv::Mat image_cv = cv::imdecode(jpeg_data, -1);
cv_bridge::CvImage img_bridge = 
                  cv_bridge::CvImage(header, sensor_msgs::image_encodings::MONO8, image_cv);
sensor_msgs::CompressedImage img_msg;

How do I do the same in ROS2 Foxy?

I tried using compressed_image_transport::CompressedPublisher but I later realized that won't work. the CompressedPublisher publish method accepts a normal Image, compresses the image, and then pushes it to


Before I try to somehow manually do the same publish call, i just want to be sure if this is even implemented in ROS2? Seems to be way more difficult than necessary. A normal image_transport::Publisher also has no methods accepting a sensor_msgs::msg::CompressedImage.

Thank you in advance

edit retag flag offensive close merge delete



image_pub_ = node->create_publisher<sensor_msgs::msg::CompressedImage>("image/compressed", 10);
sensor_msgs::msg::CompressedImage img_msg;

not work?

ChuiV gravatar image ChuiV  ( 2021-09-03 10:04:02 -0500 )edit

Hi stan,

Did you find a solution to this problem?

cmetz gravatar image cmetz  ( 2022-02-14 09:35:38 -0500 )edit