%Random Noise Reduction
%copyright by hamideh
I1=imread('aav.jpg');
%I1=imread('lenz.bmp','bmp');

I=double(I1);
m=size(I);
%Adding the Random Noise
n=random('unif',-10,10,m(1),m(2));
double(n);
J=I+n;

SUBPLOT(3,1,1), imshow(I,[])
SUBPLOT(3,1,2), imshow(J,[])

%Body Noise Reduction
for i=2:m(1)-1
    for j=2:m(2)-1
        A=J(i-1:i+1,j-1:j+1);
        S=mean(median(sort(A,2),2));
       % S=median(sort(A,2),2);

        Ir(i,j)=S;
    end
end

%Edages Noise Reduction 
if i==1 
    for j=1:m(2)
        B=I(i:i+1,j:j+1);
        S=mean(median(sort(B,2),2));
        Ir(i,j)=S;  
    end
end
if i==m(1) 
    for j=1:m(2)
        B=I(i-1:i,j:j+1);
        S=mean(median(sort(B,2),2));
        Ir(i,j)=S;  
    end
end
if j==1 
    for i=1:m(1)
        B=I(i:i+1,j:j+1);
        S=mean(median(sort(B,2),2));
        Ir(i,j)=S;  
    end
end
if j==m(2) 
   for i=1:m(1)
        B=I(i:i+1,j-1:j);
        Ir(i,j)=S;  
        end
 end
SUBPLOT(3,1,3), imshow(Ir,[])


