使用 Qt 显示灰度图像 [英] Display grayscale image using Qt

查看:114
本文介绍了使用 Qt 显示灰度图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Qt 显示灰度图像.图像数据从包含 256x256 浮点数据的 .txt 文件加载.图像不涉及标题.我已经尝试了这个 链接

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

我使用QLabel 类来调用我的uchar* image_data_arraysetPixmap.即使我可以打开 Qt GUI 窗口,但该窗口只显示空白屏幕.

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

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

推荐答案

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;

使用 RGBA 以获得良好的灰度.

Use RGBA for good grayscale.

这篇关于使用 Qt 显示灰度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆