数字图像处理实验三中值滤波和均值滤波实验报告Word格式.docx
- 文档编号:7114605
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:21
- 大小:465.06KB
数字图像处理实验三中值滤波和均值滤波实验报告Word格式.docx
《数字图像处理实验三中值滤波和均值滤波实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《数字图像处理实验三中值滤波和均值滤波实验报告Word格式.docx(21页珍藏版)》请在冰点文库上搜索。
五、实验结果截图
实验均值滤波采用的是3X3的方块,取周围的像素点取得其均值代替原像素点。
边缘像素的处理方法是复制边缘的像素点,增加一个边框,计算里面的像素值得均值滤波。
六、实验体会
本次实验在前一次的实验基础上增加均值滤波和中值滤波,对于椒盐噪声的处理,发现中值滤波的效果更为好一点,而均值滤波是的整个图像变得模糊了一点,效果差异较大。
本次实验更加增加了对数字图像处理的了解与学习。
七、实验程序代码注释及分析
.”菜单项添加到系统菜单中。
gbBlue;
lpBits2[p2+1]=pPalette[Palette].rgbGreen;
lpBits2[p2+2]=pPalette[Palette].rgbRed;
}
}
deletelpTemp;
}
lpBits=lpBitmap+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
if(lpBackup)deletelpBackup;
lpBackup=newBYTE[nLen];
memcpy(lpBackup,lpBitmap,nLen);
}
voidCHistDemoADlg:
:
OnOpen()mp|所有文件|*.*||"
this);
//新建文件选择对话框
if()==IDOK)
{
FileName=();
//得到文件的路径
if(!
(FileName,CFile:
modeRead))return;
//以只读方式打开文件
//TODO:
addloadingcodehere
if(lpBitmap)deletelpBitmap;
//保证lpBitmap为空
nLen=(int)();
//得到文件的长度
lpBitmap=newBYTE[nLen];
//为lpBitmap分配空间
(lpBitmap,nLen);
//将文件的内容读入到lpBitmap所指向的内存区域
LoadBitmap();
//调用LoadBitmap(),加载位图图像
if(lpBitmap)(nWidth,nHeight,lpBits);
OnHist()
{
//TODO:
在此添加控件通知处理程序代码
HistogramEq();
voidGetPoints(intnWidth,intnHeight,BYTE*lpBits,BYTE*lpPoints)
intx,y,p;
intnByteWidth=nWidth*3;
if(nByteWidth%4)nByteWidth+=4-(nByteWidth%4);
for(y=0;
y<
nHeight;
y++)//每一行
for(x=0;
x<
nWidth;
x++)//每一列
{
p=x*3+y*nByteWidth;
lpPoints[x+y*nWidth]=(BYTE)*(float)lpBits[p+2]+*(float)lpBits[p+1]+*(float)lpBits[p]+;
//三种颜色的比例计算对应点的颜色值,并且强制转换成BYTE
voidPutPoints(intnWidth,intnHeight,BYTE*lpBits,BYTE*lpPoints)//逐个对lpBits进行赋值
intx,y,p,p1;
p1=x+y*nWidth;
lpBits[p]=lpPoints[p1];
lpBits[p+1]=lpPoints[p1];
lpBits[p+2]=lpPoints[p1];
HistogramEq(void)
if(lpBitmap==0)return;
BYTE*lpOutput=newBYTE[nByteWidth*nHeight];
HistogramEq1(nWidth,nHeight,lpBits,lpOutput);
(nWidth,nHeight,lpOutput);
//在直方图均衡化的区域显示结果
deletelpOutput;
NoColor();
//将原始图像转换成灰度图像
NoColor()
BYTEPoint;
Point=(BYTE)*(float)lpBits[p+2]+*(float)lpBits[p+1]+*(float)lpBits[p]+;
//计算颜色值,在0-255的灰度级之间
lpBits[p+2]=Point;
lpBits[p+1]=Point;
lpBits[p]=Point;
(nWidth,nHeight,lpBits);
//将彩色图像转化成灰度图像
HistogramEq1(intnWidth,intnHeight,BYTE*lpInput,BYTE*lpOutput)
intx,y;
BYTE*lpPoints=newBYTE[nWidth*nHeight];
//像素点的个数
GetPoints(nWidth,nHeight,lpInput,lpPoints);
//lpPoints存的是颜色值
intr[256],s[256];
//颜色值数组,统计对应颜色值像素点的个数
ZeroMemory(r,1024);
ZeroMemory(s,1024);
y++){//统计对应颜色值像素点的个数,Point(x,y)是lpPoints(x,y)
x++){
r[Point(x,y)]++;
s[0]=r[0];
for(y=1;
256;
y++)
s[y]=s[y-1];
s[y]+=r[y];
}//计算颜色值的前y种颜色的总像素点的个数(像素点颜色值<
=y)
y++){//将计算对应点的像素值,直方图均匀化的结果保存在lpPoints
Point(x,y)=s[Point(x,y)]*255/nWidth/nHeight;
PutPoints(nWidth,nHeight,lpOutput,lpPoints);
//输出lpPoints到lpOutput
deletelpPoints;
OnBnClickedClose()
//ExitProcess(0);
//注意使用时先释放分配的内存,以免造成内存泄露
//exit(0);
//正常终止程序;
exit(非0)非正常终止程序
PostQuitMessage(0);
//最常用
OnBnClickedMeanfilter()
MeanFilter(nWidth,nHeight,lpBits,lpOutput);
MeanFilter(intnWidth,intnHeight,BYTE*lpInput,BYTE*lpOutput)
{
BYTE*lpPoints1=newBYTE[(nWidth+2)*(nHeight+2)];
nHeight+1;
y++){//中间最整块图像的拷贝
for(x=1;
Point1(x,y)=Point(x-1,y-1);
//lpPoints1[y][x]=lpPoints[y-1][x-1];
}
y++){//最左边和最右边一列的拷贝
Point1(0,y)=Point(0,y-1);
Point1(nWidth+1,y)=Point(nWidth-1,y-1);
//lpPoints1[y][0]=lpPoints[y-1][0];
//lpPoints1[y][nWidth+1]=lpPoints[y-1][nWidth-1];
for(x=0;
nWidth+2;
x++){//最上边和最下边一行的拷贝
Point1(x,0)=Point1(x,1);
Point1(x,nHeight+1)=Point1(x,nHeight);
//lpPoints1[0][x]=lpPoints1[1][x];
//lpPoints1[nHeight+1][x]=lpPoints1[nHeight][x];
y++){//求以某点为中心的九个数平均值
for(x=0;
Point(x,y)=(Point1(x,y)+Point1(x+1,y)+Point1(x+2,y)+
Point1(x,y+1)+Point1(x+1,y+1)+Point1(x+2,y+1)+
Point1(x,y+2)+Point1(x+1,y+2)+Point1(x+2,y+2))/9;
/*
lpPoints[y][x]=(lpPoints1[y][x]+lpPoints1[y][x+1]+lpPoints1[y][x+2]+
lpPoints1[y+1][x]+lpPoints1[y+1][x+1]+lpPoints1[y+1][x+2]+
lpPoints1[y+2][x]+lpPoints1[y+2][x+1]+lpPoints1[y+2][x+2])/9;
*/
}
MedianFilter(intnWidth,intnHeight,BYTE*lpInput,BYTE*lpOutput)
y++){//中间一整块拷贝
BYTE*window=newBYTE[9];
y++){
intk=0;
for(inti=y;
i<
=y+2;
i++){
for(intj=x;
j<
=x+2;
j++){
if(k<
9)
window[k++]=Point1(j,i);
}
for(intm=0;
m<
5;
++m)//求9个数的中值,window[4]为中值
{
intmin=m;
for(intn=m+1;
n<
9;
++n)
if(window[n]<
window[min])
min=n;
//Putfoundminimumelementinitsplace
BYTEtemp=window[m];
window[m]=window[min];
window[min]=temp;
Point(x,y)=window[4];
OnBnClickedMedianfilter()
MedianFilter(nWidth,nHeight,lpBits,lpOutput);
//在中值滤波的区域显示结果
类继承自CWnd主要是图像显示方面的函数,如水平滚轮和垂直滚轮的事件函数,以及绘制函数OnPaint(),初始化函数等等,以及存放需要绘制的图像信息的成员变量。
setImage(),外接提供绘制的信息,通过参数传递给内部的成员变量。
#pragmaonce
classCImageWnd:
publicCWnd
public:
intHCurrentPosition;
//当前位置(水平)
intHScrollMax;
//滑动最大位置(水平)
intHScrollPosition;
//滑动位置(水平)
intVCurrentPosition;
//当前位置(垂直)
intVScrollMax;
//滑动最大位置(垂直)
intVScrollPosition;
//滑动位置(垂直)
intnWidth;
//宽度
intnHeight;
//高度
intnByteWidth;
//字节宽度
BYTE*lpBits;
//指向字节的指针
CImageWnd(void);
~CImageWnd(void);
voidSetImage(intcx,intcy,constvoid*bits);
voidSetScroll(intcx,intcy);
protected:
afx_msgBOOLOnEraseBkgnd(CDC*pDC);
afx_msgvoidOnHScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar);
afx_msgvoidOnVScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar);
afx_msgvoidOnPaint();
afx_msgintOnCreate(LPCREATESTRUCTlpCreateStruct);
DECLARE_MESSAGE_MAP()
/*
//{{AFX_MSG(CImageWnd)
afx_msgBOOLOnEraseBkgnd(CDC*pDC);
afx_msgvoidOnHScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar);
afx_msgvoidOnVScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar);
//}}AFX_MSG
*/
};
具体实现的一些功能函数,注释已经给出,在此不再赘述。
#include"
"
#defineBACKGROUNDRGB(128,128,128)
CImageWnd:
CImageWnd(void)//初始化,将其全设为0
HScrollPosition=0;
HCurrentPosition=0;
HScrollMax=0;
VScrollPosition=0;
VCurrentPosition=0;
VScrollMax=0;
lpBits=0;
~CImageWnd(void)//析构函数,释放空间
if(lpBits)deletelpBits;
BEGIN_MESSAGE_MAP(CImageWnd,CWnd)
//{{AFX_MSG_MAP(CImageWnd)
ON_WM_ERASEBKGND()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_PAINT()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOLCImageWnd:
OnEraseBkgnd(CDC*pDC)//设置背景色,320,240
if(!
lpBits)pDC->
FillSolidRect(0,0,320,240,BACKGROUND);
returnTRUE;
voidCImageWnd:
OnPaint()//绘制函数,每次需要显示的图像发生变化,就会重画,重复调用这个函数进行重画
CPaintDCdc(this);
//当前对象作为参数
if(lpBits)
intx,y;
x=-HScrollPosition;
y=-VScrollPosition;
if(nWidth<
320)x=(320-nWidth)/2;
if(nHeight<
240)y=(240-nHeight)/2;
//设置绘制位图文件的头信息
BITMAPINFOHEADERbmi;
=sizeof(BITMAPINFOHEADER);
=nWidth;
=nHeight;
=1;
=24;
=BI_RGB;
=0;
//将像素点填充到绘图区域
StretchDIBits,x,y,nWidth,nHeight,0,0,nWidth,nHeight,lpBits,(BITMAPINFO*)&
bmi,DIB_RGB_COLORS,SRCCOPY);
OnHScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar)//水平滚轮事件处理函数,
switch(nSBCode)
caseSB_LINEDOWN:
//每次滑动的间隔为5个像素点
HScrollPosition+=5;
b
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数字图像 处理 实验 中值 滤波 均值 报告
![提示](https://static.bingdoc.com/images/bang_tan.gif)