我想弄清楚如何将图像从 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/