我想弄清楚如何将图像从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中有更好的方法吗?
答案 0 :(得分:1)
imdata是你的16位图像缓冲区
#include <cstdint>
uint16_t *imdata;
for (int i = 0 ; i < imheight ; ++i)
{
//preincrement, is faster than post
for (int j = 0 ; j < imwidth ; ++j)
{
//imdata[i*imwidth+j]>>8 removes least significant 8bits
//imdata has one added to adjust for components
qi->setPixel(j,i,qRgb((uint8_t)(imdata[i*imwidth+j]>>8),(uint8_t)(imdata[i*imwidth+j+1]>>8),(uint8_t)(imdata[i*imwidth+j+2]>>8)));
}
}
答案 1 :(得分:1)
如果我正确理解你的问题,你会得到一个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;
}