% read in MATLAB demo text image
image = imread('text.png');
[M N] = size(image)

% Show Image

figure(1)
imshow(image)

% compute fft and display its spectra

F=fft2(double(image));
figure(2)
imshow(abs(fftshift(F))/256);

%compute Ideal Butterworth Pass Filter

u0 = 50; % set cut off frequency


u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);

for i = 1: M
    for j = 1:N
      %Apply a 2nd order Butterworth  
      UVw = double((U(i,j)*U(i,j) + V(i,j)*V(i,j))/(u0*u0));
      
      H(i,j) = 1/(1 + UVw*UVw);
    end
end    
% display
figure(3)
imshow(fftshift(H))

% Apply filter and do inverse FFT
G=H.*F;
g=real(ifft2(double(G)));

% Show Result

figure(4)
imshow(g)