% Chapter 3.5.2
f=imread('circuitboard.tif');        %  
imshow(f)                           %Show original 
pause

% Add salt and pepper noise
fn=imnoise(f,'salt & pepper', 0.2);
figure,imshow(fn)               %Show noisy image
pause

gm=medfilt2(fn);
figure,imshow(gm)     % Show median filtered image
pause               

gms=medfilt2(fn,'symmetric');
figure, imshow(gms)    % Improve border filtering 
pause

m=3;n=3;
g=ordfilt2(fn,median(1:m*n),ones(m,n));
figure,imshow(g)    % Median filtering using ordfilt2
pause

gmax=ordfilt2(f,m*n,ones(m,n));
figure,imshow(gmax)  % Max-filtering using ordfilt2
pause

gmin=ordfilt2(f,1,ones(m,n));
figure,imshow(gmin)  % Min filtering using ordfilt2
pause

gdiff=gmax-gmin;
figure,imshow(gdiff)  % Edge enhancement using Max - Min
pause

close all