%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Jingting Zeng (Nikki)
% Homework 6: image compression in FD
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function main

%use different image
a = imread('dog.jpg');
% a = imread('ship.jpg');
% a = imread('house.jpg');
% a = imread('moon.jpg');
a = imresize(a, [600 800]);
a=im2double(a);
b=rgb2gray(a);
figure;
imshow(b);
title('before');
%frequency domain transformation
F=fft2(b);

%show the "black background and white dot"
% figure;imshow(fftshift(imageNormalize(log(abs(F)+1))));

%highlight higher frequency 

%%(wrong code for misunderstanding request)
% len=size(F);
% F(len(1)/2:len(1),len(2)/2:len(2))=F(len(1)/2:len(1),len(2)/2:len(2))*2;

h=fspecial('gaussian',[3 3],0.65);
fil=freqz2(h,[600 800]);
fil=fil+0.89; %increase the value 
F=F.*fil;

%compress image
for i=1:5:100
    FC=F.*(abs(F)>i);
    x=abs(F);
    y=abs(FC);
    re=std(x(:)-y(:));
    %use the value 15 as standard
    if re>=15
        break;
    end
end
%spatial domain transformation
f=ifft2(FC);
figure;
imshow(f);
title('after');
return;


function p=imageNormalize(p)
    p=im2double(p);
    minp=min(p(:));maxp=max(p(:));
    p=(p-minp)/(maxp-minp);
return