%CS445 Proj.2 Solution
%prob.1
%read in file
img = imread('cameraman','tif');
img = im2double(img);
%get size of pic
[height, width] = size(img)
%set noise levels
noise = [0.05,0.10,0.20];
%initialize data arrays

NimgMSE = zeros(3,11);
%cycle for each noise level
for i = 1:3    
%get noisy image    
Nimg = imnoise(img,'salt & pepper', noise(i));    
%get MSE for Noisy image    
NimgMSE(i,1) = MSError(img, Nimg);    
%cycle through median filter iteration    
for j = 1:10        
    tempNimg = Nimg;       
 %median filter        
 Nimg = medfilt2(Nimg);        
%find difference        
tempNimg = tempNimg - Nimg;       

%Fraction of pixels changed       
a = (tempNimg ~= 0);
%pause
ChangePix(i, j) = sum(a(:))/(height*width);
        
%find MSE for filtered image        
NimgMSE(i,j+1) = MSError(Nimg,img);    
end
end
%output
%ChangePix'NimgMSE'

plot([1:10],ChangePix(1,:),[1:10],ChangePix(2,:),[1:10],ChangePix(3,:))
xlabel('median filter iterations')
ylabel('fraction of pixels changed')
pause;
plot([0:10],NimgMSE(1,:),[0:10],NimgMSE(2,:),[0:10],NimgMSE(3,:))
xlabel('median filter iterations')
ylabel('MSE');


