C语言实现将彩色bmp图像转化为灰图、灰度图像反色
程序员文章站
2022-03-13 20:51:36
本文实例为大家分享了c语言实现将彩色bmp图像转化为灰图、灰度图像反色的具体代码,供大家参考,具体内容如下彩色图像转灰度图像彩色(24位)bmp图像结构:typedef struct{ bitmapf...
本文实例为大家分享了c语言实现将彩色bmp图像转化为灰图、灰度图像反色的具体代码,供大家参考,具体内容如下
彩色图像转灰度图像
彩色(24位)bmp图像结构:
typedef struct{ bitmapfileheader bfheader; bitmapinfoheader biheader; unsigned char *imgdata; }bmp;
灰度(8位)bmp图像结构:
typedef struct{ bitmapfileheader bfheader; bitmapinfoheader biheader; rgbquad palette[256]; unsigned char *imgdata; }bmp;
bmp灰度图像比彩色图像多了一个调色板,调色板中每个元素的类型是一个rgbquad结构,占四个字节,其定义如下:
typedef struct{ unsigned char rgbblue; unsigned char rgbgreen; unsigned char rgbred; unsigned char rgbreserved; }rgbquad;
在实际的bmp图像中调色板:
代码实现将彩色bmp图像转化位灰值图像:
#include<stdio.h> #include<stdlib.h> #include<string.h> #pragma pack(1) //全紧凑模式 typedef struct { unsigned char bftype[2]; unsigned int bfsize; unsigned short bfreserved1; unsigned short bfreserved2; unsigned int bfoffbits; }bitmapfileheader; typedef struct { unsigned int bisize; unsigned int biwidth; unsigned int biheight; unsigned short biplanes; unsigned short bibitcount; unsigned int bicompression; unsigned int bisizeimage; unsigned int bixpixpermeter; unsigned int biypixpermeter; unsigned int biclrused; unsigned int biclrimportant; }bitmapinfoheader; typedef struct{ unsigned char rgbblue; unsigned char rgbgreen; unsigned char rgbred; unsigned char rgbreserved; }rgbquad; typedef struct{ bitmapfileheader bfheader; bitmapinfoheader biheader; rgbquad palette[256]; unsigned char *imgdata; }bmp; int main(){ file *fp; if((fp=fopen("d:\temp\\test.bmp","rb"))==null){ perror("can not open file!"); return -1; } //读入彩色bmp图像文件头,信息头和图像数据 bitmapfileheader bfheader; fread(&bfheader,14,1,fp); bitmapinfoheader biheader; fread(&biheader,40,1,fp); int imsize=biheader.bisizeimage; int width=biheader.biwidth; int height=biheader.biheight; int bitcount=biheader.bibitcount; int linebytes=(width*bitcount+31)/32*4; fseek(fp,bfheader.bfoffbits,seek_set); unsigned char*imagedata=(unsigned char*)malloc(imsize*sizeof(unsigned char)); fread(imagedata,imsize*sizeof(unsigned char),1,fp); fclose(fp); bmp b; int i,j,k; memcpy(&(b.bfheader),&bfheader,sizeof(bfheader)); memcpy(&(b.biheader),&biheader,sizeof(biheader)); b.bfheader.bfoffbits=1078; //因新增了调色板,需调整图像数据偏移位置 b.biheader.bibitcount=8; //改变图像位数 int linebytes_new=(width*8+31)/32*4; //重新计算每行数据字节 b.biheader.bisizeimage=linebytes_new*height; //改变图像数据大小 b.bfheader.bfsize=1078+b.biheader.bisizeimage; //改变文件数据大小 b.imgdata=(unsigned char*)malloc(sizeof(unsigned char)*b.biheader.bisizeimage); memset(b.imgdata,0,sizeof(unsigned char)*b.biheader.bisizeimage); for(i=0;i<256;i++){ b.palette[i].rgbblue=i; b.palette[i].rgbgreen=i; b.palette[i].rgbred=i; } for(i=0;i<height;i++){ for(j=0;j<width;j++){ //将每一个像素都按公式y=b*0.299+g*0.587+r*0.114进行转化 b.imgdata[linebytes_new*i+j]=imagedata[linebytes*i+j*3]*0.299+imagedata[linebytes*i+j*3+1]*0.587+imagedata[linebytes*i+j*3+2]*0.114; } } char savepath[]="d:\temp\\save_test.bmp"; file *f=fopen(savepath,"wb"); if(f==null){ perror("can not open file!"); return -2; } fwrite(&b.bfheader,sizeof(bitmapfileheader),1,f); fwrite(&b.biheader,sizeof(bitmapinfoheader),1,f); fwrite(&b.palette,1024,1,f); fwrite(b.imgdata,sizeof(unsigned char)*b.biheader.bisizeimage,1,f); fclose(f); free(imagedata); free(b.imgdata); return 0; }
代码效果:
灰度图像反色
int i,j; for(i=0;i<height;i++){ for(j=0;j<width;j++){ b.imgdata[linebytes*i+j]=255-imagedata[linebytes*i+j]; } }
代码效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。