设置16位灰度QImage的像素值 [英] Set pixel value of 16 bit grayscale QImage

查看:496
本文介绍了设置16位灰度QImage的像素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个宽度(imagewidth")和高度(imageheight")的 16 位图像.数据当前存储在一个长度为 ("imagewidth"*"imageheight") 的无符号短整型数组中

I have an 16 bit image of width ("imagewidth") and height ("imageheight"). The data is currently stored in an unsigned short int array of length ("imagewidth"*"imageheight")

我想从名为数据"的数据集创建一个 16 位灰度 QImage(使用 Qt 5.14).

I want to create an 16 bit grayscale QImage (using Qt 5.14) from my dataset which is called "data".

这是我正在使用的代码:

This is the code that I am using:

QImage image = Qimage(imagewidth,imageheight,QImage::Format_Grayscale16);

for(int i=0;i<imagewidth;i++)
{
   for(int j=0;j<imageheight;j++)
   {

    uint pixelval = data[i+j*imagewidth];
    QRgb color = qRgb(pixelval, pixelval, pixelval);
    image.setPixel(i,j, color);

   }
}

代码正在运行,我正在获取图像,但我只能以 255 的增量获取值...所以 0、255、...

The code is working and I am getting an image but I only get values in increments of 255... So 0, 255, ...

如何为每个像素设置从 0 到 65535 的实际像素值?

How I can set the actual pixel value for each pixel from 0 to 65535 ?

推荐答案

@Scheff,感谢您的输入!我现在使用以下代码:

@Scheff, thanks for the input ! I am now using the following code:

Qimage = QImage(imagewidth,imageheight,QImage::Format_Grayscale16);


    for (int j = 0; j < imageheight; ++j)
    {
       quint16 *dst =  reinterpret_cast<quint16*>(Qimage.bits() + j * Qimage.bytesPerLine());

       for (int i = 0; i < imagewidth; ++i)
       {

            unsigned short pixelval =  static_cast<unsigned short>(image[i + j * imagewidth]);

            dst[i] = pixelval;

       }

    }

这篇关于设置16位灰度QImage的像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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