0
$\begingroup$

Rosanswers logo

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


Originally posted by StFS on ROS Answers with karma: 182 on 2012-07-10

Post score: 2

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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?


Originally posted by ipso with karma: 1416 on 2012-07-10

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$
0
$\begingroup$

Rosanswers logo

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 ...
}

Originally posted by VictorLamoine with karma: 1505 on 2019-11-04

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by NordThor on 2020-04-14:
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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.