Qt编程之转换成8,16bit的灰度图

代码大致是下面这样的。是8bit的灰度图,不是16bit。

 1 QString img_path = "C:\Users\Yajun Dou\Desktop\test.bmp";
 2 QImage buff(img_path);
 3 QImage image; 
 4 uchar* pImageData1 = NULL,*pImageData2 = NULL;
 5 
 6 pImageData1 = buff.bits();
 7 
 8 image = buff.convertToFormat(QImage::Format_Indexed8);
 9 
10 QVector<QRgb> table(256);
11 for( int i = 0; i < 256; ++i )
12 {
13 table[i] = qRgb(i,i,i);
14 }
15 
16 image.setColorTable(table);
17 
18 for(int i =0; i< image.width();i++)    
19 {
20 for(int j=0; j< image.height();j++)
21 {
22 QRgb pix_value = image.pixel(i,j);
23 image.setPixel(i,j,qGray(pix_value));
24 }
25 
26 }
27 
28 pImageData2 = image.bits();
29 image.save("D:\out.bmp",0,100);
30 
31  

references:

http://qt-project.org/faq/answer/how_can_i_convert_a_colored_qpixmap_into_a_grayscaled_qpixmap

http://www.qtcentre.org/threads/46596-How-to-convert-a-32-bit-RGB-image-to-8-bit-gray-scale-image

http://www.qtcentre.org/threads/19296-is-there-a-simple-and-quick-way-to-convert-color-images-to-gray-image

http://www.qtcentre.org/threads/15186-QImage-Greyscale-8-bits-pixel

http://stackoverflow.com/questions/15672743/convert-16-bit-grayscale-to-qimage

原文地址:https://www.cnblogs.com/foohack/p/3889813.html