Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

user interface - Display grayscale image using Qt

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...