第二节--中值滤波VC++实现



//------------------------------------------【中值滤波VC++实现】--------------------------------------------
//1--中值滤波是一种典型的--非线性滤波技术
//2--中值滤波本质上是--一种统计排序滤波器,对于源图像中某点(i,j),中指滤波对以该点为中心的 邻域内所有的像素统计排序
//---然后,取排序结果最中间的值作为(i,j)点的响应(新值),中指不同于均值,是指位于排序队列中间位置的元素的值
//3--中指滤波--对于某些类型的随机噪声具有非常理想的降噪能力,与线性平滑滤波器相比,中值滤波在降噪的同时引起的模糊
//---效应较低
//4--中指滤波的一种典型应用是---消除辣椒盐噪声
//----------------------------------------------------------------------------------------------------------
/************************************************************************************************************
*函数原型:void CImgProcess::MedianFilter(CImgProcess* pTo,int nFilterH,int nFilterW,int nFilterMY,int nFilterMX)
*函数功能:中指滤波---对突发性噪声,如辣椒盐噪声具有较好的抑制效果
*函数参数:1--CImgProcess* pTo--源图像的指针
*        2--int nFilterH-------滤波器的高度
*        3--int nFilterW-------滤波器的宽度
*        4--int nFilterMY------滤波器的中心元素Y的坐标
*        5--int nFilterMX------滤波器的中心元素X的坐标
*函数返回值:
*        无
*************************************************************************************************************/
void CImgProcess::MedianFilter(CImgProcess* pTo,int nFilterH,int nFilterW,int nFilterMY,int nFilterMX)
{
 //[1]初始化目标图
 pTo->InitPixels(0);
 //[2]声明一些变量
 int i,j,k,l;
 int nHeight=GetHeight();
 int nWidth=GetWidthPixel();

 int nGray;

 int* pAryGray;//邻域像素组
 pAryGray=new int[nFilterH*nFilterW];

 //[3]逐行扫描图像,进行中值滤波,行,除去边缘几行
 for(i=nFilterMY;i<nHeight-nFilterH+nFilterMY+1;i++)
 {
  //[4]列--除去边缘几行
  for(j=nFilterMX;j<nWidth-nFilterW+nFilterMX+1;j++)
  {
   //[5]读取滤波器数组
   for(k=0;k<nFilterH;k++)
   {
    for(l=0;l<nFilterW;i++)
    {
     //[6]原图像第i+k-nFilterMY行,第j+1-nFilterMX列的像素值
     nGray=GetGray(j+1-nFilterMX,i+k-nFilterMY);
     //[7]保存像素值
     pAryGray[k*nFilter+1]=nGray;
    }//l
   }//k
   //[8]通过排序获取中指
         nGray=GetMedianValue(pAryGray,nFilterH*nFilterW);
   //[9]以中指作为原来位置像素的新值
   pTo->SetPixel(j,i,RGB(nGray,nGray,nGray));
  }//j
 }//i
 delete[] pAryGray;
}
/****************************************************************************************************
*函数原型:int CImgProcess::GetMedianValue(int* pAryGray,int nFilterLen);
*函数功能:采用冒泡法对数组进行排序,并返回数组元素的值
*函数参数:
*        1--int* pAryGray----要排序提取中指的元素
*        2--int nFilterLen---数组长度
*返回值:int 中值滤波
*****************************************************************************************************/
int CImgProcess::GetMedianValue(int* pAryGray,int nFilterLen)
{
 int i,j;
 int nMedianValue;
 //[1]中间变量
 int nTemp;
 //[2]冒泡排序
 for(j=0;j<nFilterLen-1;j++)
 {
  for(i=0;i<nFilterLen-j-1;i++)
  {
   if(pAryGray[i]>pAryGray[i+1])
   {
    //[3]交换位置
       nTemp=pAryGray[i];
    pAryGray[i]=pAryGray[i+1];
    pAryGray[i+1]=nTemp;
   }//if
  }//i
 }//j
 //[4]计算中间值
 if((nFilterLen&1)>0)
 {
  //[5]数组有奇数个元素,返回中间一个元素
  nMedianValue=pAryGray[(nFilterLen+1)/2];
 }
 else
 {
  //[6]数组有偶数个元素,返回中间两个元素的平均值
  nMedianValue=(pAryGray[nFilterLen/2]+pAryGray[nFilterLen/2+1])/2;
 }//if
 //[7]返回中间值
 return nMedianValue;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值