数字图像处理实验三均值滤波、中值滤波的计算机实现12281166崔雪莹计科1202班一、实验目的:1)熟悉均值滤波、中值滤波处理的理论基础;2)掌握均值滤波、中值滤波的计算机实现方法;3)学习VC++6。0的编程方法;4)验证均值滤波、中值滤波处理理论;5)观察均值滤波、中值滤波处理的结果。二、实验的软、硬件平台:硬件:微型图像处理系统,包括:主机,PC机;摄像机;软件:操作系统:WINDOWS2000或WINDOWSXP应用软件:VC++6.0三、实验内容:1)握高级语言编程技术;2)编制均值滤波、中值滤波处理程序的方法;3)编译并生成可执行文件;4)考察处理结果。四、实验要求:1)学习VC++确6。0编程的步骤及流程;2)编写均值滤波、中值滤波的程序;3)编译并改错;4)把该程序嵌入试验二给出的界面中(作适当修改);5)提交程序及文档;6)写出本次实验的体会。五、实验结果截图实验均值滤波采用的是3X3的方块,取周围的像素点取得其均值代替原像素点。边缘像素的处理方法是复制边缘的像素点,增加一个边框,计算里面的像素值得均值滤波。六、实验体会本次实验在前一次的实验基础上增加均值滤波和中值滤波,对于椒盐噪声的处理,发现中值滤波的效果更为好一点,而均值滤波是的整个图像变得模糊了一点,效果差异较大。本次实验更加增加了对数字图像处理的了解与学习。七、实验程序代码注释及分析//HistDemoADlg.h:头文件//#includeImageWnd.h#pragmaonce//CHistDemoADlg对话框classCHistDemoADlg:publicCDialogEx{//构造public:CHistDemoADlg(CWnd*pParent=NULL);//标准构造函数intnWidth;intnHeight;intnLen;intnByteWidth;BYTE*lpBackup;BYTE*lpBitmap;BYTE*lpBits;CStringFileName;CImageWndsource,dest;//对话框数据enum{IDD=IDD_HISTDEMOA_DIALOG};protected:virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持//实现protected:HICONm_hIcon;//生成的消息映射函数virtualBOOLOnInitDialog();afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);afx_msgvoidOnPaint();afx_msgHCURSOROnQueryDragIcon();DECLARE_MESSAGE_MAP()public:voidLoadBitmap(void);afx_msgvoidOnOpen();afx_msgvoidOnHist();voidHistogramEq(void);voidNoColor(void);voidHistogramEq1(intnWidth,intnHeight,BYTE*lpInput,BYTE*lpOutput);voidMeanFilter(intnWidth,intnHeight,BYTE*lpInput,BYTE*lpOutput);voidMedianFilter(intnWidth,intnHeight,BYTE*lpInput,BYTE*lpOutput);afx_msgvoidOnBnClickedClose();afx_msgvoidOnBnClickedMeanfilter();afx_msgvoidOnBnClickedMedianfilter();};HistDemoADlg.cpp对HistDemoADlg.h进行具体的实现,OnOpen()函数响应ID为IDC_OPEN的按钮事件,而且会调取文件选择对话框,选取文件之后,会显示在原始图像区域显示对应的位图图像,OnHist()函数会响应ID为IDC_HIST的按钮事件,调用HistogramEq()进行直方图均衡化的处理,HistogramEq()会调用HistogramEq1()进行直方图均衡化的处理,并用dst.setImage()显示处理之后的图像,以及NoColor()函数,对原始图像转化为灰度图像之后再显示。//HistDemoADlg.cpp:实现文件//#includestdafx.h#includeHistDemoA.h#includeHistDemoADlg.h#includeafxdialogex.h#ifdef_DEBUG#definenewDEBUG_NEW#endif#definePoint(x,y)lpPoints[(x)+(y)*nWidth]#definePoint1(x,y)lpPoints1[(x)+(y)*nWidth]//用于应用程序“关于”菜单项的CAboutDlg对话框classCAboutDlg:publicCDialogEx{public:CAboutDlg();//对话框数据enum{IDD=IDD_ABOUTBOX};protected:virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持//实现protected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg():CDialogEx(CAboutDlg::IDD){}voidCAboutDlg::DoDataExchange(CDataExchange*pDX){CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg,CDialogEx)END_MESSAGE_MAP()//CHistDemoADlg对话框CHistDemoADlg::CHistDemoADlg(CWnd*pParent/*=NULL*/):CDialogEx(CHistDemoADlg::IDD,pParent){m_hIcon=AfxGetApp()-LoadIcon(IDR_MAINFRAME);lpBitmap=0;lpBackup=0;}voidCHistDemoADlg::DoDataExchange(CDataExchange*pDX){CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CHistDemoADlg,CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_OPEN,&CHistDemoADlg::OnOpen)ON_BN_CLICKED(IDC_HIST,&CHistDemoADlg::OnHist)ON_BN_CLICKED(IDCLOSE,&CHistDemoADlg::OnBnClickedClose)ON_BN_CLICKED(IDC_MEANFILTER,&CHistDemoADlg::OnBnClickedMeanfilter)ON_BN_CLICKED(IDC_MEDIANFILTER,&CHistDemoADlg::OnBnClickedMedianfilter)END_MESSAGE_MAP()//CHistDemoADlg消息处理程序BOOLCHistDemoADlg::OnInitDialog(){CDialogEx::OnInitDialog();//将“关于...”菜单项添加到系统菜单中。//IDM_ABOUTBOX必须在系统命令范围内。ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX0xF000);CMenu*pSysMenu=GetSystemMenu(FALSE);if(pSysMenu!=NULL){BOOLbNameValid;CStringstrAboutMenu;bNameValid=strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if(!strAboutMenu.IsEmpty()){pSysMenu-AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);}}//设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动//执行此操作SetIcon(m_hIcon,TRUE);//设置大图标SetIcon(m_hIcon,FALSE);//设置小图标//TODO:在此添加额外的初始化代码source.Create(0,LSource,WS_CHILD|WS_VISIBLE,CRect(40,40,360,280),this,10000);dest.Create(0,LDestination,WS_CHILD|WS_VISIBLE,CRect(400,40,720,280),this,10001);returnTRUE;//除非将焦点设置到控件,否则返回TRUE}voidCHistDemoADlg::OnSysCommand(UINTnID,LPARAMlParam){if((nID&0xFFF0)==IDM_ABOUTBOX){CAboutDlgdlgAbout;dlgAbout.DoModal();}else{CDialogEx::OnSysCommand(nID,lParam);}}//如果向对话框添加最小化按钮,则需要下面的代码//来绘制该图标。对于使用文档/视图模型的MFC应用程序,//这将由框架自动完成。voidCHistDemoADlg::OnPaint(){if(IsIconic()){CPaintDCdc(this);//用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND,reinterpret_castWPARAM(dc.GetSafeHdc()),0);//使图标在工作区矩形中居中intcxIcon=GetSystemMetrics(SM_CXICON);intcyIcon=GetSystemMetrics(SM_CYICON);CRectrect;GetClientRect(&rect);intx=(rect.Width()-cxIcon+1)/2;inty=(rect.Height()-cyIcon+1)/2;//绘制图标dc.DrawIcon(x,y,m_hIcon);}else{CDialogEx::OnPaint();}}//当用户拖动最小化窗口时系统调用此函数取得光标//显示。HCURSORCHistDemoADlg::OnQueryDragIcon(){returnstatic_castHCURSOR(m_hIcon);}voidCHistDemoADlg::LoadBitmap(){//位图文件:BITMAPFILEHEADER+BITMAPINFOHEADER+有效信息部分BITMAPINFOHEADER*pInfo;//位图文件的头部信息指针pInfopInfo=(BITMAPINFOHEADER*)(lpBitmap+sizeof(BITMAPFILEHEADER));//pInfo指向位图文件的头部信息nWidth=pInfo-biWidth;//图片宽度nByteWidth=nWidth*3;//字节宽度if(nByteWidth%4)nByteWidth+=4-(nByteWidth%4);//使字节宽度为4的整数倍nHeight=pInfo-biHeight;//图片高度if(pInfo-biBitCount!=24)//位图的位深度不为2