Robotics StackExchange | Archived questions

How to publish a CompressedImage in ROS2 Foxy?

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;
img_bridge.toCompressedImageMsg(img_msg);
image_pub_.publish(img_msg);

How do I do the same in ROS2 Foxy?

I tried using compressedimagetransport::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

publish_fn(compressed);

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 imagetransport::Publisher also has no methods accepting a sensormsgs::msg::CompressedImage.

Thank you in advance

Asked by stan-g on 2021-09-01 10:16:55 UTC

Comments

Does

image_pub_ = node->create_publisher<sensor_msgs::msg::CompressedImage>("image/compressed", 10);
[...]
sensor_msgs::msg::CompressedImage img_msg;
img_bridge.toCompressedImage(img_msg);
image_pub_.publish(img_msg);

not work?

Asked by ChuiV on 2021-09-03 10:04:02 UTC

Hi stan,

Did you find a solution to this problem?

Asked by cmetz on 2022-02-14 10:35:38 UTC

Answers