版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、圖像平滑化處理 鄰域平均法C語言實現鄰域平均法是簡單的空域圖像平滑化處理方法,對圖像噪聲的去除有一定的效果。#ifndef BMP_H_4_INCLUDED#define BMP_H_4_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 bfReserve
2、d2;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 tagRG
3、BQUAD/rgbq BYTE rgbBlue;BYTE rgbGreen;BYTE rgbRed;BYTE rgbReserved;RGBQUAD;typedef struct tagBITMAPINFOBITMAPINFOHEADER bmiHeader;RGBQUAD bmiColors1;BITMAPINFO;#include stdio.h#include stdlib.h#include string.h#include malloc.h#include ctype.h#include process.h#include"BMP_4.h"BITMAPFILEHE
4、ADER bmfh;BITMAPINFOHEADER bmih;BYTE*imgData;void readData();int ReadFileHeader(BITMAPFILEHEADER*);int ReadInfoHeader(BITMAPINFOHEADER*);int CreatePalette(RGBQUAD);int ReadPixelData(BYTE*);LONG GetLineBytes(int,int);void NeighborAverage(double);int SaveAsImage(char*);int main()int i;char saveasfilep
5、ath256;DWORD dwLineBytes;double Error;readData();dwLineBytes=GetLineBytes(bmih.biWidth,bmih.biBitCount);printf("Input error!n");scanf("%f",&Error);NeighborAverage(Error);printf("Save as another path(ex.d:/poon.bmp)n");scanf("%s",saveasfilepath);i=SaveAsIma
6、ge(saveasfilepath);if(i=-1)printf("Error:failed to save the image.n");return 0;void readData()int i,k,h;DWORD dwLineBytes;i=ReadFileHeader(&bmfh);if(i=0)printf("Read file header successful!n");k=ReadInfoHeader(&bmih);if(k=0)printf("Read info header successful!n"
7、);dwLineBytes=GetLineBytes(bmih.biWidth,bmih.biBitCount);imgData=(BYTE*)malloc(dwLineBytes*bmih.biHeight*sizeof(BYTE);h=ReadPixelData(imgData);if(h=0)printf("Read pixel data successful!n");if(i=0&&k=0&&h=0)printf("Read datas successful!n");int ReadFileHeader(BITMA
8、PFILEHEADER*bmfh)FILE*dataFile;dataFile=fopen("shuaige.bmp","r");if(fread(&bmfh-bfType,sizeof(WORD),1,dataFile)!=1)printf("Can not read bfType in the file header.n");fclose(dataFile);return-1;if(fread(&bmfh-bfSize,sizeof(DWORD),1,dataFile)!=1)printf("Can no
9、t read bfSize in the file header.n");fclose(dataFile);return-1;if(fread(&bmfh-bfReserved1,sizeof(WORD),1,dataFile)!=1)printf("Can not read bfReserved1 in the file header.n");fclose(dataFile);return-1;if(fread(&bmfh-bfReserved2,sizeof(WORD),1,dataFile)!=1)printf("Can not r
10、ead bfReserved2 in the file header.n");fclose(dataFile);return-1;if(fread(&bmfh-bfOffBits,sizeof(DWORD),1,dataFile)!=1)printf("Can not read bfOffBits in the file header.n");fclose(dataFile);return-1;fclose(dataFile);return 0;int ReadInfoHeader(BITMAPINFOHEADER*bmih)FILE*dataFile;d
11、ataFile=fopen("shuaige.bmp","r");fseek(dataFile,14,SEEK_SET);if(fread(&bmih-biSize,sizeof(DWORD),1,dataFile)!=1)printf("Can not read biSize in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biWidth,sizeof(LONG),1,dataFile)!=1)printf("Can not r
12、ead biWidth in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biHeight,sizeof(LONG),1,dataFile)!=1)printf("Can not read biHeight in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biPlanes,sizeof(WORD),1,dataFile)!=1)printf("Can not read biPlane
13、s in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biBitCount,sizeof(WORD),1,dataFile)!=1)printf("Can not read biBitCount in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biCompression,sizeof(DWORD),1,dataFile)!=1)printf("Can not read biCompr
14、ession in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biSizeImage,sizeof(DWORD),1,dataFile)!=1)printf("Can not read biSizeImage in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biXPelsPerMeter,sizeof(LONG),1,dataFile)!=1)printf("Can not rea
15、d biXPelsPerMeter in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biYPelsPerMeter,sizeof(LONG),1,dataFile)!=1)printf("Can not read biYPelsPerMeter in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biClrUsed,sizeof(DWORD),1,dataFile)!=1)printf(&quo
16、t;Can not read biClrUsed in the info header.n");fclose(dataFile);return-1;if(fread(&bmih-biClrImportant,sizeof(DWORD),1,dataFile)!=1)printf("Can not read biClrImportant in the info header.n");fclose(dataFile);return-1;fclose(dataFile);return 0;int CreatePalette(RGBQUAD pal)int i;i
17、f(sizeof(pal)/sizeof(RGBQUAD)!=256)printf("The size of the palette must be 256.n");return-1;for(i=0;i 256;i+)pali.rgbBlue=i;pali.rgbGreen=i;pali.rgbRed=i;pali.rgbReserved=0;return 0;int ReadPixelData(BYTE*imgData)BYTE*data;FILE*dataFile;DWORD dwLineBytes;dwLineBytes=GetLineBytes(bmih.biWid
18、th,bmih.biBitCount);data=(BYTE*)malloc(dwLineBytes*bmih.biHeight*sizeof(BYTE);dataFile=fopen("shuaige.bmp","rb");fseek(dataFile,bmfh.bfOffBits,SEEK_SET);if(fread(data,dwLineBytes*bmih.biHeight*sizeof(BYTE),1,dataFile)!=1)printf("Can not read the pixel data.n");free(data
19、);fclose(dataFile);return-1;memcpy(imgData,data,dwLineBytes*bmih.biHeight*sizeof(BYTE);free(data);fclose(dataFile);return 0;LONG GetLineBytes(int imgWidth,int bitCount)return(imgWidth*bitCount+31)/32*4;void NeighborAverage(double Error)int dwLineBytes;int NewdwLineBytes;int NewHeight;BYTE*NewData;do
20、uble gray;int i,j;dwLineBytes=GetLineBytes(bmih.biWidth,bmih.biBitCount);NewdwLineBytes=dwLineBytes+2;NewHeight=bmih.biHeight+2;NewData=(BYTE*)malloc(NewdwLineBytes*NewHeight*sizeof(BYTE);for(i=0;i NewHeight;i+)for(j=0;j NewdwLineBytes;j+)NewDataNewdwLineBytes*i+j=0;for(i=0;i bmih.biHeight;i+)for(j=
21、0;j dwLineBytes;j+)NewDataNewdwLineBytes*(i+1)+j+1=imgDatadwLineBytes*i+j;for(i=1;i NewHeight-1;i+)for(j=1;j NewdwLineBytes-1;j+)gray=(double)(NewDataNewdwLineBytes*(i-1)+j-1+NewDataNewdwLineBytes*i+j-1+NewDataNewdwLineBytes*(i+1)+j-1+NewDataNewdwLineBytes*(i-1)+j+NewDataNewdwLineBytes*(i+1)+j+NewDa
22、taNewdwLineBytes*(i-1)+j+1+NewDataNewdwLineBytes*i+j+1+NewDataNewdwLineBytes*(i+1)+j+1)/8;if(abs(double)(NewDataNewdwLineBytes*i+j)-gray)Error)imgDatadwLineBytes*(i-1)+j-1=(int)gray;int SaveAsImage(char*filepath)FILE*dataFile;RGBQUAD pal256;int i;DWORD dwLineBytes;dwLineBytes=GetLineBytes(bmih.biWid
23、th,bmih.biBitCount);dataFile=fopen(filepath,"wb");if(!dataFile)printf("Error:Can not open the file:%sn",filepath);return-1;for(i=0;i 256;i+)pali.rgbReserved=0;pali.rgbBlue=i;pali.rgbGreen=i;pali.rgbRed=i;if(fwrite(&bmfh.bfType,sizeof(WORD),1,dataFile)!=1)printf("Can not
24、write bfType in the file header.n");fclose(dataFile);return-1;if(fwrite(&bmfh.bfSize,sizeof(DWORD),1,dataFile)!=1)printf("Can not write bfSize in the file header.n");fclose(dataFile);return-1;if(fwrite(&bmfh.bfReserved1,sizeof(WORD),1,dataFile)!=1)printf("Can not write bf
25、Reserved1 in the file header.n");fclose(dataFile);return-1;if(fwrite(&bmfh.bfReserved2,sizeof(WORD),1,dataFile)!=1)printf("Can not write bfReserved2 in the file header.n");fclose(dataFile);return-1;if(fwrite(&bmfh.bfOffBits,sizeof(DWORD),1,dataFile)!=1)printf("Can not wri
26、te bfOffBits in the file header.n");fclose(dataFile);return-1;if(fwrite(&bmih.biSize,sizeof(DWORD),1,dataFile)!=1)printf("Can not write biSize in the info header.n");fclose(dataFile);return-1;if(fwrite(&bmih.biWidth,sizeof(LONG),1,dataFile)!=1)printf("Can not write biWidt
27、h in the info header.n");fclose(dataFile);return-1;if(fwrite(&bmih.biHeight,sizeof(LONG),1,dataFile)!=1)printf("Can not write biHeight in the info header.n");fclose(dataFile);return-1;if(fwrite(&bmih.biPlanes,sizeof(WORD),1,dataFile)!=1)printf("Can not write biPlanes in t
28、he info header.n");fclose(dataFile);return-1;if(fwrite(&bmih.biBitCount,sizeof(WORD),1,dataFile)!=1)printf("Can not write biBitCount in the info header.n");fclose(dataFile);return-1;if(fwrite(&bmih.biCompression,sizeof(DWORD),1,dataFile)!=1)printf("Can not write biCompression in the info header.n");fclose(dataFile);return-1;if(fwrite(&bmih.biSizeImage,sizeof(DWORD),1,dataFile)!=1)printf("Can not write biSizeImage in the info header.n");fclose(dataFile);return-1;if(fwrite(&bmih.biXPelsPerMeter,sizeof(LONG),1,dataFile)!=1)pri
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《巨匠童心》課件
- 《童年回憶的》課件
- 《客戶梳理技巧》課件
- 2024年黑龍江農業(yè)工程職業(yè)學院單招職業(yè)技能測試題庫標準卷
- 四川省南充市2025屆高三上學期高考適應性考試(一診)英語試卷含答案
- 單位管理制度匯編大全職員管理
- 單位管理制度合并選集人力資源管理
- 單位管理制度分享合集【人力資源管理篇】
- 單位管理制度分享大合集【人力資源管理篇】
- 單位管理制度范例匯編職員管理篇十篇
- 2021-2022學年四川省南充市九年級(上)期末數學試卷
- 2024政府采購評審專家考試題庫附含答案
- 《商務跟單工作流程》課件
- 中小學膳食經費管理的目標與原則
- 2024高血壓的診斷與治療
- 重度子癇前期產后護理查房
- 制作課件wps教學課件
- 北京市海淀區(qū)2023屆高三上學期期末考試化學試卷 附解析
- MCN機構簽約合同范本
- 2024年滬教版一年級上學期語文期末復習習題
- 2024廣東省廣州市天河區(qū)中考一模語文試題含答案解析
評論
0/150
提交評論