

f=imread('MRInoise.png');
[rows,columns]=size(f);
g=zeros(rows, columns);
m=zeros(rows, columns);

% g will be the filtered image
w=ones(3)/9;
%This is a 3x3 averaging(smooth) filter
%g is smoothing with correlation algorithm
%m is median filtering
for x=2:rows-1
  for y=2:columns-1
  
  g(x,y) = w(1,1)*f(x-1,y-1)+ w(1,2)*f(x-1,y) + w(1,3)*f(x-1,y+1)+ w(2,1)*f(x,y-1)+ w(2,2)*f(x,y) + w(2,3)*f(x,y+1)+ w(3,1)*f(x+1,y-1)+w(3,2)*f(x+1,y)+ w(3,3)*f(x+1,y+1); 
  v=[f(x-1,y-1),f(x-1,y),f(x-1,y+1),f(x,y-1),f(x,y),f(x,y+1),f(x+1,y-1),f(x+1,y),f(x+1,y+1)];
  m(x,y)=median(v);
  end
end

% To visualise the image info lost after filtering
figure, imshow(uint8(10*(f-g)))
figure, imshow(uint8(10*(f-m)))