最新用c语言进行数字图像处理_第1页
最新用c语言进行数字图像处理_第2页
最新用c语言进行数字图像处理_第3页
最新用c语言进行数字图像处理_第4页
最新用c语言进行数字图像处理_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、用c语言进行数字图像处理其实,数字图像处理有几步呢? 一共三步。第一步,读入图片。第二步,处理图片。第三步,保存图片。而第二步主要涉及的是处理图像的算法,所以,我在这里就不多说了。 而第一步和第三步是为第二步做位图文件结构的声明:BMP.h#ifndef BMP_H_INCLUDED#defi ne BMP_H_INCLUDED typedef un sig ned short WORD;typedef un sig ned long DWORD;typedef long LONG;typedef un sig ned char BYTE;typedef struct tagBITMAPFIL

2、EHEADER / bmfhWORDbfType;DWORDbfSize;WORDbfReserved1;WORDbfReserved2;DWORDbfOffBits;BITMAPFILEHEADER; / bmihDWORDLONGLONGWORDWORDDWORDDWORDLONGLONGDWORDDWORDtypedef struct tagBITMAPINFOHEADERbiSize;biWidth; biHeight;biPla nes; biBitCou nt; biCompressi on; biSizeImage;biXPelsPerMeter; biYPelsPerMeter

3、; biClrUsed; biClrImporta nt;BITMAPINFOHEADER; typedef struct tagRGBQUAD / rgbqBYTErgbBlue;BYTErgbGree n;BYTErgbRed;BYTErgbReserved;RGBQUAD;typedef struct tagBITMAPINFOBITMAPINFOHEADER bmiHeader;RGBQUAD bmiColors1;BITMAPINFO;#en dif / BMP_H_INCLUDED主程序:mai n.c#i nclude #i nclude #in clude #i nclude

4、#in clude #i nclude #i nclude BMP.hBITMAPFILEHEADER bmfh;BITMAPINFOHEADER bmih;BYTE *imgData;bool bReadBMFH=false;bool bReadBMIH=false;bool bReadPixel=false;检查路径是否合法:文件能打开;以 bmp为后缀名int CheckFilePath(char *filepath);/读入位图的文件头int ReadFileHeader(char *filepath,BITMAPFILEHEADER *bmfh);/打印位图的文件头void Prin

5、 tFileHeader(BITMAPFILEHEADER *bmfh);/读入位图的信息头int ReadI nfoHeader(char *filepath,BITMAPINFOHEADER *bmih);/打印位图的信息头void Printin foHeader(BITMAPINFOHEADER *bmih);/创建8位位图的调色板int CreatePalette(RGBQUAD pal);/读入位图的像素数据int ReadPixelData(char *filepath,BYTE *imgData);/计算每行像素所占的字节数LONG GetLi neBytes(i nt img

6、Width,i nt bitCou nt);/打印位图的像素数据void Prin tPixelData(BYTE *imgData,i nt width,i nt height,i nt bitCou nt);/打印菜单选项void Prin tMe nu();/另存为位图int SaveAslmage(char *filepath);/显示位图void Showlmage(char * filepath);/保存文件头int SaveFileHeader(FILE* fp);/保存信息头int SaveI nfoHeader(FILE* fp);/保存调色板int SaveColorPal

7、ette(FILE *fp);/保存像素数据int SavePixelData(FILE* fp);int mai n()char filepath256;char saveasfilepath256;int i;int width;int height;int bitCou nt;DWORD dwLi neBytes;int select;int q=0;system(echo off);system(color 2);printf(TIMimagen);prin tf(I nput the path of the BMP file: n);gets(filepath);i=CheckFil

8、ePath(filepath);if(i=-1)return -1;doPrin tMe nu();sea nf(% u,& select);switch(select)case 0:prin tf(I nput the path of the BMP file:n);scan f(%s,filepath);CheckFilePath(filepath);break;case 1:i=ReadFileHeader(filepath,& bmfh);if(i!=-1)prin tf(Read the file header successfully.n); bReadBMFH=true;brea

9、k;elseprin tf(Read the file header failed.n); bReadBMFH=false;q=1;break;case 2:i=ReadI nfoHeader(filepath,&bmih);if(i!=-1)prin tf(Read the info header successfully.n); bReadBMIH=true;break;elseprin tf(Read the info header failed.n); bReadBMIH=false;q=1;break;case 3:if(!bReadBMIH)prin tf(Please read

10、the info header at first.n); break;height=bmih.biHeight;width=bmih.biWidth;bitCou nt=bmih.biBitCou nt;dwLi neBytes=GetLi neBytes(width,bitCou nt);imgData=(BYTE*)malloc(dwL in eBytes*height*sizeof(BYTE); if(!imgData)prin tf(Ca n not allocate memory for the image.n);q=1;break;i=ReadPixelData(filepath,

11、imgData);if(i=-1)prin tf(Read the pixel data failed.n); bReadPixel=false;q=1; break;elseprin tf(Read the pixel data successfully. n); bReadPixel=true;break;case 4:if(bReadBMFH)Prin tFileHeader(&bmfh); break;elseprin tf(Please read the file header at first. n);break;case 5:if(bReadBMIH)Printin foHead

12、er(&bmih); break;elseprin tf(Please read the info header at first.n); break;case 6:if(bReadPixel)Prin tPixelData(imgData,width,height,bitCou nt); break;elseprin tf(Please read the pixel data at first.n);break;case 7:Showimage(filepath);break;case 8:prin tf(I nput the path(ex. d:/po on .bmp) you want

13、 to save:n); sca nf(%s,saveasfilepath);i=SaveAslmage(saveasfilepath);if(i=-1)prin tf(Error: failed to save the image.n); break;break;default:q=1;break;select=9527; while (q=0);return 0;int ReadFileHeader(char *filepath,BITMAPFILEHEADER *bmfh)FILE *fp;fp=fope n(filepath,rb);if(!fp)prin tf(Can not ope

14、 n the file:%sn,filepath);return -1;if(fread(&bmfh-bfType,sizeof(WORD),1,fp)!=1)prin tf(Ca n not read bfType in the file header.n); fclose(fp);return -1;if(fread(&bmfh-bfSize,sizeof(DWORD),1,fp)!=1)prin tf(Ca n not read bfSize in the file header.n); fclose(fp);return -1;if(fread(&bmfh-bfReserved1,si

15、zeof(WORD),1,fp)!=1)prin tf(Ca n n ot read bfReserved1 in the file header.n); fclose(fp);return -1;if(fread(&bmfh-bfReserved2,sizeof(WORD),1,fp)!=1)prin tf(Ca n n ot read bfReserved2 in the file header.n); fclose(fp);return -1;if(fread(&bmfh-bfOffBits,sizeof(DWORD),1,fp)!=1)prin tf(Can n ot read bfO

16、ffBits in the file header.n); fclose(fp);return -1;fclose(fp);return 0;int ReadI nfoHeader(char *filepath,BITMAPINFOHEADER *bmih) FILE *fp;fp=fope n(filepath,rb);if(!fp)prin tf(Can not ope n the file:%sn,filepath); return -1;fseek(fp,14,SEEK_SET);if(fread(&bmih-biSize,sizeof(DWORD),1,fp)!=1)prin tf(

17、Ca n not read biSize in the info header.n); fclose(fp);return -1;if(fread(&bmih-biWidth,sizeof(LONG),1,fp)!=1)prin tf(Ca n not read biWidth in the info header.n); fclose(fp);return -1;if(fread(&bmih-biHeight,sizeof(LONG),1,fp)!=1)prin tf(Ca n not read biHeight in the info header.n); fclose(fp);retur

18、n -1;if(fread(&bmih-biPla nes,sizeof(WORD),1,fp)!=1)prin tf(Ca n not read biPla nes in the info header.n); fclose(fp);return -1;if(fread(&bmih-biBitCou nt,sizeof(WORD),1,fp)!=1)prin tf(Ca n not read biBitCo unt in the info header.n); fclose(fp);return -1;if(fread(&bmih-biCompressio n,sizeof(DWORD),1

19、,fp)!=1)prin tf(Ca n not read biCompressi on in the info header.n); fclose(fp);return -1;if(fread(&bmih-biSizelmage,sizeof(DWORD),1,fp)!=1)prin tf(Ca n not read biSizeImage in the info header.n); fclose(fp);return -1;if(fread(&bmih-biXPelsPerMeter,sizeof(LONG),1,fp)!=1)prin tf(Can n ot read biXPelsP

20、erMeter in the info header.n); fclose(fp);return -1;if(fread(&bmih-biYPelsPerMeter,sizeof(LONG),1,fp)!=1)prin tf(Can n ot read biYPelsPerMeter in the info header.n); fclose(fp);return -1;if(fread(&bmih-biClrUsed,sizeof(DWORD),1,fp)!=1)prin tf(Ca n not read biClrUsed in the info header.n); fclose(fp)

21、;return -1;if(fread(&bmih-biClrlmporta nt,sizeof(DWORD),1,fp)!=1)prin tf(Ca n not read biClrImporta nt in the info header.n); fclose(fp);return -1;fclose(fp);return 0;int CreatePalette(RGBQUAD pal)int i;if(sizeof(pal)/sizeof(RGBQUAD)!=256)prin tf(The size of the palette must be 256.n); return -1;for

22、(i=0;ibfOffBits);prin tf(bfReserved1: %ldn,bmfh-bfReserved1);prin tf(bfReserved2: %ldn,bmfh-bfReserved2);prin tf(bfSize: %ldn,bmfh-bfSize);prin tf(bfType: %ldn,bmfh-bfType);void Printin foHeader(BITMAPINFOHEADER *bmih)prin tf(The content in the info header of the BMP file: n);prin tf(biBitCou nt: %l

23、dn,bmih-biBitCou nt);prin tf(biClrImporta nt: %ldn,bmih-biClrImporta nt);prin tf(biClrUsed: %ldn,bmih-biClrUsed);prin tf(biCompressi on: %ldn,bmih-biCompressi on);prin tf(biHeight: %ldn,bmih-biHeight);prin tf(biPla nes: %ldn ,bmih-biPla nes);prin tf(biSize: %ldn,bmih-biSize);prin tf(biSizelmage: %ld

24、n,bmih-biSizelmage);prin tf(biWidth: %ldn,bmih-biWidth);prin tf(biXPelsPerMeter: %ldn,bmih-biXPelsPerMeter);prin tf(biYPelsPerMeter: %ldn,bmih-biYPelsPerMeter);LONG GetLi neBytes(i nt imgWidth,i nt bitCou nt)return (imgWidth*bitCo un t+31)/32*4;void Prin tPixelData(BYTE *imgData,i nt width,i nt heig

25、ht, int bitCou nt)int i;int j ;in t p;DWORD dwLi neBytes=GetLi neBytes(width,bitCou nt);if(bitCou nt=8)for(i=0;iheight;i+)for(j=0;jwidth;j+)p=*(imgData+dwLi neBytes*(height-1-i)+j); prin tf(%d,p);prin tf(n);else if(bitCou nt=24)for(i=0;iheight;i+)for(j=0;jwidth*3;j+)prin tf();p=*(imgData+dwLi neByte

26、s*(height-1-i)+j); prin tf(%d,p);j+ ;p=*(imgData+dwLi neBytes*(height-1-i)+j); prin tf(%d,p);j+ ;p=*(imgData+dwL in eBytes*(height-1-i)+j); prin tf(%d) ,p);prin tf(n);elseprin tf(O nly supported: 8 or 24 bits.n);int CheckFilePath(char *filepath)FILE *fp;int len=strle n(filepath)/sizeof(char);char ex

27、t3;if(filepathO!=i nt()strncpy(ext, &filepathle n-3,3);if(!(extO=b & ext1=m & ext2=p)prin tf(Error: The file is n ot a BMP file.n);prin tf(Error: The exte nti on of the file name must be bmp, no t BMP n);return -1;fp=fope n( filepath,r);if(!fp)prin tf(Error: The path is not correct.n);return -1;fclo

28、se(fp);elseprin tf(Error: The path must not in clude bla nk space.n);return -1;return 0;void Prin tMe nu()printf( -Choose Your Operation-n);prin tf( | 0-I nput the image path|n);prin tf( | 1-Read the file header|n);prin tf( | 2-Read the info header|n);prin tf( | 3-Read the pixel data|n);prin tf( | 4

29、-Print the file header|n);prin tf( | 5-Print the info header|n);prin tf( | 6-Pri nt the pixel data|n);prin tf( | 7-View the origi nal image|n);prin tf( | 8-Save as the image|n)prin tf( | otherExit the program|n);printf( -n ”);int SaveAslmage(char *filepath)FILE *fp;fp=fope n( filepath,wb);if(!fp)pri

30、n tf(Error: can not create the file.n); return -1;SaveFileHeader(fp);SaveI nfoHeader(fp); if(bmih.biBitCou nt=8)SaveColorPalette(fp);SavePixelData(fp);fclose(fp);prin tf(Save As the image successfully. n); return 0;void ShowImage(char * filepath)char cmd266;strcpy(cmd,start );strcat(cmd,filepath);pr

31、in tf(%sn,cmd); system(cmd);int SaveFileHeader(FILE *fp)if(!bReadBMFH)prin tf(Please read the file header at first. n); return -1;if(fwrite(&bmfh.bfType,sizeof(WORD),1,fp)!=1)prin tf(Ca n not write bfType in the file header. n); fclose(fp);return -1;if(fwrite(&bmfh.bfSize,sizeof(DWORD),1,fp)!=1)prin

32、 tf(Ca n not write bfSize in the file header. n); fclose(fp);return -1;if(fWrite(&bmfh.bfReserved1,sizeof(WORD),1,fp)!=1)prin tf(Ca n n ot write bfReservedl in the file header. n); fclose(fp);return -1;if(fwrite(&bmfh.bfReserved2,sizeof(WORD),1,fp)!=1)prin tf(Ca n not write bfReserved2 in the file h

33、eader. n); fclose(fp);return -1;if(fwrite(&bmfh.bfOffBits,sizeof(DWORD),1,fp)!=1)prin tf(Can n ot write bfOffBits in the file header.n); fclose(fp);return -1;return 0;int Savel nfoHeader(FILE *fp)if(!bReadBMIH)prin tf(Please read the info header at first. n); return -1;if(fwrite(&bmih.biSize,sizeof(

34、DWORD),1,fp)!=1)prin tf(Ca n not write biSize in the info header. n); fclose(fp);return -1;if(fwrite(&bmih.biWidth,sizeof(LONG),1,fp)!=1)prin tf(Ca n not write biWidth in the info header. n); fclose(fp);return -1;if(fwrite(&bmih.biHeight,sizeof(LONG),1,fp)!=1)prin tf(Ca n not write biHeight i n the

35、info header. n); fclose(fp);return -1;if(fWrite(&bmih.biPla nes,sizeof(WORD),1,fp)!=1)prin tf(Ca n not write biPla nes in the info header. n); fclose(fp);return -1;if(fwrite(&bmih.biBitCou nt,sizeof(WORD),1,fp)!=1)prin tf(Ca n not write biBitCo unt in the info header. n); fclose(fp);return -1;if(fwr

36、ite(&bmih.biCompressio n,sizeof(DWORD),1,fp)!=1)prin tf(Ca n not write biCompressi on in the info header. n); fclose(fp);return -1;if(fwrite(&bmih.biSizelmage,sizeof(DWORD),1,fp)!=1)prin tf(Ca n not write biSizelmage in the info header. n); fclose(fp);return -1;if(fwrite(&bmih.biXPelsPerMeter,sizeof(LONG),1,fp)!=1)prin tf(Can n ot writ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论