Display grayscale image using Qt

后端 未结 2 1722
天涯浪人
天涯浪人 2020-12-21 03:59

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 im

相关标签:
2条回答
  • 2020-12-21 04:19

    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);
    
    0 讨论(0)
  • 2020-12-21 04:38
    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.

    0 讨论(0)
提交回复
热议问题