PDA

View Full Version : How to convert a 32 bit RGB image to 8 bit gray scale image



vinodpaul
29th December 2011, 12:39
Hi, can someone please tell me how to convert a 32 bit RGB image to 8 bit gray scale image. I am using QImage

Thanks

Zlatomir
29th December 2011, 12:58
convertToFormat (http://developer.qt.nokia.com/doc/qt-4.8/qimage.html#convertToFormat) should do the trick.

vinodpaul
30th December 2011, 05:44
Hi, thanks for the reply , but that does not work :( . I tried to convert to QImage::Format_Indexed8, but the grayscale image is not proper.

Added after 1 30 minutes:

it Worked when I did like this


QImage ImageActionConvert::convertToGray(*pInputImage)
{

if ( pInputImage && !pInputImage->isNull() )
{
QImage retImg(pInputImage->width(),pInputImage->height(),QImage::Format_Indexed8);
QVector<QRgb> table( 256 );
for( int i = 0; i < 256; ++i )
{
table[i] = qRgb(i,i,i);
}
retImg.setColorTable(table);
for(int i =0; i< pInputImage->width();i++)
{
for(int j=0; j< pInputImage->height();j++)
{
QRgb value = pInputImage->pixel(i,j);
retImg.setPixel(i,j,qGray(value));
}

}
return retImg;
}