% Lab3a Spring 2005   Terje Natås
% Define "image" f:
f=[ 1 1 1 1 15 15 15 15;
    1 1 1 1 15 15 15 15; 
    1 1 1 1 15 15 15 15; 
    1 1 1 1 15 15 15 15; 
    1 1 1 1 15 15 15 15; 
    1 1 1 1 15 15 15 15; 
    1 1 1 1 15 15 15 15; 
    1 1 1 1 15 15 15 15];
[M N]=size(f)           % Dimensions:  M rows and N columns 
g=f;                    % Define output image g 
%  Define 3x3 mask
w=[1 1 1;1 1 1;1 1 1 ]/9;   % Averaging filter
% Go through image pixel by pixel:
% ( Borderpixels are left unfiltered ) 
for x=2:M-1
    for y=2:N-1
       % Operate on current pixel: 
       g(x,y)=0;        % Start summing up from 0
       for i=-1:1
           for j=-1:1
               % i=j=-1 makes first mask array position: w(1,1) 
               g(x,y)=g(x,y)+f(x+i,y+j)*w(i+2,j+2);
           end
       end              
    end
end
g                   % Show array result

