%Question No:11

%Write a MATLAB function for smoothing an RGB colour image with a linear
%spatial filter

function rgbspatial(x)
f=imread(x);
f=im2double(f);
r=f(:,:,1);
g=f(:,:,2);
b=f(:,:,3);
[m n]=size(r);
for i=1:m
    for j=1:n
        ip=i+1;
        im=i-1;
        jm=j-1;
        jp=j+1;
        if(im<1)
            im=i;
        elseif (ip>m)
            ip=i;
        end
        if(jm<1)
            jm=j;
        elseif (jp>n)
            jp=j;
        end
        rs(i,j)=(1/9)*(r(i,j)+ r(i,jm)+r(i,jp)+r(ip,j)+r(im,j)+r(im,jm)+r(ip,jm)+r(ip,jp)+r(im,jp));
        gs(i,j)=(1/9)*(g(i,j)+ g(i,jm)+g(i,jp)+g(ip,j)+g(im,j)+g(im,jm)+g(ip,jm)+g(ip,jp)+g(im,jp));
        bs(i,j)=(1/9)*(b(i,j)+ b(i,jm)+b(i,jp)+b(ip,j)+b(im,j)+b(im,jm)+b(ip,jm)+b(ip,jp)+b(im,jp));
       end
end
S=cat(3,rs,gs,bs);
imshow(f),title('Original Image');
figure, imshow(S),title('Smoothened Image');
