均值滤波是用像素点邻域的均值代替该像素点,通过将各个像素点做平均平滑滤波,平滑效果与滤波窗口有关,窗口越大,平滑效果越强,但细节和边缘也越加模糊。
matlab实现
滤波效果
matlab实现
点击(此处)折叠或打开
- %均值滤波 test
- A = imread('E:\test\Lenna.jpg');
- B = imnoise(A,'gaussian');
- figure;subplot(221),imshow(A);title('原图')
- subplot(222),imshow(B);title('高斯噪声图')
- C = m_average_filter(B,5);
- subplot(223),imshow(uint8(C));title('均值滤波后')
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Purpose: do averge filter
- % Author: pottor
- % Date: 2017/5/10
- function out = m_average_filter(in,w)
- if( mod(w,2)==0 )
- error( 'w must be odd' );
- end
- out = in;
- hw = floor(w/2);
- F = ones(w,w)/(w*w);
- if size(in,3) == 1
- [m,n] = size(in);
- for i = 1:m
- for j = 1:n
- iMin = max(i-hw,1);
- iMax = min(i+hw,m);
- jMin = max(j-hw,1);
- jMax = min(j+hw,n);
-
- I = in(iMin:iMax,jMin:jMax);
- out(i,j) = sum(sum(double(I).*F((iMin:iMax)-i+hw+1,(jMin:jMax)-j+hw+1)));
- end
- end
- else
- r = in(:,:,1);
- g = in(:,:,2);
- b = in(:,:,3);
- [m,n] = size(r);
- for i = 1:m
- for j = 1:n
- iMin = max(i-hw,1);
- iMax = min(i+hw,m);
- jMin = max(j-hw,1);
- jMax = min(j+hw,n);
-
- Ir = r(iMin:iMax,jMin:jMax);
- Ig = g(iMin:iMax,jMin:jMax);
- Ib = b(iMin:iMax,jMin:jMax);
- out(i,j,1) = sum(sum(double(Ir).*F((iMin:iMax)-i+hw+1,(jMin:jMax)-j+hw+1)));
- out(i,j,2) = sum(sum(double(Ig).*F((iMin:iMax)-i+hw+1,(jMin:jMax)-j+hw+1)));
- out(i,j,3) = sum(sum(double(Ib).*F((iMin:iMax)-i+hw+1,(jMin:jMax)-j+hw+1)));
- end
- end
- end