C语言进行数字图像处理

上传人:桔**** 文档编号:431608786 上传时间:2023-10-28 格式:DOC 页数:20 大小:185KB
返回 下载 相关 举报
C语言进行数字图像处理_第1页
第1页 / 共20页
C语言进行数字图像处理_第2页
第2页 / 共20页
C语言进行数字图像处理_第3页
第3页 / 共20页
C语言进行数字图像处理_第4页
第4页 / 共20页
C语言进行数字图像处理_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《C语言进行数字图像处理》由会员分享,可在线阅读,更多相关《C语言进行数字图像处理(20页珍藏版)》请在金锄头文库上搜索。

1、用C语言进行数字图像处理作者:彭千贺由B件:pengqianheOgmailcom博客:http :/pengqianhe googlepages com声明:本篇文章和程序全部有彭千贺原创,严禁任何形式的修改.如有发现任何形式的修改,将 有权追究其相关责任人的法律责任。但是你可以自由的传播本文章的副本。如需要修改请邮 件通知我。其实,数字图像处理有几步呢? 一共三步。第一步,读入图片。第二步,处理图片。第三步,保存图片。 而第二步主要涉及的是处理图像的算法.所以.我在这里就不多说了。而第一步和第三步是为第二步做 铺垫的。关于位图文件的文件结构.可以参看我的另外一篇文章详说如何用C语言.C+和

2、JAVA来读入和保 存位图。此文章可以在我的博客下载到。我在这里,贴出用C语言进行读入位图的文件头,信息头. 像素数据和保存位图等程序,另外还有声明位图文件结构的头文件。以下程序在VC6 0中.已经无误通过编译o位图文件结构的声明:BMP-h#ifndef BMPJI_INCLUDEDdefine BMPJI_INCLUDEDtypedef unsigned short WORD: typedef unsigned long DWORD: typedef long LONG;typedef unsigned char BYTE:typedef struct tagBITMAPFILEHEADE

3、R / bmfhWORDbfType:DWORDbfSize;WORDbfReservedl;WORDbfReserved2;DWORDbfOffBits;BITMAPFILEHEADER;typedef struct tagBITMXPINFOHEADER / bmihDWORD biSize;LONG biWidth;LONG biHeight;WORD biPlanes;WORDbiBitCount;DWORDDWORDI.ONGbiCompression; biSizeimage;bi XPelsPerMeter;LONGDWORDbiYPelsPerMeter; biClrlsed;

4、DWORDbiClrlmportant: BITMAPINFOHEADER;typedef struct tagRGBQUAD / rgbqBYTE rgbBlue;BYTE rgbGreen:BYTE rgbRed;BYTE rgbReserved;RGBQUAD;typedef struct tagBITMAPINFOBITMAPINFOHEADER bmiHeader:RGBQUAD bmiColorsfl:BITMAPINFO;#endif /INCLUDED主程序:main.c彳include 彳include 斗include include 二include #inelude h

5、 includeBITMAPFILEHEADER bmfh: BITMAPiXFOHEADER bmih: BYTE * imgData: bool bReadBMFH=false; bool bReadBMlH=false; boo1 bReadPixel=false:/检査路径是否合法:文件能打开;以bmp为后缀名 int CheckFilePath(char *filepath):/读入位图的文件头int ReadFileHeader(char *fi1epath,BITMAPFILEHEADER *bmfh):/打印位图的文件头void PrintFileHeader(BITMAPFI

6、LEHEADER *bmfh):/读入位图的信息头int Read InfoHeader (char *f i lepath, BITMAP1 NFOHE.XDER *bmih):/打印位图的信息头void PrintlnfoHeader(BIBfAPINFOHEADER *bmih):/创建8位位图的调色板int CreatePalette(RGBQUAD pa1);/读入位图的像素数据int ReadPixe1 Data(char *filepath.BYTE *imgData):/计算每行像素所占的字节数LONG GetLineBytes(int imgWidth,int bitCoun

7、t):/打印位图的像素数据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 Save Inf oHeader(FILE* fp);/保存调色板int SaveColorPalette(FILE *fp):/保存像素数据int

8、 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);p厂 i ntf (*T1M i magen);printf (* Input the path of the BMP f i 1 e:n*j ; gets (filepath);i二CheckF i1 ePath(fi1 epa

9、th);if(i=-l)return -1:doPrintMenuO :scanf(%iT,&select);switch(select)case 0:printf(Input the path of the BMP file:n);scanf(%s,filepath);CheckFi1 ePath(fi1epath);break:case 1:i=ReadFileHeader(filepath,&bmfh);if(i!=-l)printf(MRead the file header successfully.n*): bReadBMFH二true;break;elseprintf(MRead

10、 the file header failed.n):bReadBMFH=false;q=l;break;case 2:i =ReadInfoHeader(f ilepath,&bmi h);if(i!=-l)printf(Read the info header successfully.n); bReadBIIlI 二 true;break;elseprintf(Read the info header failed.n); bReadBMIH=false;q=l:break:case 3:if(!bReadBMIH)printf(Please read the info header a

11、t first.n); break;height=bmihbi Height;width二bmih.biWidth;bitCount=bmih.biBitCount;tlwLineRytes=Geti,ineByt.es(wicll.h .hi tCount):imgData=(BYTE*)malloc(dwL i neBy tes* he i gh t * s i zeof(BYTE): if(!imgData)printf(MCan not al locate memory for the imagen); q=l: break;i =ReadPixelData(filepath,imgD

12、ata);if(i=-l) printf (Read the pixel data failed.nM): bReadPixel=false;Q=l:break;else printf(Read the pixel data successful 1yn); bReadPixel=true:break:case 4:if(bReadBMFH)PrintFileHeader(&bmfh):break;elseprintf(Please read the file header at first.nN); break;case 5:if(bReadBMlH)PrintInfoHeader(&bm

13、ih);break;elseprintf(Please read the info header at first.nN); break;case 6:if(bReadPixel)PrintPixelData(imgData.width.hei ght.bitCount); break;elseprintf(Please read the pixel data at first.n): break;case 7:ShowImage(filepath):break:case 8:save:n);printf (f, Input the path (ex. d:/poon.bmp) you wan

14、t scanf(H%sMsaveasf ilepath):i=SaveAsImage(saveasfilepath);if(i=-l)printf(MError: failed to save the image.n); break;break;default:q二 1;break;select二9527; while (q二二0):return 0;int ReadFileHeader(char *fi1 epath.BITMAPFILEHEADER *bmfh)FILE *fp;f p=f open(f ilepath.rb):if(!fp)printf(Can not open the file:%sn,filepath);return -1;if(fread(&bmfh-bfType.sizeof(WORD)1,fp)!二1) prin

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号