How to read data drom Image message


#1

Hello,

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);

msg.data = encoded;

pub.publish(msg);

}

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.

Thanks.


#2

Hello,

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;
  img.SetFromData(
      (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;

#3

Thanks a lot, that helped.