均值滤波是用像素点邻域的均值代替该像素点,通过将各个像素点做平均平滑滤波,平滑效果与滤波窗口有关,窗口越大,平滑效果越强,但细节和边缘也越加模糊。

matlab实现

点击(此处)折叠或打开

  1. %均值滤波 test
  2. A = imread('E:\test\Lenna.jpg');
  3. B = imnoise(A,'gaussian');

  4. figure;subplot(221),imshow(A);title('原图')
  5. subplot(222),imshow(B);title('高斯噪声图')

  6. C = m_average_filter(B,5);
  7. subplot(223),imshow(uint8(C));title('均值滤波后')

  8. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  9. % Purpose: do averge filter
  10. % Author: pottor
  11. % Date: 2017/5/10

  12. function out = m_average_filter(in,w)

  13. if( mod(w,2)==0 )
  14.     error( 'w must be odd' );
  15. end

  16. out = in;
  17. hw = floor(w/2);

  18. F = ones(w,w)/(w*w);

  19. if size(in,3) == 1
  20.     [m,n] = size(in);
  21.     for i = 1:m
  22.         for j = 1:n
  23.             iMin = max(i-hw,1);
  24.             iMax = min(i+hw,m);
  25.             jMin = max(j-hw,1);
  26.             jMax = min(j+hw,n);
  27.             
  28.             I = in(iMin:iMax,jMin:jMax);
  29.             out(i,j) = sum(sum(double(I).*F((iMin:iMax)-i+hw+1,(jMin:jMax)-j+hw+1)));
  30.         end
  31.     end
  32. else
  33.     r = in(:,:,1);
  34.     g = in(:,:,2);
  35.     b = in(:,:,3);
  36.     [m,n] = size(r);
  37.     for i = 1:m
  38.         for j = 1:n
  39.             iMin = max(i-hw,1);
  40.             iMax = min(i+hw,m);
  41.             jMin = max(j-hw,1);
  42.             jMax = min(j+hw,n);
  43.             
  44.             Ir = r(iMin:iMax,jMin:jMax);
  45.             Ig = g(iMin:iMax,jMin:jMax);
  46.             Ib = b(iMin:iMax,jMin:jMax);

  47.             out(i,j,1) = sum(sum(double(Ir).*F((iMin:iMax)-i+hw+1,(jMin:jMax)-j+hw+1)));
  48.             out(i,j,2) = sum(sum(double(Ig).*F((iMin:iMax)-i+hw+1,(jMin:jMax)-j+hw+1)));
  49.             out(i,j,3) = sum(sum(double(Ib).*F((iMin:iMax)-i+hw+1,(jMin:jMax)-j+hw+1)));
  50.         end
  51.     end
  52. end
滤波效果
常用去噪之均值滤波-LMLPHP
08-29 23:42