




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、圖像平滑化處理 鄰域平均法C語(yǔ)言實(shí)現(xiàn)鄰域平均法是簡(jiǎn)單的空域圖像平滑化處理方法,對(duì)圖像噪聲的去除有一定的效果。#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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 安全生產(chǎn)工作要點(diǎn)概述
- 智能財(cái)稅綜合實(shí)訓(xùn) 下篇 課件 智能財(cái)稅基礎(chǔ)業(yè)務(wù)5 社會(huì)共享中級(jí)外包實(shí)務(wù)
- 2025年黨政領(lǐng)導(dǎo)干部黨章黨規(guī)黨紀(jì)黨史知識(shí)培訓(xùn)考試題庫(kù)及答案(共230題)
- 2025年度商標(biāo)權(quán)轉(zhuǎn)讓款代付服務(wù)協(xié)議
- 上市公司資金管理存款居間
- 實(shí)驗(yàn)動(dòng)物房裝修合同解除
- 無(wú)縫物流操作指南文件匯編
- 電子商務(wù)平臺(tái)客戶服務(wù)提升預(yù)案
- 塔式起重機(jī)安裝專項(xiàng)施工方案內(nèi)容
- 有機(jī)蔬菜種植要求
- 2023年寧夏回族自治區(qū)中考地理真題(原卷版)
- 2025年安全員C證考試題庫(kù)及答案-
- 2025年全球及中國(guó)電子雷管芯片模組行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 小腸扭轉(zhuǎn)病人護(hù)理查房
- 第二十屆中央紀(jì)律檢查委員會(huì)第四次全體會(huì)議公報(bào)學(xué)習(xí)解讀
- 2025年國(guó)家財(cái)政部部屬單位招聘47人歷年高頻重點(diǎn)提升(共500題)附帶答案詳解
- 幼兒園歌唱活動(dòng)基本流程
- 機(jī)場(chǎng)航站樓高空保潔服務(wù)方案
- 醫(yī)用氣體安全培訓(xùn)
- 信息系統(tǒng)應(yīng)急響應(yīng)計(jì)劃制定指南考核試卷
- 人類同種異體組織市場(chǎng)發(fā)展預(yù)測(cè)和趨勢(shì)分析
評(píng)論
0/150
提交評(píng)論