【转】真彩bmp图像转化为灰度图

感谢大神分享 原链接https://blog.csdn.net/u011450295/article/details/37316909
下面是用c语言实现的将 真彩 bmp图像 转化为灰度图
  1. #include "stdafx.h"  
  2. #include<stdio.h>  
  3. #include<windows.h>  
  4.   
  5. int _tmain(int argc, _TCHAR* argv[])  
  6. {  
  7.     int bmpHeight;  
  8.     int bmpWidth;  
  9.     unsigned char *pBmpBuf;  
  10.     RGBQUAD *pColorTable;  
  11.     int biBitCount;  
  12.   
  13.     //读取bmp文件  
  14.     FILE *fp=fopen("dog1.bmp","rb");  
  15.     if(fp==0)  
  16.         return 0;  
  17.     fseek(fp,sizeof(BITMAPFILEHEADER),0);  
  18.   
  19.     BITMAPINFOHEADER head;  
  20.     fread(&head,40,1,fp);  
  21.     bmpHeight=head.biHeight;  
  22.     bmpWidth=head.biWidth;  
  23.     biBitCount=head.biBitCount;  
  24.       
  25.     if(biBitCount==8)  
  26.     {  
  27.         pColorTable=new RGBQUAD[256];  
  28.         fread(pColorTable,sizeof(RGBQUAD),256,fp);  
  29.     }  
  30.     int LineByte=(bmpWidth*biBitCount/8+3)/4*4;//保证每一行字节数都为4的整数倍  
  31.     pBmpBuf=new unsigned char[LineByte*bmpHeight];  
  32.     fread(pBmpBuf,LineByte*bmpHeight,1,fp);  
  33.     fclose(fp);  
  34.     测试读取功能模块  
  35.     printf("bmpWidth:%d ,bmpHeight: %d,biBitCount:%d \n",bmpWidth,bmpHeight,biBitCount);  
  36.   
  37.     //将24位真彩图灰度化并保存  
  38.   
  39.     FILE *fp1=fopen("dog2.bmp","wb");  
  40.     if(fp1==0)  
  41.         return 0;  
  42.     int LineByte1=(bmpWidth*8/8+3)/4*4;  
  43.     //修改文件头,其中有两项需要修改,分别为bfSize和bfOffBits  
  44.     BITMAPFILEHEADER bfhead;  
  45.     bfhead.bfType=0x4D42;  
  46.     bfhead.bfSize=14+40+256*sizeof(RGBQUAD)+LineByte1*bmpHeight;//修改文件大小  
  47.     bfhead.bfReserved1=0;  
  48.     bfhead.bfReserved2=0;  
  49.     bfhead.bfOffBits=14+40+256*sizeof(RGBQUAD);//修改偏移字节数  
  50.     fwrite(&bfhead,14,1,fp1);    //将修改后的文件头存入fp1;  
  51.   
  52.     //修改信息头,其中有两项需要修改,1个位biBitCount:真彩图为24 ,应改成8;另一个是biSizeImage:由于每像素所占位数的变化,所以位图数据的大小发生变化  
  53.     BITMAPINFOHEADER head1;  
  54.     head1.biBitCount=8;    //将每像素的位数改为8  
  55.     head1.biClrImportant=0;  
  56.     head1.biCompression=0;  
  57.     head1.biClrUsed=0;  
  58.     head1.biHeight=bmpHeight;  
  59.     head1.biWidth=bmpWidth;  
  60.     head1.biPlanes=1;  
  61.     head1.biSize=40;  
  62.     head1.biSizeImage=LineByte1*bmpHeight;//修改位图数据的大小  
  63.     head1.biXPelsPerMeter=0;  
  64.     head1.biYPelsPerMeter=0;  
  65.     fwrite(&head1,40,1,fp1);  //将修改后的信息头存入fp1;  
  66.   
  67.     if(biBitCount==24)//判断是否是真彩图,若是,由于真彩图没有颜色表,所以在变为灰度图后要加上颜色表  
  68.     {  
  69.         pColorTable=new RGBQUAD[256];  
  70.         for (int  i = 0; i < 256; i++ )  
  71.             pColorTable[i].rgbRed = pColorTable[i].rgbGreen = pColorTable[i].rgbBlue = i; //是颜色表里的B、G、R分量都相等,且等于索引值  
  72.   
  73.     }  
  74.     fwrite(pColorTable,sizeof(RGBQUAD),256,fp1); //将颜色表写入fp1;  
  75.   
  76.     //写位图数据  
  77.     unsigned char *pBmpBuf1;  
  78.     pBmpBuf1= new unsigned char[LineByte1*bmpHeight];  
  79.     for(int i=0;i<bmpHeight;i++)  
  80.         for(int j=0;j<bmpWidth;j++)  
  81.         {  
  82.             unsigned char *pb1,*pb2;  
  83.             pb1=pBmpBuf+i*LineByte+j*3;  
  84.             int y=*(pb1)*0.299+*(pb1+1)*0.587+*(pb1+2)*0.114;   //将每一个像素都按公式y=B*0.299+G*0.587+R*0.114进行转化  
  85.             pb2=pBmpBuf1+i*LineByte1+j;  
  86.             *pb2=y;  
  87.   
  88.         }  
  89.     if(biBitCount==8)  
  90.         fwrite(pBmpBuf,LineByte*bmpHeight,1,fp1);  
  91.     else  
  92.         fwrite(pBmpBuf1,LineByte1*bmpHeight,1,fp1);  
  93.   
  94.   
  95.     fclose(fp1);  
  96.   
  97.     system("pause");  
  98.     return 0;  
  99. }  
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值