You are on page 1of 27

Frequency Domain Filtering

Frequency filtering is based on the Fourier Transform. The operator


usually takes an image and a filter function in the Fourier domain.
This image is then multiplied with the filter function in a pixel-bypixel fashion:

Where F(k,l) is the input image in the Fourier domain, H (k,l) the
filter function and G(k,l) is the filtered image. To obtain the resulting
image in the spatial domain,
G ( k , l ) has to be re-transformed using the inverse Fourier
Transform. Since the multiplication in the Fourier space is identical
to convolution in the spatial domain, all frequency filters can in
theory be implemented as a spatial filter. However, in practice, the
Fourier domain filter function can only be approximated by the
filtering kernel in spatial domain.
I

Frequency Domain Bandpass Filters

A bandpass attenuates very low and very high frequencies, but


retains a middle range band of frequencies. Bandpass filtering can
be used to enhance edges (suppressing low frequencies) while
reducing the noise at the same time (attenuating high frequencies).
We obtain the filter function of a bandpass by multiplying the filter
functions of a low pass and of a high pass in the frequency domain,
where the cutoff frequency of the low pass is higher than that of the
high pass. Therefore, in theory, one can derive a bandpass filter
function if the low pass filter function is available. Bandpass filtering
is attractive, but there is always a trade-off between blurring and
noise: low pass reduces noise, but accentuates blurring, high pass
reduces blurring but accentuates noise.
1. Ideal Bandpass Filter
The ideal bandpass filter passes only frequencies within the pass
band and gives an output in the spatial domain that is in most cases
blurred and/or ringed. It is the easiest bandpass filter to simulate
but its vertical edges and sharp corners are not realizable in the
physical world. An ideal bandpass filter with a frequency range of
[DL,DH] is defined as follows:

The following example demonstrates the effect of applying an ideal


bandpass filter with cutoff frequencies of 80 and 180 to a grey scale
512512 image.

Original Image

Filtered Image

Frequency Spectrum
of Image

Frequency domain filter


function

As expected, the ideal filter has the effect of ringing and blurring on
the original image. The associated Matlab code is given below:
clear all;
close all;
clc
ima=imread('lena.bmp');
ima = double(ima);
figure (1)
imshow(ima,[]);
imafft = fftshift(fft2(fftshift(ima)));
% Fourier Spectrum of Image
imafft2 = fft2(ima);
imafft3 = fftshift(imafft2);
s = size(ima);
ma=max(max((imafft)));
maxr = 0.5*sqrt(s(1)^2+s(2)^2);
cutoff1 = maxr*80;

cutoff2 = maxr*180;
c=1;
for i = 1 : s(1)
for j = 1 : s(2)
r = sqrt((i-1-s(1)/2)^2+(j-1-s(2)/2)^2);
if( r < 80)
z(i,j) = 0;
else
if( r > 180)
z(i,j) = 0;
else
z(i,j) =255;
end
end
end
end
% Plots
figure (2)
imafft=imafft.*z/255;
ima_out = fftshift(ifft2(fftshift(imafft)));
ima_out =ima_out-ima;
fftshow(ima_out,'abs');
figure (3)
fftshow(imafft3,'log');
figure (4)
fftshow(z,'log');
2. Butterworth Bandpass Filter
This filter can be derived mathematically by multiplying the transfer
functions of a a low andhigh pass filter. The low pass filter will have
the higher cut off frequency.

Where Dl and DH are the cut frequencies of the low and high pass
filters respectively; n is the order of the filter and D(u,v) is the
distance from the origin.
The Butterworth filter has a smooth transfer function without any
discontinuity or clear cut off frequency. The range of frequencies

that the filter allows is largely dependent on the order of the filter. In
the selection of n there has to be a compromise between the
demands

of

the

frequency

domain

(sharp

cutoff)

and

the

spatial domain (rapid decay). The example presented in this section


demonstrates the effect of applying a fourth order Butterworth filter
with cut off frequencies 80 and 180 to a 512512 grey scale image.

Original Image

Filtered Image

Frequency Spectrum
of Image

Frequency domain filter


function

The result shows that the ringing and blurring effect that was
observed with the ideal bandpass filter does not exist for
the Butterworth filter. The Matlab code is presented below:
clear all;
close all;
clc
micro = imread('lena.bmp');
micro = double(micro);
[nx ny] = size(micro);
u = micro;
micro = uint8(u);
imwrite(micro,'lena1.bmp');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
figure(1)
imshow(micro,[]);
figure(2)
fftshow(fftu,'log')

% Initialize filter.
filter1 = ones(2*nx-1,2*ny-1);
filter2 = ones(2*nx-1,2*ny-1);
filter3 = ones(2*nx-1,2*ny-1);
n = 4;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
% Use Butterworth filter.
filter1(i,j)= 1/(1 + (dist/180)^(2*n));
filter2(i,j) = 1/(1 + (dist/80)^(2*n));
filter3(i,j)= 1.0 - filter2(i,j);
filter3(i,j) = filter1(i,j).*filter3(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
3. Gaussian Bandpass Filter
The Gaussian filter out-performs the previously discussed filter types
because when a Gaussian is transformed between the frequency
and spatial domains, it remains a Gaussian; and therefore does not
incur the ringing effect in the spatial domain of the filtered image.
Again the derivation of a Gaussian bandpass filter starts from the
low pass filter.

Where DL ,DH are the cut frequencies of the low and high pass
filters respectively; D(u,v) is the distance from the origin. The
example below shows that the Gaussian bandpass filter is the best
among the three discussed filters

Original Image

Filtered Image

Frequency Spectrum
of Image

The Matlab code is presented below:


clear all;
close all;
clc
micro = imread('lena.bmp');
micro = double(micro);
[nx ny] = size(micro);
u = micro;
micro = uint8(u);
imwrite(micro,'lena1.bmp');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
figure(1)
imshow(micro,[]);
figure(2)
fftshow(fftu,'log')
% Initialize filter.
filter1 = ones(2*nx-1,2*ny-1);

Frequency domain filter


function

filter2 = ones(2*nx-1,2*ny-1);
filter3 = ones(2*nx-1,2*ny-1);
n = 4;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
% Use Gaussian filter.
filter1(i,j) = exp(-dist^2/(2*180^2));
filter2(i,j) = exp(-dist^2/(2*80^2));
filter3(i,j) = 1.0 - filter2(i,j);
filter3(i,j) = filter1(i,j).*filter3(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
II

Frequency Domain low pass Filters


A filter that attenuates high frequencies while passing the low
frequencies.

Low

frequencies

represent

the

gray-level

appearance of an image over smooth areas.


1. Ideal low pass Filter
1 if D(u , v) D0
H (u, v)
0 if D(u , v) D0

D(u,v) is the distance from the origin, D0 is the cutoff frequency. The
following example demonstrates the effect of applying an ideal
lowpass filter with cutoff frequency 80 a grey scale 512512 image.

Original Image

Filtered Image

Frequency Spectrum
of Image

The associated Matlab code is given below:


clear all;
close all;
clc
ima=imread('lena.bmp');
ima = double(ima);
figure (1)
imshow(ima,[]);
imafft = fftshift(fft2(fftshift(ima)));
% Fourier Spectrum of Image
imafft2 = fft2(ima);
imafft3 = fftshift(imafft2);
s = size(ima);
[M N]=size(ima);
ma=max(max((imafft)));
maxr = 0.5*sqrt(s(1)^2+s(2)^2);
cutoff1 = maxr*80;
Imf=fft2(ima);
D0=80

Frequency domain filter


function

for i=1:M
for j=1:N
D=sqrt(i^2+j^2);
if D<D0
H(i,j)=1;
else
H(i,j)=0;
end
G(i,j)=Imf(i,j)*H(i,j);
end
end
F=ifft2(G);
% Plots
figure (2)
imafft=imafft.*G/255;
ima_out = fftshift(ifft2(fftshift(imafft)));
imshow(F,[]);
figure (3)
fftshow(imafft3,'log');
figure (4)
fftshow(G,'log');
H (u , v)
2.

1
1 [ D(u , v) / D0 ]2 n

Butterworth lowpass Filter

D(u,v) is the distance from the origin, D 0 is the cutoff frequency. n is


the order of the filter. The following example demonstrates the
effect of applying an ideal lowpass filter with cutoff frequency 80
a grey scale 512512 image.

Original Image

Filtered Image

Frequency Spectrum
of Image

The associated Matlab code is given below:


clear all;
close all;
clc
micro = imread('lena.bmp');
micro = double(micro);
[nx ny] = size(micro);
u = micro;
micro = uint8(u);
imwrite(micro,'lena1.bmp');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
figure(1)
imshow(micro,[]);
figure(2)
fftshow(fftu,'log')
% Initialize filter.
filter1 = ones(2*nx-1,2*ny-1);
filter2 = ones(2*nx-1,2*ny-1);

Frequency domain filter


function

filter3 = ones(2*nx-1,2*ny-1);
n = 2;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;.
filter1(i,j)= 1/(1 + (dist/80)^(2*n));
filter3(i,j) = filter1(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
H (u , v) e D

3.

( u , v ) / 2 D02

Gaussian lowpass Filter

D(u,v) is the distance from the origin, D0 is the cutoff frequency. The
following example demonstrates the effect of applying an ideal
lowpass filter with cutoff frequency 80 a grey scale 512512 image.

Original Image

Filtered Image

Frequency Spectrum
of Image

The associated Matlab code is given below:


clear all;
close all;
clc
micro = imread('lena.bmp');
micro = double(micro);
[nx ny] = size(micro);
u = micro;
micro = uint8(u);
imwrite(micro,'lena1.bmp');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
figure(1)
imshow(micro,[]);
figure(2)
fftshow(fftu,'log')
% Initialize filter.

Frequency domain filter


function

filter1 = ones(2*nx-1,2*ny-1);
filter2 = ones(2*nx-1,2*ny-1);
filter3 = ones(2*nx-1,2*ny-1);
n = 4;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
% Use Gaussian filter.
filter1(i,j) = exp(-dist^2/(2*80^2));
filter3(i,j) = filter1(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
III

Frequency Domain high pass Filters


A filter that attenuates low frequencies while passing the high
frequencies. High frequencies represents the details such as
edges and noise.
1. Ideal highpass Filter

0
1

H (u , v )

if D (u, v) D0
if D(u , v) D0

D(u,v) is the distance from the origin, D0 is the cutoff frequency. The
following example demonstrates the effect of applying an ideal
lowpass filter with cutoff frequency 80 a grey scale 512512 image.

Original Image

Filtered Image

Frequency Spectrum
of Image

The associated Matlab code is given below:


clear all;
close all;
clc
ima=imread('lena.bmp');
ima = double(ima);
figure (1)
imshow(ima,[]);
imafft = fftshift(fft2(fftshift(ima)));
% Fourier Spectrum of Image
imafft2 = fft2(ima);
imafft3 = fftshift(imafft2);
s = size(ima);
ma=max(max((imafft)));
maxr = 0.5*sqrt(s(1)^2+s(2)^2);
cutoff1 = maxr*0;
cutoff2 = maxr*80;
c=1;
for i = 1 : s(1)

Frequency domain filter


function

for j = 1 : s(2)
r = sqrt((i-1-s(1)/2)^2+(j-1-s(2)/2)^2);
if(r>=80)
z(i,j) = 1;
else
z(i,j) = 0;
end
end
end
figure (2)
imafft=imafft.*z/255;
F=ifft2(imafft);
imshow(F);
ima_out = fftshift(ifft2(fftshift(imafft)));
ima_out =ima_out-ima;
figure (3)
fftshow(imafft3,'log');
figure (4)
fftshow(z,'log');
2. Butterworth highpass Filter
H (u, v)

1
1 [ D0 / D(u , v)]2 n

D(u,v) is the distance from the origin, D 0 is the cutoff frequency. n is


the order of the filter. The following example demonstrates the
effect of applying an ideal lowpass filter with cutoff frequency 80
a grey scale 512512 image.

Original Image

Frequency Spectrum
of Image
The associated
Matlab code is given below:
clear all;
close all;
clc

Filtered Image

Frequency domain filter


function

micro = imread('lena.bmp');
micro = double(micro);
[nx ny] = size(micro);
u = micro;
micro = uint8(u);
imwrite(micro,'lena1.bmp');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
figure(1)
imshow(micro,[]);
figure(2)
fftshow(fftu,'log')
% Initialize filter.
filter1 = ones(2*nx-1,2*ny-1);
filter2 = ones(2*nx-1,2*ny-1);
filter3 = ones(2*nx-1,2*ny-1);
n = 2;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
% Use Butterworth filter.
filter1(i,j)= 1/(1 + (80/dist)^(2*n));
filter3(i,j) = filter1(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
3. Gaussian highpass Filter

H (u , v) 1 e D

( u , v ) / 2 D02

D(u,v) is the distance from the origin, D0 is the cutoff frequency. The
following example demonstrates the effect of applying an ideal
lowpass filter with cutoff frequency 80 a grey scale 512512 image.

Original Image

Filtered Image

Frequency Spectrum
of Image

The associated Matlab code is given below:


clear all;
close all;
clc
micro = imread('lena.bmp');
micro = double(micro);
[nx ny] = size(micro);
u = micro;
micro = uint8(u);
imwrite(micro,'lena1.bmp');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
figure(1)
imshow(micro,[]);
figure(2)
fftshow(fftu,'log')
% Initialize filter.
filter1 = ones(2*nx-1,2*ny-1);

Frequency domain filter


function

filter3 = ones(2*nx-1,2*ny-1);
n = 4;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
filter2(i,j) = 1-exp(-dist^2/(2*80^2));
filter3(i,j) = 1.0 - filter2(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])

You might also like