您当前的位置:首页 > 互联网教程

c语言,怎样读取一个BMP图片

发布时间:2025-05-12 04:26:05    发布人:远客网络

c语言,怎样读取一个BMP图片

一、c语言,怎样读取一个BMP图片

void image_channel();//抽取RGB通道

void image_bright();//改变图像亮度

unsigned short bfType;//文件标识 2字节必须为BM

unsigned int bfSize;//文件大小 4字节

unsigned short bfReserved1;//保留,每字节以"00"填写 2字节

unsigned short bfReserved2;//同上 2字节

unsigned int bfOffBits;//记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节

unsigned int biSize;//表示本结构的大小 4字节

int biWidth;//位图的宽度 4字节

int biHeight;//位图的高度 4字节

unsigned short biPlanes;//永远为1, 2字节

unsigned short biBitCount;//位图的位数分为1 4 8 16 24 32 2字节

unsigned int biCompression;//压缩说明 4字节

unsigned int biSizeImage;//表示位图数据区域的大小以字节为单位 4字节

int biXPelsPerMeter;//用象素/米表示的水平分辨率 4字节

int biYPelsPerMeter;//用象素/米表示的垂直分辨率 4字节

unsigned int biClrUsed;//位图使用的颜色索引数 4字节

unsigned int biClrImportant;//对图象显示有重要影响的颜色索引的数目 4字节

extern unsigned char*imagedata;

int times=3;//输入文件名次数。

printf("\nplease enter a file name for reading:");

printf("\nplease enter a file name for reading again:");

//printf("\n%s",bmp_name);

file=fopen(bmp_name,"rb+");//打开一个文件进行读写操作。

printf("\nerror opening%s for reading!",bmp_name);

printf("\nsorry, shutdown!");

fseek(file,0L,0);//读取图像文件类型

fread(&bmp,sizeof(BMP),1,file);

printf("\n bmp tpye:%u",bmp.bfType);

printf("\n bmp size:%u",bmp.bfSize);

printf("\n bmp reserved1:%u",bmp.bfReserved1);

printf("\n bmp reserved2:%u",bmp.bfReserved2);

printf("\n bmp offBits:%u",bmp.bfOffBits);

printf("\n bmp bisize:%u",bmp.biSize);

printf("\n bmp biWidth:%d",bmp.biWidth);

printf("\n bmp biHeight:%d",bmp.biHeight);

printf("\n bmp biplans:%u",bmp.biPlanes);

printf("\n bmp biBitCount:%u",bmp.biBitCount);

printf("\n bmp biCompression:%u",bmp.biCompression);

printf("\n bmp biSizeImage:%u",bmp.biSizeImage);

printf("\n bmp biXPelsPerMeter:%d",bmp.biXPelsPerMeter);

printf("\n bmp biYPelsPerMeter:%d",bmp.biYPelsPerMeter);

printf("\n bmp biClrUsed:%u",bmp.biClrUsed);

printf("\n bmp biClrImportant:%u\n",bmp.biClrImportant);

line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4;//获得图像数据每行的数据个数

//printf("dfsa%u",bmp.line_byte);

imagedata=(unsigned char*)malloc(bmp.biSizeImage);

fseek(file,(long)bmp.bfOffBits,0);

fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

int times=3;//输入文件名次数。

printf("\nplease enter a file name for writeing:");

printf("\nplease enter a file name for writeing again:");

file=fopen(bmp_name,"wb+");//打开一个文件进行读写操作。

printf("\nerror opening%s for writing",bmp_name);

printf("\nsorry, shutdown!");

fseek(file,0L,0);//图像文件类型

fwrite(&(bmp.bfType),sizeof(short),1,file);

printf("\n bmp tpye:%d",bmp.bfType);

fseek(file,2L,0);//图像文件大小

fwrite(&(bmp.bfSize),sizeof(int),1,file);

printf("\n bmp size:%d",bmp.bfSize);

fseek(file,6L,0);//图像文件保留字1

fwrite(&(bmp.bfReserved1),sizeof(short),1,file);

printf("\n bmp reserved1:%d",bmp.bfReserved1);

fseek(file,8L,0);//图像文件保留字2

fwrite(&(bmp.bfReserved2),sizeof(short),1,file);

printf("\n bmp reserved2:%d",bmp.bfReserved2);

fseek(file,10L,0);//数据区的偏移量

fwrite(&(bmp.bfOffBits),sizeof(short),1,file);

printf("\n bmp offBits:%d",bmp.bfOffBits);

fseek(file,14L,0);//文件头结构大小

fwrite(&(bmp.biSize),sizeof(int),1,file);

printf("\n bmp bisize:%d",bmp.biSize);

fseek(file,18L,0);//图像的宽度

fwrite(&(bmp.biWidth),sizeof(int),1,file);

printf("\n bmp biWidth:%d",bmp.biWidth);

fseek(file,22L,0);//图像的高度

fwrite(&(bmp.biHeight),sizeof(int),1,file);

printf("\n bmp biHeight:%d",bmp.biHeight);

fseek(file,24L,0);//图像的面数

fwrite(&(bmp.biPlanes),sizeof(short),1,file);

printf("\n bmp biplans:%d",bmp.biPlanes);

fseek(file,28L,0);//图像一个像素的字节数

fwrite(&(bmp.biBitCount),sizeof(short),1,file);

printf("\n bmp biBitCount:%d",bmp.biBitCount);

fseek(file,30L,0);//图像压缩信息

fwrite(&(bmp.biCompression),sizeof(short),1,file);

printf("\n bmp biCompression:%d",bmp.biCompression);

fseek(file,34L,0);//图像数据区的大小

fwrite(&(bmp.biSizeImage),sizeof(int),1,file);

printf("\n bmp biSizeImage:%d",bmp.biSizeImage);

fseek(file,38L,0);//水平分辨率

fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file);

printf("\n bmp biXPelsPerMeter:%d",bmp.biXPelsPerMeter);

fseek(file,42L,0);//垂直分辨率

fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file);

printf("\n bmp biYPelsPerMeter:%d",bmp.biYPelsPerMeter);

fseek(file,46L,0);//颜色索引数

fwrite(&(bmp.biClrUsed),sizeof(int),1,file);

printf("\n bmp biClrUsed:%d",bmp.biClrUsed);

fseek(file,50L,0);//重要颜色索引数

fwrite(&(bmp.biClrImportant),sizeof(int),1,file);

printf("\n bmp biClrImportant:%d\n",bmp.biClrImportant);

fseek(file,(long)(bmp.bfOffBits),0);

fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

for(i=0;i<bmp.biHeight;i++)

for(j=0;j<line_byte/3;j++)

tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));

imagedata[i*line_byte+j*3+0]=tmp;

imagedata[i*line_byte+j*3+1]=tmp;

imagedata[i*line_byte+j*3+2]=tmp;

//printf("\nnidsfh%d%d",i,j);

for(i=0;i<bmp.biHeight;i++)

if((*(imagedata+i*line_byte+j))<128)

imagedata[i*line_byte+j]=255;

for(i=0;i<bmp.biHeight;i++)

imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);

void image_channel()//抽取RGB通道

printf("\nplease enter a char(r/g/b):");

for(i=0;i<bmp.biHeight;i++)

for(j=0;j<line_byte/3;j++)

imagedata[i*line_byte+3*j+1]=0;

imagedata[i*line_byte+3*j+2]=0;

for(i=0;i<bmp.biHeight;i++)

for(j=0;j<line_byte/3;j++)

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+2]=0;

for(i=0;i<bmp.biHeight;i++)

for(j=0;j<line_byte/3;j++)

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+1]=0;

void image_bright()//改变图像亮度

printf("\n please enter the level of brightness[-255 to 255]:");

for(i=0;i<bmp.biHeight;i++)

if((imagedata[i*line_byte+j]+level)>255)

imagedata[i*line_byte+j]=255;

imagedata[i*line_byte+j]+=level;

if((imagedata[i*line_byte+j]-abs(level))<0)

imagedata[i*line_byte+j]+=level;

//void image_create()//创建一幅24位BMP图像文件。

image_info(file);//imagedata已经分配了动态内存,但是没有释放

printf("\n 1.image_opposite");

printf("\n 2.image_gray");

printf("\n 3.image_binarization");

printf("\n 4.image_channel");

printf("\n 5.image_brightness");

printf("\nchoose your options:");

printf("\n wrong choose!");

printf("\nlet's go on?(y/n):");

二、C语言 读取黑白BMP图

黑白BMP文件图的像素大多是从62字节(从0数起)开始。具体从哪一字节开始,

决定于文件中第10字节(从0数起)开始处4字节的整形数是多少.

因为文件头后面不是紧接着像素值.

黑白BMP图的一个像素用一个bit表示。一般0表示背景(缺省为白色),1表示前景色(缺省为黑色)。即一个字节代表相邻的8个像素。

320*240的黑白BMP图,每行320个像素用40个字节表示,240行,应占40*240=9600字节。加上前头62字节,文件大小应为9662字节。

BMP图像素的存储顺序是自底向上。所以文件最后40个字节表示的是第一行的像素值。

行宽如不是8的倍数,也扩展到8的倍数。如行宽321,

则每行像素需用41个字节存储。你的行宽320,恰好是8的倍数,没有这个问题。

三、如何用C语言程序从bmp格式的图片中读取图片的灰度值

1、fp=fopen("Lena.raw","rb");//打开文件。注意raw格式图像要以只读二进制流的形式打开

2、unsignedchar*pData=newunsignedchar[256*256];//注意:raw图像用无符号char型读入

3、fread(pData,sizeof(unsignedchar),(256*256),fp);//fread具体用法见msdn

4、intvalue[256]={0};//声明并初始化存灰度值的数组

5、for(i=0;i<(256*256);i++)//统计灰度值

6、fprintf(fp,"灰度值\t\t大小\r\n");//注意\r\n否则不能换行!

7、for(j=0;j<256;j++)//将结果输出到txt中

8、printf("%d\t\t%d\n",j,value[j]);

9、//fwrite(value,sizeof(int),256,fp);

10、fprintf(fp,"%d\t\t%d\r\n",j,value[j]);//注意\r\n否则不能换行

11、intfread(unsignedchar*pData)//把声明的那个数组空间指针作为参数来回传

12、fread(pData,sizeof(unsignedchar),(256*256),fp);

13、for(inti=0;i<(256*256);i++)//统计灰度值

14、fprintf(fp,"灰度值\t\t大小\r\n");

15、for(intj=0;j<256;j++)//将结果输出到txt中

16、printf("%d\t\t%d\n",j,value[j]);

17、//fwrite(value,sizeof(int),256,fp);

18、fprintf(fp,"%d\t\t%d\r\n",j,value[j]);

19、pData=newunsignedchar[256*256];//在主函数里声明新空间