PDA

View Full Version : Displaying 16bit grayscale image; white shows up as blue??



dsab123
8th August 2012, 16:11
Hey everybody,

So I'm loading an image from an OpenCV Mat and attempting to display it on a Qlabel like so:




Mat image;
...
QImage img((uchar*)image.data, image.cols, image.rows, QImage::Format_RGB16);
ui->maxLabel->setPixmap(QPixmap::fromImage(img, Qt::MonoOnly)); //maxLabel is the QLabel


the image shows up, but where the image is supposed to be white, it shows up blue; I looked around and found this post (http://qt-project.org/forums/viewthread/6863), and attempted to fix the image like so, by iterating through every pixel and setting all red and green values to the current blue value (not exactly sure if that's what the post above was suggesting):



for(int i = 0; i < img.width(); i++) {
for(int j = 0; j < img.height(); j++) {
img.setPixel(i, j, qRgb(qBlue(img.pixel(i,j)), qBlue(img.pixel(i,j)), qBlue(img.pixel(i,j))));
}
}


Unfortunately, this didn't work as well as I had hoped, as the colors are much 'harder' (the gray pixels are either completely white or black).

This is really my first time messing around with QImages and pixels and such; does anyone recommend another method of loading/displaying, or did I take a wrong turn somewhere when looking at the 'Pixel Manipulation' section in the QImage doc?

Thanks!!


EDIT: Apparently, when the image loads with the blue hue, the image's colors already look pretty 'hard'; so then I must be loading the image incorrectly...

EDIT: if I use QImage::rgbSwapped(), the image looks perfect, except that it's scrunched in the top half of the QLabel, and the bottom half looks like gibberishy-garbage..

high_flyer
9th August 2012, 17:27
EDIT: if I use QImage::rgbSwapped(), the image looks perfect, except that it's scrunched in the top half of the QLabel, and the bottom half looks like gibberishy-garbage..
Show your current working code.

dsab123
10th August 2012, 14:21
Ok, so the image isn't scrunched up anymore, but still looks different; here's what I have so far:



Mat image;
QLabel maxLabel;

//do stuff with image;

QImage img((uchar*)image.data, image.cols, image.rows, image.step, QImage::Format_RGB16);

img = img.rgbSwapped();
for(int i = 0; i < img.width(); i++) {
for(int j = 0; j < img.height(); j++) {
img.setPixel(i, j, qRgb(qBlue(img.pixel(i,j)), qBlue(img.pixel(i,j)), qBlue(img.pixel(i,j))));
}
}

ui->maxLabel->setPixmap(QPixmap::fromImage(img));



8121

The image on the left is Qt's representation, while the one on the right is openCV's.

Thanks high_flyer!

dsab123
14th August 2012, 15:47
After doing a bit more reading on QImage, I found that it cannot display a pure 16 bit image, i.e. 16 bits per pixel; the closest it can get is 8 bpp, according to the 'Image Transformations' section of QImage, http://doc.qt.nokia.com/4.7-snapshot/qimage.html, and this S.O. thread: http://stackoverflow.com/questions/1846106/is-qimage-able-to-open-and-render-pure-16-bit-images.

I think my only options for now are to either figure out how to embed OpenCV's namedWindow inside my GUI (which I think will probably be pretty hard), or use some other library for displaying the images (libtiff?).