Ask Your Question

Converting ROS image to QImage

asked 2012-07-10 08:10:13 -0500

StFS gravatar image

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

ngrennan gravatar image


I'm making a Qt based GUI for our ROS application and part of it is to display images from a ROS topic.

I've managed to create a descendant class of QLabel that displays an RGB image from our cameras (/camera/image_raw topic) but when I try to display a grayscale image /camera/image_mono) I get weird results.

Here is the code I'm using:

img_format_table = {'rgb8': QtGui.QImage.Format_RGB888, 'mono8': QtGui.QImage.Format_Mono}


    def up_camera_callback(self, data):
            format = img_format_table[data.encoding]

            image = QtGui.QImage(, data.width, data.height, format)

        except Exception, e:

The up_camera_signal is connected to my "video display" widget and just displays the image on it. As I said, this works fine for RGB images (when the data.encoding is 'rgb8') but if I do this for 'mono8' images I see strange results:

image description

I have also tried mapping 'mono8' to QtGui.QImage.Format_MonoLSB but that doesn't change anything (documentation for the QImage formatting is at

Can anyone comment on this? Is there anything specific about the mono8 format in ROS that might be causing these problems?

Kind regards, Stefan Freyr

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-07-10 10:51:56 -0500

ipso gravatar image

updated 2012-07-10 10:57:31 -0500

I have no direct experience with either QImages or ROS image data, but reading the QImage documentation -- and specifically QImage#image-formats -- I came across this:

Monochrome images are stored using 1-bit indexes into a color table with at most two colors.

Looking at some code in various ROS packages that deals with mono8 images, it does not seem that ROS uses paletted images, which could explain your strange results.

Hm, AFAICT mono8 isn't actually monochrome, but more like 8bit greyscale. According to QImage#PySide.QtGui.PySide.QtGui.QImage.Format QImage does not support 8bit images, at least not without a palette. Perhaps you need to convert the mono8 image to a 16bit or 32bit pp image first?

edit flag offensive delete link more

answered 2019-11-04 01:34:20 -0500

VictorLamoine gravatar image

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;
    ROS_ERROR_STREAM("Image pixel format not supported!");

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

  // Use image ...
edit flag offensive delete link more



Hi. The constructor for QImage used in your answer assumes the data is 32-bit aligned. Alternative use QImage source_image(&msg->data[0], msg->width, msg->height, msg->step, format); to support data that are aligned differently

NordThor gravatar image NordThor  ( 2020-04-14 03:21:40 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2012-07-10 08:10:13 -0500

Seen: 2,538 times

Last updated: Nov 04 '19