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

Revision history [back]

click to hide/show revision 1
initial version

Depending on the encoding of the image you have to support multiple conversions. Here mono8 and bgr8 images are supported:

void imageCallback(const sensor_msgs::ImageConstPtr &msg)
{
  QImage::Format format;
  if (msg->encoding == "mono8")
    format = QImage::Format_Grayscale8;
  else if (msg->encoding == "bgr8")
    // There is no BGR format in Qt; B and R channels will be inverted later
    format = QImage::Format_RGB888;
  else
  {
    ROS_ERROR_STREAM("Image pixel format not supported!");
    return;
  }

  QImage source_image(&msg->data[0], msg->width, msg->height, format);
  if (msg->encoding == "bgr8") // From RGB8 to BGR8
    source_image = source_image.rgbSwapped();

  if (source_image.isNull())
  {
    ROS_ERROR_STREAM("Failed to read image");
    return;
  }

  // Use image ...
}