lowpass grayscale HOWTO: m-file s-by-s decomposition

m-file: i2.m

Main Idea: transform figure into spectra (2D Fast Fourier Transform), apply freq-domain filter and transform back to "time" or space domain (2D inverse FFT)

clear all

% input filter parametrs, order n, diameter Do (cutoff frequency):

n = 80; Do = 20;

% load image and convert RGB into grayscale

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

%read image:

original image

% get size of image, height, width NOTE: image must be the same in w x h (square for fft!)

[im_height,im_width] = size(im);

% euclidean distance matrix (spec. matrix for cylincric-symetrical filter), such colorized images: use colormap jet;

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

euclidean distance matrix for generation of lin cone

% generate butterworth filter from linear freq. cone (figure above)

H = 1./(1+(D./Do).^(2*n));

butterworth lowpass filter, 2nd order

%spectrum of image, and freq shift (miniumum freq. was on edges, after shift freq. is in the center of matrix)

IM = fftshift(fft2(im));

%power spectrum of image (10log(abs(IM)^2)

power spectrum of picture

%filter image with lowpass butterw (matrix dot product), harmonics ai*filter_gain@ai

IMF=IM.*H;

%power spectrum of filtered image

power spectrum of picture

%image reconstruction from spectra IMF (back spectra shift and inverse fft)

imf = ifft2(fftshift(IMF));

%filtered image:

filtered image

%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));