




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 机油购销合同范本x(2025版)
- 加工厂劳务承包合同(2025版)
- 国际公路货物运输合同的内容范本2025年
- 2025年高校教师资格证之高等教育心理学考前冲刺试卷A卷含答案
- 网络直播行业规范化背景下的商业模式创新与产业链协同
- 2025版房地产项目备案合同模板(含备案范围及期限)
- 2025版虚拟货币交易与投资管理合同
- 2025版建筑电气安装工程竣工验收合同
- 二零二五年度共有产权住房项目买卖合同模板
- 2025版教育培训机构分期付款合同下载
- 医院健康体检中心简介
- 2025年安庆怀宁县事业单位招聘考试试题【答案】
- 甲状腺结节的护理查房
- DB51∕T 705-2023 四川主要造林树种苗木质量分级
- 南充职业技术学院引进招聘考试真题2024
- 猫种类介绍教学课件
- 2025至2030年中国硫氰酸红霉素行业市场发展模式及投资趋势预测报告
- 基于BIM的水利工程设计案例分享
- 退役军人考警察试题及答案
- 年初工作报告
- 2025潍坊银行笔试题库及答案
评论
0/150
提交评论