%Question No:3

%Write a program which implements all the types of linear spatial filters
%using MATLAB functions.

function spatial(x)
f=imread(x);
f=im2double(f);
[r,c]=size(f);
for i=1:r
    for j=1:c
        ip=i+1;
        im=i-1;
        jm=j-1;
        jp=j+1;
        if(im<1)
            im=i;
        elseif (ip>r)
            ip=i;
        end
        if(jm<1)
            jm=j;
        elseif (jp>c)
            jp=j;
        end
        W=[f(i,j) f(i,jm) f(i,jp) f(ip,j) f(im,j) f(im,jm) f(ip,jm) f(ip,jp) f(im,jp)];
        m=median(W);
        MF(i,j)=m;
        LT(i,j)=(1/2)*(f(i,j)+ (1/4)*(f(i,jm)+f(i,jp)+f(ip,j)+f(im,j)));
        LS(i,j)=(1/9)*(f(i,j)+ f(i,jm)+f(i,jp)+f(ip,j)+f(im,j)+f(im,jm)+f(ip,jm)+f(ip,jp)+f(im,jp));
        HT(i,j)= 5*f(i,j)+ -1*(f(i,jm)+f(i,jp)+f(ip,j)+f(im,j));   
        HS(i,j)= 9*f(i,j)+ -1*(f(i,jm)+f(i,jp)+f(ip,j)+f(im,j)+f(im,jm)+f(ip,jm)+f(ip,jp)+f(im,jp));
        DF(i,j)=((f(im,jm)+f(im,j)+f(im,jp))-(f(ip,jm)+f(ip,j)+f(ip,jp)))+((f(im,jm)+f(i,jm)+f(ip,jm))-(f(im,jp)+f(i,jp)+f(ip,jp)));
        SF(i,j)=((f(im,jm)+2*f(im,j)+f(im,jp))-(f(ip,jm)+2*f(ip,j)+f(ip,jp)))+((f(im,jm)+2*f(i,jm)+f(ip,jm))-(f(im,jp)+2*f(i,jp)+f(ip,jp)));
       end
end
HB=2*f-LS; %High boost = A*Original image - Low pass filter
choice=0;
while (choice~=9)
choice=input('1: Low Pass Spatial Filter - Unequal Weights\n2: Low Pass Spatial Filter - Equal Weights\n3: High Pass Spatial Filter - Less Sharper\n4: High Pass Spatial Filter - More Sharper\n5: High-boost Filtering\n6: Median Filtering\n7: Prewitt Derivative Filter\n8: Sobel Derivative Filter\n9: Exit\n Enter your choice : ');
switch choice
    case 1
        imshow(f), title('Original Image'), figure,imshow(LT), title('Low Pass Spatial Filter - Unequal Weights');
    case 2
       imshow(f),title('Original Image'),figure,imshow(LS), title('Low Pass Spatial Filter - Equal Weights');
    case 3
        imshow(f),title('Original Image'),figure,imshow(HT),title('High Pass Spatial Filter - Less Sharper');
    case 4
        imshow(f),title('Original Image'),figure,imshow(HS),title('High Pass Spatial Filter - More Sharper');
    case 5
        imshow(f),title('Original Image'),figure,imshow(HB),title('High Boosting Filter');
    case 6
        imshow(f),title('Original Image'),figure,imshow(MF),title('Median Filter');
    case 7
        imshow(f),title('Original Image'),figure,imshow(DF),title('Prewitt Derivative Filter');
    case 8
        imshow(f),title('Original Image'),figure,imshow(SF),title('Sobel Derivative Filter');
    case 9
        display('Program Exited');
    otherwise
        error('Wrong Choice');
end
end