用-c-语言进行数字图像处理_第1页
用-c-语言进行数字图像处理_第2页
用-c-语言进行数字图像处理_第3页
用-c-语言进行数字图像处理_第4页
用-c-语言进行数字图像处理_第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 bmiColors 1 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 filepath 256 char saveasfilepath 256 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 switch select case 0 printf Input the path of the BMP file n scanf s filepath CheckFilePath filepath break case 1 i ReadFileHeader filepath 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 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 break else printf Please read the file header at first n break case 5 if bReadBMIH PrintInfoHeader 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 s n filepath return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread 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 s n filepath return 1 fseek fp 14 SEEK SET if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread fclose fp return 1 if fread 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 ld n bmfh bfReserved1 printf bfReserved2 ld n bmfh bfReserved2 printf bfSize ld n bmfh bfSize printf bfType ld n bmfh bfType void PrintInfoHeader BITMAPINFOHEADER bmih printf The content in the info header of the BMP file n printf biBitCount ld n bmih biBitCount printf biClrImportant ld n bmih biClrImportant printf biClrUsed ld n bmih biClrUsed printf biCompression ld n bmih biCompression printf biHeight ld n bmih biHeight printf biPlanes ld n bmih biPlanes printf biSize ld n bmih biSize printf biSizeImage ld n bmih biSizeImage printf biWidth ld n bmih biWidth printf biXPelsPerMeter ld n bmih biXPelsPerMeter printf biYPelsPerMeter ld n 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 i height i for j 0 j width j p imgData dwLineBytes height 1 i j printf d p printf n else if bitCount 24 for i 0 i height i for j 0 j width 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 ext 3 if filepath 0 int strncpy ext if ext 0 b 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 cmd 266 strcpy cmd start strcat cmd filepath printf s n cmd system cmd int SaveFileHeader FILE fp if bReadBMFH printf Please read the file header at first n return 1 if fwrite fclose fp return 1 if fwrite fclose fp return 1 if fwrite fclose fp return 1 if fwrite fclose fp return 1 if fwrite 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 fclose fp return 1 if fwrite fclose fp return 1 if fwrite fclose fp return 1 if fwrite fclose fp return 1 if fwrite fclose fp return 1 if fwrite fclose fp return 1 if fwrite fclose f

温馨提示

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

评论

0/150

提交评论