%% box mask
clear all
close all
img = rgb2gray(imread('lena.jpg'));

figure;
subplot(1,1,1)
imshow(img,[])
title('img','FontSize',14,'fontweight','bold')

I =fftshift(fft2(img));


% hight and low frequency
square = ones(size(img));
square(257-51:257+50,257-51:257+50)=0;

figure;
subplot(1,2,1)
imshow(1.0-square,[])
title('box mask','FontSize',14,'fontweight','bold')
subplot(1,2,2)
imshow(square,[])
title('1.0 - box mask','FontSize',14,'fontweight','bold')


I_high = I.*square;
figure;
subplot(2,2,1)
imshow(log(abs(I_high)),[])
title('high frequencies','FontSize',14,'fontweight','bold')


I_low = I.*(1-square);
subplot(2,2,2)
imshow(log(abs(I_low)),[])
title('low frequencies','FontSize',14,'fontweight','bold')

img_high = real(ifft2(ifftshift(I_high)));
img_low= real(ifft2(ifftshift(I_low)));

subplot(2,2,3)
imshow(img_high,[])
subplot(2,2,4)
imshow(img_low,[])

%% Box and sinc
t = -5:0.1:5;

T = 2;
bf = ones(1,length(t));
bf(abs(t)> T/2) = 0;

w= -10:0.1:10;

sinc = T* sin(pi*w*T)./(pi*w*T);

figure;
subplot(1,2,1)
plot(t,bf)
xlabel('frequency (Hz)','FontSize',14)
title('frequency mask','FontSize',14,'fontweight','bold')
axis([-5,5,-0.5,2])
subplot(1,2,2)
plot(w,sinc)
xlabel('time (s)','FontSize',14)
title('IFT','FontSize',14,'fontweight','bold')

%% gaussian mask
clear all
close all
img = rgb2gray(imread('lena.jpg'));

figure;
subplot(1,1,1)
imshow(img,[])
title('img','FontSize',14,'fontweight','bold')

I =fftshift(fft2(img));


% hight and low frequency
[x,y] = size(I);
alpha = 0.0001;
g_mask = my_gaussian_filter(x,y,alpha);
figure;
subplot(1,2,1)
imshow(g_mask,[])
title('gaussian mask','FontSize',14,'fontweight','bold')
subplot(1,2,2)
imshow(1-g_mask,[])
title('1.0 - gaussian mask','FontSize',14,'fontweight','bold')
I_high = I.*(1-g_mask);

figure;
subplot(2,2,1)
imshow(log(abs(I_high)),[])
title('high frequencies','FontSize',14,'fontweight','bold')


I_low = I.*g_mask;
subplot(2,2,2)
imshow(log(abs(I_low)),[])
title('low frequencies','FontSize',14,'fontweight','bold')

img_high = real(ifft2(ifftshift(I_high)));
img_low= real(ifft2(ifftshift(I_low)));

subplot(2,2,3)
imshow(img_high,[])
subplot(2,2,4)
imshow(img_low,[])


%% gaussian filter
clear all
close all
img = rgb2gray(imread('lena.jpg'));

h = fspecial('gaussian',11,10.0)

img_gaus = imfilter(img,h);
figure
subplot(1,3,1)
imshow(img,[])
title('original','FontSize',14,'fontweight','bold')

subplot(1,3,2)
imshow(img_gaus,[])
title('gaussian filtered','FontSize',14,'fontweight','bold')

subplot(1,3,3)
imshow(img-img_gaus,[])
title('original-gaussian filtered','FontSize',14,'fontweight','bold')

%% dog filter
clear all
close all
img = rgb2gray(imread('lena.jpg'));

h = fspecial('gaussian',11,10.0)

img_gaus = imfilter(img,h);
figure
subplot(1,3,1)
imshow(img,[])
title('original','FontSize',14,'fontweight','bold')

subplot(1,3,2)
imshow(img_gaus,[])
title('gaussian filtered','FontSize',14,'fontweight','bold')

subplot(1,3,3)
imshow(img-img_gaus,[])
title('original-gaussian filtered','FontSize',14,'fontweight','bold')

