c++ - 在QT中将16位QImage转换为8位无符号字符

标签 c++ image qt image-processing

我想弄清楚如何将图像从 uint16 转换为 uint8。我基本上必须以 float 形式读取图像,并且需要以 unsigned char 形式显示它。

我有以下内容:

   float two_eight =  pow(2.0,8);
   float two_sixteen = pow(2.0,16);
    QImage *qi = new QImage(imwidth, imheight, QImage::Format_RGB32);
        for (int i = 0 ; i < imheight ; i++)
        {
             for (int j = 0 ; j < imwidth ; j++)
             {
                    floatData[i*imwidth+j] = (two_eight* floatData[i*imwidth+j])/two_sixteen;
                    qi->setPixel(j,i,qRgb((unsigned char)floatData[i*imwidth+j],(unsigned char)floatData[i*imwidth+j],(unsigned char)floatData[i*imwidth+j]));


             }
         }

在 Qt 中是否有更好的方法来执行此操作?

最佳答案

如果我正确理解你的问题,你会得到一个 RGB 格式的图像,其中每个颜色分量都是 16 位,你想使用 QImage 加载它。

你可以这样做:

std::vector< unsigned short int > inData;
// load the data into a vector
std::vector< unsigned char > outData( inData.size(), 0 );
std::transfrorm( inData.begin(), inData.end(), outData.begin(), Convert );
// create the image object
QImage image( &outData[0],imwidth, imheight, QImage::Format_RGB32 );

Convert 函数定义如下:

unsigned char Convert( const unsigned short int v )
{
  return v >> 8;
}

关于c++ - 在QT中将16位QImage转换为8位无符号字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6310969/

相关文章:

html - 发生溢出时将图片放入水平滚动的表格单元格中

Python 显示图像时出错

c++ - QFileDialog::getOpenFileName 调试时崩溃,显然是由项目名称引起的?

c++ - QT套接字读取丢失字节

QTextEdit 和类似 bash 的彩色输出模拟

c++ - OpenCV中七个胡不变矩函数的含义

c++ - 我应该使用 Direct3D 还是仅使用 Windows GDI 来获得精美/高性能的 Win32 应用程序 UI?

python - 如何在 Python OpenCV 中绘制图像对数色度图

c++ - 不需要输出。为什么 "gx+gy"没有输出?

.net - 在 C# P/Invoke 中编码(marshal) C++ "string"类