clear; clc; close all;

pic = imread('travel_frog.jpg');
A = rgb2gray(pic);
A_fourier = fftshift(fft2(A));

centerX = size(A_fourier, 2) / 2;
centerY = size(A_fourier, 1) / 2;
sig = 70;

G = @(x, y) exp(-((x - centerX) .^ 2 + (y - centerY) .^ 2) / 2 / sig ^ 2);

[X, Y] = meshgrid(1 : size(A_fourier, 2), 1 : size(A_fourier, 1));

g = G(X, Y);
B = A_fourier .* g;
C = A_fourier .* (1 - g);

% for i = 1 : size(A_fourier, 1)
%     for j = 1 : size(A_fourier, 2)
%         g(i, j) = G(i, j);
%         B(i, j) = A_fourier(i, j) * g(i, j); 
%         C(i, j) = A_fourier(i, j) * (1 - g(i, j)); 
%     end
% end

output_LP = uint8(abs(ifft2(fftshift(B))));
output_HP = uint8(abs(ifft2(fftshift(C))));
%%

figure('Position', [300 100 500 600]);
subplot(2, 2, 1); imshow(A); title('Origin');
subplot(2, 2, 2); imshow(uint8(abs(A_fourier))); title('DFT of Origin');
subplot(2, 2, 3); imshow(g); title('Low-pass Filter (frequency domain)');
subplot(2, 2, 4); imshow(uint8(abs(B))); title('Multiplication');
figure; imshow(output_LP); title('Blurring (LP)');
output_HP(output_HP < 10) = 255;
figure; imshow(output_HP); title('Edge Detection (HP)');