用-c-语言进行数字图像处理.doc_第1页
用-c-语言进行数字图像处理.doc_第2页
用-c-语言进行数字图像处理.doc_第3页
用-c-语言进行数字图像处理.doc_第4页
用-c-语言进行数字图像处理.doc_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

用 c 语言进行数字图像处理 其实,数字图像处理有几步呢?一共三步。第一步,读入图片。第二步,处理图片。第三步,保存图片 。 而第二步主要涉及的是处理图像的算法,所以,我在这里就不多说了。而第一步和第三步是为第二步做 位图文件结构的声明:BMP.h #ifndef BMP_H_INCLUDED #define BMP_H_INCLUDED typedef unsigned short WORD; typedef unsigned long DWORD; typedef long LONG; typedef unsigned char BYTE; typedef struct tagBITMAPFILEHEADER / bmfh WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER / bmih DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; BITMAPINFOHEADER; typedef struct tagRGBQUAD / rgbq BYTE rgbBlue; BYTE rgbGreen; BYTE rgbRed; BYTE rgbReserved; RGBQUAD; typedef struct tagBITMAPINFO BITMAPINFOHEADER bmiHeader; RGBQUAD bmiColors1; BITMAPINFO; #endif / BMP_H_INCLUDED 主程序:main.c #include #include #include #include #include #include #include BMP.h BITMAPFILEHEADER 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 PrintFileHeader(BITMAPFILEHEADER *bmfh); /读入位图的信息头 int ReadInfoHeader(char *filepath,BITMAPINFOHEADER *bmih); /打印位图的信息头 void PrintInfoHeader(BITMAPINFOHEADER *bmih); /创建 8 位位图的调色板 int CreatePalette(RGBQUAD pal); /读入位图的像素数据 int ReadPixelData(char *filepath,BYTE *imgData); /计算每行像素所占的字节数 LONG GetLineBytes(int imgWidth,int bitCount); /打印位图的像素数据 void PrintPixelData(BYTE *imgData,int width,int height,int bitCount); /打印菜单选项 void PrintMenu(); /另存为位图 int SaveAsImage(char *filepath); /显示位图 void ShowImage(char * filepath); /保存文件头 int SaveFileHeader(FILE* fp); /保存信息头 int SaveInfoHeader(FILE* fp); /保存调色板 int SaveColorPalette(FILE *fp); /保存像素数据 int SavePixelData(FILE* fp); int main() char filepath256; char saveasfilepath256; int i; int width; int height; int bitCount; DWORD dwLineBytes; int select; int q=0; system(echo off); system(color 2); printf(-TIMimage-n); printf(Input the path of the BMP file:n); gets(filepath); i=CheckFilePath(filepath); if(i=-1) return -1; do PrintMenu(); scanf(%u,&select); switch(select) case 0: printf(Input the path of the BMP file:n); scanf(%s,filepath); CheckFilePath(filepath); break; case 1: i=ReadFileHeader(filepath,&bmfh); if(i!=-1) printf(Read the file header successfully.n); bReadBMFH=true; break; else printf(Read the file header failed.n); bReadBMFH=false; q=1; break; case 2: i=ReadInfoHeader(filepath,&bmih); if(i!=-1) printf(Read the info header successfully.n); bReadBMIH=true; break; else printf(Read the info header failed.n); bReadBMIH=false; q=1; break; case 3: if(!bReadBMIH) printf(Please read the info header at first.n); break; height=bmih.biHeight; width=bmih.biWidth; bitCount=bmih.biBitCount; dwLineBytes=GetLineBytes(width,bitCount); imgData=(BYTE*)malloc(dwLineBytes*height*sizeof(BYTE); if(!imgData) printf(Can not allocate memory for the image.n); q=1; break; i=ReadPixelData(filepath,imgData); if(i=-1) printf(Read the pixel data failed.n); bReadPixel=false; q=1; break; else printf(Read the pixel data successfully.n); bReadPixel=true; break; case 4: if(bReadBMFH) PrintFileHeader(&bmfh); break; else printf(Please read the file header at first.n); break; case 5: if(bReadBMIH) PrintInfoHeader(&bmih); break; else printf(Please read the info header at first.n); break; case 6: if(bReadPixel) PrintPixelData(imgData,width,height,bitCount); break; else printf(Please read the pixel data at first.n); break; case 7: ShowImage(filepath); break; case 8: printf(Input the path(ex. d:/poon.bmp) you want to save:n); scanf(%s,saveasfilepath); i=SaveAsImage(saveasfilepath); if(i=-1) printf(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=fopen(filepath,rb); if(!fp) printf(Can not open the file:%sn,filepath); return -1; if(fread(&bmfh-bfType,sizeof(WORD),1,fp)!=1) printf(Can not read bfType in the file header.n); fclose(fp); return -1; if(fread(&bmfh-bfSize,sizeof(DWORD),1,fp)!=1) printf(Can not read bfSize in the file header.n); fclose(fp); return -1; if(fread(&bmfh-bfReserved1,sizeof(WORD),1,fp)!=1) printf(Can not read bfReserved1 in the file header.n); fclose(fp); return -1; if(fread(&bmfh-bfReserved2,sizeof(WORD),1,fp)!=1) printf(Can not read bfReserved2 in the file header.n); fclose(fp); return -1; if(fread(&bmfh-bfOffBits,sizeof(DWORD),1,fp)!=1) printf(Can not read bfOffBits in the file header.n); fclose(fp); return -1; fclose(fp); return 0; int ReadInfoHeader(char *filepath,BITMAPINFOHEADER *bmih) FILE *fp; fp=fopen(filepath,rb); if(!fp) printf(Can not open the file:%sn,filepath); return -1; fseek(fp,14,SEEK_SET); if(fread(&bmih-biSize,sizeof(DWORD),1,fp)!=1) printf(Can not read biSize in the info header.n); fclose(fp); return -1; if(fread(&bmih-biWidth,sizeof(LONG),1,fp)!=1) printf(Can not read biWidth in the info header.n); fclose(fp); return -1; if(fread(&bmih-biHeight,sizeof(LONG),1,fp)!=1) printf(Can not read biHeight in the info header.n); fclose(fp); return -1; if(fread(&bmih-biPlanes,sizeof(WORD),1,fp)!=1) printf(Can not read biPlanes in the info header.n); fclose(fp); return -1; if(fread(&bmih-biBitCount,sizeof(WORD),1,fp)!=1) printf(Can not read biBitCount in the info header.n); fclose(fp); return -1; if(fread(&bmih-biCompression,sizeof(DWORD),1,fp)!=1) printf(Can not read biCompression in the info header.n); fclose(fp); return -1; if(fread(&bmih-biSizeImage,sizeof(DWORD),1,fp)!=1) printf(Can not read biSizeImage in the info header.n); fclose(fp); return -1; if(fread(&bmih-biXPelsPerMeter,sizeof(LONG),1,fp)!=1) printf(Can not read biXPelsPerMeter in the info header.n); fclose(fp); return -1; if(fread(&bmih-biYPelsPerMeter,sizeof(LONG),1,fp)!=1) printf(Can not read biYPelsPerMeter in the info header.n); fclose(fp); return -1; if(fread(&bmih-biClrUsed,sizeof(DWORD),1,fp)!=1) printf(Can not read biClrUsed in the info header.n); fclose(fp); return -1; if(fread(&bmih-biClrImportant,sizeof(DWORD),1,fp)!=1) printf(Can not read biClrImportant 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) printf(The size of the palette must be 256.n); return -1; for(i=0;ibfOffBits); printf(bfReserved1: %ldn,bmfh-bfReserved1); printf(bfReserved2: %ldn,bmfh-bfReserved2); printf(bfSize: %ldn,bmfh-bfSize); printf(bfType: %ldn,bmfh-bfType); void PrintInfoHeader(BITMAPINFOHEADER *bmih) printf(The content in the info header of the BMP file:n); printf(biBitCount: %ldn,bmih-biBitCount); printf(biClrImportant: %ldn,bmih-biClrImportant); printf(biClrUsed: %ldn,bmih-biClrUsed); printf(biCompression: %ldn,bmih-biCompression); printf(biHeight: %ldn,bmih-biHeight); printf(biPlanes: %ldn,bmih-biPlanes); printf(biSize: %ldn,bmih-biSize); printf(biSizeImage: %ldn,bmih-biSizeImage); printf(biWidth: %ldn,bmih-biWidth); printf(biXPelsPerMeter: %ldn,bmih-biXPelsPerMeter); printf(biYPelsPerMeter: %ldn,bmih-biYPelsPerMeter); LONG GetLineBytes(int imgWidth,int bitCount) return (imgWidth*bitCount+31)/32*4; void PrintPixelData(BYTE *imgData,int width,int height,int bitCount) int i; int j ; int p; DWORD dwLineBytes=GetLineBytes(width,bitCount); if(bitCount=8) for(i=0;iheight;i+) for(j=0;jwidth;j+) p=*(imgData+dwLineBytes*(height-1-i)+j); printf(%d,p); printf(n); else if(bitCount=24) for(i=0;iheight;i+) for(j=0;jwidth*3;j+) printf(); p=*(imgData+dwLineBytes*(height-1-i)+j); printf(%d,p); j+ ; p=*(imgData+dwLineBytes*(height-1-i)+j); printf(%d,p); j+ ; p=*(imgData+dwLineBytes*(height-1-i)+j); printf(%d) ,p); printf(n); else printf(Only supported: 8 or 24 bits.n); int CheckFilePath(char *filepath) FILE *fp; int len=strlen(filepath)/sizeof(char); char ext3; if(filepath0!=int() strncpy(ext,&filepathlen-3,3); if(!(ext0=b & ext1=m & ext2=p) printf(Error: The file is not a BMP file.n); printf(Error: The extention of the filename must be bmp,not BMP n); return -1; fp=fopen(filepath,r); if(!fp) printf(Error: The path is not correct.n); return -1; fclose(fp); else printf(Error: The path must not include blank space.n); return -1; return 0; void PrintMenu() printf( -Choose Your Operation-n); printf( | 0-Input the image path |n); printf( | 1-Read the file header |n); printf( | 2-Read the info header |n); printf( | 3-Read the pixel data |n); printf( | 4-Print the file header |n); printf( | 5-Print the info header |n); printf( | 6-Print the pixel data |n); printf( | 7-View the original image |n); printf( | 8-Save as the image |n); printf( | other-Exit the program |n); printf( -n); int SaveAsImage(char *filepath) FILE *fp; fp=fopen(filepath,wb); if(!fp) printf(Error: can not create the file.n); return -1; SaveFileHeader(fp); SaveInfoHeader(fp); if(bmih.biBitCount=8) SaveColorPalette(fp); SavePixelData(fp); fclose(fp); printf(Save As the image successfully.n); return 0; void ShowImage(char * filepath) char cmd266; strcpy(cmd,start ); strcat(cmd,filepath); printf(%sn,cmd); system(cmd); int SaveFileHeader(FILE *fp) if(!bReadBMFH) printf(Please read the file header at first.n); return -1; if(fwrite(&bmfh.bfType,sizeof(WORD),1,fp)!=1) printf(Can not write bfType in the file header.n); fclose(fp); return -1; if(fwrite(&bmfh.bfSize,sizeof(DWORD),1,fp)!=1) printf(Can not write bfSize in the file header.n); fclose(fp); return -1; if(fwrite(&bmfh.bfReserved1,sizeof(WORD),1,fp)!=1) printf(Can not write bfReserved1 in the file header.n); fclose(fp); return -1; if(fwrite(&bmfh.bfReserved2,sizeof(WORD),1,fp)!=1) printf(Can not write bfReserved2 in the file header.n); fclose(fp); return -1; if(fwrite(&bmfh.bfOffBits,sizeof(DWORD),1,fp)!=1) printf(Can not write bfOffBits in the file header.n); fclose(fp); return -1; return 0; int SaveInfoHeader(FILE *fp) if(!bReadBMIH) printf(Please read the info header at first.n); return -1; if(fwrite(&bmih.biSize,sizeof(DWORD),1,fp)!=1) printf(Can not write biSize in the info header.n); fclose(fp); return -1; if(fwrite(&bmih.biWidth,sizeof(LONG),1,fp)!=1) printf(Can not write biWidth in the info header.n); fclose(fp); return -1; if(fwrite(&bmih.biHeight,sizeof(LONG),1,fp)!=1) printf(Can not write biHeight in the info header.n); fclose(fp); return -1; if(fwrite(&bmih.biPlanes,sizeof(WORD),1,fp)!=1) printf(Can not write biPlanes in the info header.n); fclose(fp); return -1; if(fwrite(&bmih.biBitCount,sizeof(WORD),1,fp)!=

温馨提示

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

评论

0/150

提交评论