ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

image.publish() via image transport

asked 2013-02-12 08:51:08 -0500

pmarinplaza gravatar image

updated 2014-01-28 17:15:12 -0500

ngrennan gravatar image


I have troubles to publish images via image_transport.

This is what I want to do:

    right_image_.header.frame_id = left_image_.header.frame_id = config_.frame_id;
    left_image_.header.stamp = right_image_.header.stamp=image->header.stamp;
    left_image_.height = right_image_.height=image->height;
    left_image_.width = right_image_.width=image->width;
    left_image_.encoding = right_image_.encoding = image->encoding;
    //Split image into left image and right image
    left_image_.step = right_image_.step = image->step;
    int image_size = image->height*image->step;;;
    memcpy(&[0], &image->data[0], image_size);     
    // the image of right camera is the first half of the deinterlaced image.
    memcpy(&[0], &image->data[image_size], image_size);     
    // the image of left camera is the second half of the de interlaced image.

    // Publish via image_transport
left_image_pub_.publish(left_image_, ci);
right_image_pub_.publish(right_image_, ci);

But publish method only admit

void publish(const sensor_msgs::ImageConstPtr& image, const sensor_msgs::CameraInfoConstPtr& info) const;

And I have only sensor_msgs::Image.

How can I convert from sensor_msgs::Image to sensor_msgs::ImageConstPtr ??

Thanks a lot.

Best regards

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-02-13 14:20:11 -0500

Thomas gravatar image

updated 2013-02-13 14:23:07 -0500

To publish an image, please allocate the shared pointer yourself. I.e.

#include <boost/make_shared.hpp>

sensor_msgs::ImagePtr image = boost::make_shared<sensor_msgs::Image> ();
image->field = something; // fill your fields.
pub.publish (image);

Please prefer std::copy over memcpy. It has as efficient and behave properly on all C++ types unlike memcpy.

edit flag offensive delete link more


Thank you Thomas. It works very well.

pmarinplaza gravatar image pmarinplaza  ( 2013-02-18 02:53:09 -0500 )edit

answered 2013-02-12 13:49:32 -0500

updated 2013-02-12 13:50:24 -0500

enter code here`pmarinplaza,

if your sensor_msgs::image has been properly built, then sensor_msgs::imageConstPtr& will be automatically generated. Have a look in your sensor_msgs/image.h, ConstPtr should be there as an element in the Image_ structure. So there should be no conversion (by you) required.

It should be a matter of simply

left_image_pub_.publish(left_image_.ConstPtr, ci);
edit flag offensive delete link more


Thanks but even with that The code hates me. I will update my question with the result.

pmarinplaza gravatar image pmarinplaza  ( 2013-02-13 02:42:15 -0500 )edit

Question Tools


Asked: 2013-02-12 08:51:08 -0500

Seen: 772 times

Last updated: Feb 13 '13