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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值