How to read data drom Image message



I am reading an image from a simulated camera placed somewhere in the world. What I would like to do is to pack the image data into a base64 encoded string and send it to another application. Here is how I try to do it:

void callback(ConstImageStampedPtr& _msg)
std_msgs::String msg;

int width = _msg->image().width();
int height = _msg->image().height();
std::string data = _msg->image().data();

std::string encoded = base64_encode(data); = encoded;



The problem is that the string that I get does not seem to be a valid image. I would like to understand in what form the image is stored in the message and how I can get it from there.




You can use gazebo::common::Image to convert this data into RGB values:

  unsigned char *rgbData = NULL;
  unsigned int rgbDataSize = 0;

  // Convert the image data to RGB
  common::Image img;
      (unsigned char *)(_msg->image().data().c_str()),
      width, height, (common::Image::PixelFormat)(_msg->image().pixel_format()));

  img.GetRGBData(&rgbData, rgbDataSize);

  // work on rgbData here

  delete [] rgbData;


Thanks a lot, that helped.