Ask Your Question
2

Converting ROS image to QImage

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

StFS gravatar image

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

ngrennan gravatar image

Hello.

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):
        try:
            format = img_format_table[data.encoding]

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

        except Exception, e:
            rospy.logerr(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 http://www.pyside.org/docs/pyside/PySide/QtGui/QImage.html).

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
0

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

ipso gravatar image

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

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
0

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

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;
  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 ...
}
edit flag offensive delete link more

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

Stats

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

Seen: 1,670 times

Last updated: Nov 04