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 cha开发者_如何转开发r)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 :
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 );
where the Convert
function is defined like this :
unsigned char Convert( const unsigned short int v )
{
return v >> 8;
}
imdata is your 16bit image buffer
#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)));
}
}
精彩评论