4
votes

I have been trying to display a gray-scale image using Qt. The image data is loaded from a .txt file that contains 256x256 float data. There is no header involved for the image. I have tried the solution posted in this link

I used QLabel class to call setPixmap of my uchar* image_data_array. Even though I could get a Qt GUI window to open, but the window shows just blank screen.

imageLabel -> setPixmap(QPixmap::fromImage(*myImage));

2
It is preferable to split the two questions you have into two different questions, so it would be easier to the people who answer to focus on each of them individually. It will also help other people in the future to access the relevant parts.Shai
If you are asking 2 questions here, I agree with Shai. I would suggest that you make sure displaying an unaltered QPixmap in your QLabel works and then move on to altering it.user336063

2 Answers

3
votes
QImage img = AImage;
if (!AImage.isNull())
{
    int pixels = img.width() * img.height();
    if (pixels*(int)sizeof(QRgb) <= img.byteCount())
    {
        QRgb *data = (QRgb *)img.bits();
        for (int i = 0; i < pixels; i++)
        {
            int val = qGray(data[i]);
            data[i] = qRgba(val, val, val, qAlpha(data[i]));
        }
    }
}
return img;

Use RGBA for good grayscale.

2
votes

How are you loading the QImage with the grey image data ?

QT doesn't have a greyscale image type, only a bilevel type. You can either create an RGB image with R=G=B=grey. Or more preferably use QImage::Format_Indexed8 and create a colour table where each entry has the same value as the index. i.e.

QImage *qi = new QImage(data_ptr, width, height, QImage::Format_Indexed8);
QVector<QRgb> my_table;
for(int i = 0; i < 256; i++) my_table.push_back(qRgb(i,i,i));
qi->setColorTable(my_table);