function Lab02_SpatialFilters_c()
    clc; clear; close all;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %   Spatal filtering 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      
    
    %% Average, gaussian and median filter ********************************************
    clc; clear; close all;
        
    Img = imread('eighttif.png');    
    Img = im2double(Img(:,:,1));
    % Add noise
    Img_spn = imnoise(Img,'salt & pepper',0.1); % noise density
    Img_gn = imnoise(Img,'gaussian',0,0.01); % M,V
        
    % Kernels and filtering 
    h_b = fspecial('average',5); % hsize
    Img_spn_boxF = imfilter(Img_spn,h_b,'symmetric','corr');
    Img_gn_boxF = imfilter(Img_gn,h_b,'replicate','corr');
    
    h_g = fspecial('gaussian',5,1); % hsize,sigma   
    Img_spn_gaussianF = imfilter(Img_spn,h_g,'replicate','corr');
    Img_gn_gaussianF = imfilter(Img_gn,h_g,'circular','corr');
    
       
    Img_spn_medF = medfilt2(Img_spn,[3 3],'symmetric' ); %Img,  [Neighborhood size], 'Padding'
    Img_gn_medF = medfilt2(Img_gn,[3 3],'symmetric' );
   
    nr = 2;
    nc = 4;    
    figure('Name','Some filters ','NumberTitle','off');
    subplot(nr,nc,1); imshow(Img_spn,[0,1]); title('Img salt & pepper noise'); 
    subplot(nr,nc,2); imshow(Img_spn_boxF,[0,1]); title({'k',['average']}); 
    subplot(nr,nc,3); imshow(Img_spn_gaussianF,[0,1]); title({'k',['gaussian']}); 
    subplot(nr,nc,4); imshow(Img_spn_medF,[0,1]); title({'k',['median']}); 
    
    subplot(nr,nc,5); imshow(Img_gn,[0,1]); title('Img gaussian noise'); 
    subplot(nr,nc,6); imshow(Img_gn_boxF,[0,1]); title({'k',['average']}); 
    subplot(nr,nc,7); imshow(Img_gn_gaussianF,[0,1]); title({'k',['gaussian']}); 
    subplot(nr,nc,8); imshow(Img_gn_medF,[0,1]); title({'k',['median']}); 
      
    %%%%% ******************************************** Solutions ******************************************** %%%%%
    %% Implement the general filter equation (correlation)   ********************************************
    clc; clear; close all;
        
    Img = imread('eighttif.png');    
    Img = im2double(Img(:,:,1));
    % Add noise
    Img_spn = imnoise(Img,'salt & pepper',0.1); % noise density
    Img_gn = imnoise(Img,'gaussian',0,0.01); % M,V
        
    % Kernels and filtering 
    h_b = fspecial('average',5); % hsize
    Img_spn_boxF = Correlation_kernel(Img_spn,h_b);
    Img_gn_boxF = Correlation_kernel(Img_gn,h_b);
    
    h_g = fspecial('gaussian',5,1); % hsize,sigma   
    Img_spn_gaussianF = Correlation_kernel(Img_spn,h_g);
    Img_gn_gaussianF = Correlation_kernel(Img_gn,h_g);
    
       
    Img_spn_medF = medfilt2(Img_spn,[3 3],'symmetric' ); %Img,  [Neighborhood size], 'Padding'
    Img_gn_medF = medfilt2(Img_gn,[3 3],'symmetric' );
   
    nr = 2;
    nc = 4;    
    figure('Name','Some filters ','NumberTitle','off');
    subplot(nr,nc,1); imshow(Img_spn,[0,1]); title('Img salt & pepper noise'); 
    subplot(nr,nc,2); imshow(Img_spn_boxF,[0,1]); title({'k',['average']}); 
    subplot(nr,nc,3); imshow(Img_spn_gaussianF,[0,1]); title({'k',['gaussian']}); 
    subplot(nr,nc,4); imshow(Img_spn_medF,[0,1]); title({'k',['median']}); 
    
    subplot(nr,nc,5); imshow(Img_gn,[0,1]); title('Img gaussian noise'); 
    subplot(nr,nc,6); imshow(Img_gn_boxF,[0,1]); title({'k',['average']}); 
    subplot(nr,nc,7); imshow(Img_gn_gaussianF,[0,1]); title({'k',['gaussian']}); 
    subplot(nr,nc,8); imshow(Img_gn_medF,[0,1]); title({'k',['median']}); 
      
    
    %%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %    Fourier Transform
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    clc; clear; close all;
    Img = imread('eighttif.png'); 
    Img = im2double(Img(:,:,1));
    
    Img_ff = fft2(Img);
    Img_ff_s = fftshift(Img_ff);
    Img_iff = real(ifft2(Img_ff));
    
    
    nr = 1;
    nc = 4;       
    figure('Name','FFT - IFFT ','NumberTitle','off');
    subplot(nr,nc,1); imshow(Img,[0,1]); title('Img original'); 
    subplot(nr,nc,2); imshow(Img_ff,[0,1]); title('fft'); 
    subplot(nr,nc,3); imshow(Img_ff_s,[0,1]); title('fft shift'); 
    subplot(nr,nc,4); imshow(Img_iff,[0,1]); title('ifft'); 
    
    
    
    
    %% Implement filtering in Fourier space   ********************************************
    clc; clear; close all;
    Img = imread('eighttif.png'); 
    Img = im2double(Img(:,:,1));
    [m,n] = size(Img); 

    Img_ff = fftshift(fft2(Img));

    hf = 25;
    mask_LowPass = zeros(m,n);
    mask_LowPass(m/2-hf:m/2+hf,n/2-hf:n/2+hf) = 1;

    mask_HighPass = ones(m,n);
    mask_HighPass(m/2-hf:m/2+hf,n/2-hf:n/2+hf) = 0;
    % filtering
    Img_ff_lp = Img_ff.*mask_LowPass;
    Img_ff_hp = Img_ff.*mask_HighPass;

    Img_iff = ifft2(ifftshift(Img_ff));
    Img_iff_lp = ifft2(ifftshift(Img_ff_lp));
    Img_iff_hp = ifft2(ifftshift(Img_ff_hp));

    nr = 2;
    nc = 4;       
    subplot(nr,nc,1); imshow(Img,[0,1]); title('Img original'); 

    subplot(nr,nc,2); imshow(Img_ff,[0,1]); title('fft'); 
    subplot(nr,nc,3); imshow(Img_ff_lp,[0,1]); title('fft Low Pass'); 
    subplot(nr,nc,4); imshow(Img_ff_hp,[0,1]); title('fft High Pass'); 

    subplot(nr,nc,6); imshow(Img_iff,[0,1]); title('ifft'); 
    subplot(nr,nc,7); imshow(Img_iff_lp,[0,1]); title('ifft Low Pass'); 
    subplot(nr,nc,8); imshow(Img_iff_hp,[0,1]); title('ifft High Pass'); 

   
    %%   
    


end 
