function B = Filter (A, r, func)
% Use a radius r filter on A to produce B. The function
% func is applied to the radius r neighborhood of each pixel
% to produce a new pixel value. A is an m-by-n uint8 array.
%
% Usage: B = Filter(A, 2, @MedVal);

[m,n] = size(A);
B = A;
for i=1:m
for j=1:n
iMin = max(1,i-r);
iMax = min(m,i+r);
jMin = max(1,j-r);
jMax = min(n,j+r);
C = A(iMin:iMax,jMin:jMax);
B(i,j) = func(C);
end
end