最新用c語言進(jìn)行數(shù)字圖像處理_第1頁
最新用c語言進(jìn)行數(shù)字圖像處理_第2頁
最新用c語言進(jìn)行數(shù)字圖像處理_第3頁
最新用c語言進(jìn)行數(shù)字圖像處理_第4頁
最新用c語言進(jìn)行數(shù)字圖像處理_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、用c語言進(jìn)行數(shù)字圖像處理其實,數(shù)字圖像處理有幾步呢? 一共三步。第一步,讀入圖片。第二步,處理圖片。第三步,保存圖片。而第二步主要涉及的是處理圖像的算法,所以,我在這里就不多說了。 而第一步和第三步是為第二步做位圖文件結(jié)構(gòu)的聲明: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);/創(chuàng)建8位位圖的調(diào)色板int CreatePalette(RGBQUAD pal);/讀入位圖的像素數(shù)據(jù)int ReadPixelData(char *filepath,BYTE *imgData);/計算每行像素所占的字節(jié)數(shù)LONG GetLi neBytes(i nt img

6、Width,i nt bitCou nt);/打印位圖的像素數(shù)據(jù)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);/保存調(diào)色板int SaveColorPal

7、ette(FILE *fp);/保存像素數(shù)據(jù)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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論