均值滤波

来源:互联网 发布:销售数据表格 编辑:程序博客网 时间:2024/05/19 02:24

1 均值滤波概述

       均值滤波是典型的线性滤波算法,它是指在图像上对目标像素给一个模板,该模板包括了其周围的临近像素(以目标象素为中心的周围8个象素,构成一个滤波模板,即去掉目标象素本身)。再用模板中的全体像素的平均值来代替原来像素值。

     均值滤波也称为线性滤波,其采用的主要方法为邻域平均法。线性滤波的基本原理是用均值代替原图像中的各个像素值,即对待处理的当前像素点(x,y),选择一个模板,该模板由其近邻的若干像素组成,求模板中所有像素的均值,再把该均值赋予当前像素点(x,y),作为处理后图像在该点上的灰度个g(x,y),即个g(x,y)=1/m∑f(x,y) m为该模板中包含当前像素在内的像素总个数。

  

      均值滤波本身存在着固有的缺陷,即它不能很好地保护图像细节,在图像去噪的同时也破坏了图像的细节部分,从而使图像变得模糊,不能很好地去除噪声点。

2 调试

2.1 matlab调试结果及代码

 均值滤波

MATLAB代码

I=imread('D:\Administrator\MyPictures\lenagray.bmp');  

J1=imnoise(I,'salt & pepper',0.02); 

%均值为0,方差为0.02 椒盐噪声 

J2=imnoise(I,'gaussian',0.02); 

%均值为0,方差为0.02高斯噪声          

subplot(2,2,1),imshow(J1);               

subplot(2,2,2),imshow(J2);

%均值滤波                              

I1=avg_filter(J1,3)                        

I2=avg_filter(J2,3)                        

subplot(2,2,3),imshow(I1);                 

subplot(2,2,4),imshow(I2);                 

 

 

%均值滤波函数

functiond=avg_filter(x,n)    

a(1:n,1:n)=1;  

[hight,width]=size(x);  

x1=double(x); 

x2=x1; 

for i=1:hight-n+1 

    forj=1:width-n+1 

       c=x1(i:i+(n-1),j:j+(n-1)).*a;     

 s=sum(sum(c));                     

  x2(i+(n-1)/2,j+(n-1)/2)=s/(n*n);

   end 

end 

 

d=uint8

 

2.2 ccs调试结果及代码

2.2.1 均值滤波

测试结果:

椒盐噪声图像滤波:

   
均值滤波


 

高斯图像滤波:

  均值滤波

 

CCS程序:

 

  #include

 

 

#define IMAGEWIDTH  256

#define IMAGEHEIGHT 256

#defineUint8      unsigned char

 

void ReadImage(char *cFileName);

void bmpDataPart(FILE* fpbmp);

void MeanFilter();

 

unsigned char grey[IMAGEHEIGHT][IMAGEWIDTH];

 

void main()

{

 

 

  

  ReadImage("D:\\Administrator\\My Pictures\\Lena2.bmp");

   MeanFilter();

 

 

   while(1);                                               

                                        

 

 

 

void ReadImage(char *cFileName)

{

 

 

       FILE *fp;

 

       if ( fp=fopen(cFileName,"rb" ) )

       {

          

                bmpDataPart(fp);

                            

 

               fclose(fp);

       }

}

 

 

 

void bmpDataPart(FILE* fpbmp)

{

  int i, j=0;

 

  unsigned char* pix=NULL;

  fseek(fpbmp, 55L, SEEK_SET);

 

  pix=(unsigned char*)malloc(256);

 

  for(j=0;j

   {

              fread(pix, 1, 256, fpbmp);

    for(i=0;i

       {

                        

           grey[IMAGEHEIGHT-1-j][i]  =pix[i];

 

       }

   }

}

 

void MeanFilter()

{

 

 int i=0,j=0;

 intwheight=1,wwidth=1;                  //窗口大小(2*wheight+1) * (2*wwidth+1)

 int k=0,l=0;

 double sum=0;

 

 for(i=0;i

   {

 

    for(j=0;j

       {

           

 

           if((i>wheight-1) && (iwwidth-1) && (j

                      

                     {

              

              sum=0;

 

                      for(k=i-wheight;k<=i+wheight;k++)

                       for(l=j-wwidth;l<=j+wwidth;l++)

                        {

                  sum += grey[k][l];

                }

 

               

              grey[i][j]= sum/((2*wheight+1) * (2*wwidth+1))+0.5;

                     }

 

       }

 

   }

 

}

0 0
原创粉丝点击