 
% im = imread('ork.jpg'); 
im = imread('cof2.jpg'); 
% im = imread('sandman.jpg');  
% im = imread('cthulhu.jpg'); 
% im = imread('cthulhu2.jpg'); 

clear s
clear q
clear hp

im = sum(im,3);
im = im/max(im(:));

x = size(im,2);
y = size(im,1);

for i=1:(x/2)
    for j=1:(y/2)
        hp(i,j) = 1./(1+exp(-((i-(x/4))+(j-(y/4)))/((x+y)/20)));
    end
end

if (mod(x,2) > 0)
    i=floor(x/2)+1;
    for j=1:(y/2)
        hp(i,j) = 1./(1+exp(-((i-(x/4))+(j-(y/4)))/((x+y)/20)));
    end
end

if (mod(y,2) > 0)
    j=floor(y/2)+1;
    for i=1:size(hp,1)
        hp(i,j) = 1./(1+exp(-((i-(x/4))+(j-(y/4)))/((x+y)/20)));
    end
end

hp = hp';
hp = [hp fliplr(hp(:,1:floor(x/2)))];
hp = [hp; flipud(hp(1:floor(y/2),:))];

ft = fft2(im);
ft = ft .* hp;

s(1,:)=std(abs(ft));
q(1)=sum(std(abs(ft)));
m(1,:) = mean(abs(ft));

clear im

fc=ft.*(abs(ft)>1);
ift = ifft2(fc);
s(2,:)=std(abs(fc));
q(2)=sum(std(abs(fc)));
m(2,:) = mean(abs(fc));
%subplot(2,3,1);
imshow(abs(ift));
pause;

for T=1:20
    fc=ft.*(abs(ft)>T*10);
    ift = ifft2(fc);
    s(T+2,:)=std(abs(fc));
    q(T+2)=sum(std(abs(fc)));
    m(T+2,:) = mean(abs(fc));
    if ((q(1) - q(T+2))/q(1) > 0.05)
        if (T > 1)
            fprintf('Best threshold is: %d \n',(T-1)*10);break;
        else
            fprintf('Best threshold is: 1 \n');break;
        end
    end
%    subplot(2,3,T+1);
    imshow(abs(ift));
    pause;
end
