I am trying to figure out how to convert an image from uint16 to uint8. I am basically having to read the image in float, and I need to display it in unsigned char.
I have the following:
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]));
}
}
Is there a better way of performing this in Qt?
If I understand your question correctly, you got an image in RGB format where each color component is 16 bits, and you want to load this using QImage.
You can do it like this :
where the
Convertfunction is defined like this :