clear all
% input parametrs:
n = 80; Do = 20; %filter parameters


%load image
imrgb = imread('pattern2_cropped.png');
im = im2double(rgb2gray(imrgb));

%size of image
[im_height,im_width] = size(im);

%euclidean distance matrix
[U,V] = rc2uv(im_height,im_width);
D = sqrt(U.^2+V.^2); 

%generate butterworth filter from euclidean distance matrix
H = 1./(1+(D./Do).^(2*n));

%spectra of image
IM = fftshift(fft2(im));

%filter image with lowpass butterw (matrix dot product)

IMF=IM.*H;

%image reconstruction from spectra IMF (inverse fft)

imf = ifft2(fftshift(IMF));

%print output:
figure(1); clf;


subplot(1,2,1);
%print input figure
imshow(im);
title('original figure');

%print output figure
subplot(1,2,2);
imshow(imf);
title(sprintf('filtered figure with lowpass butterw. order=%d,D=%d',n,Do));

%print butter filter
figure(2);
colormap jet; colorbar;
mesh(H);
title(sprintf('Lowpass Butterworth filter, param order=%d,diam(D)=%d',n,Do));


