You are on page 1of 10

EASTERN MEDITERRANEAN UNIVERSITY

Electrical and Electronic Engineering Department


EE 583 (Digital Image Processing)

Home Work IV

PREPARED BY:

LEONARDO O. IHEME
(106054)

SUBMITTED TO:

Assoc. Prof. Dr. Hasan Demirel

27th March, 2011


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-by-pixel fashion:

( ) ( ) ( )

where ( ) is the input image in the Fourier domain, ( ) the filter function and G(k,l)

is the filtered image. To obtain the resulting image in the spatial domain, ( ) 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.

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 cut-off 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
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 is defined as follows:

( )
( ) {

The following example demonstrates the effect of applying an ideal bandpass filter with cut

off frequencies of 30 and 120 to a grey scale 256×256 image.

Figure 1a: Original Image Figure 1b: Filtered Image

Figure 1c: Frequency Spectrum of Figure 1d: Frequency domain


Image 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:

% Ideal Bandpass Filter


clc
clear all
close all
ima=imread('grass2.jpg');
ima = double(ima);
figure
subplot(221)
imshow(ima,[]);
title('Original image');
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*30;
cutoff2 = maxr*120;
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 < 30)
z(i,j) = 0;
else
if ( r > 120)
z(i,j) = 0;
else
z(i,j) =255;
end
end
end
end

% Plots
subplot(222)
imafft=imafft.*z/255;
ima_out = fftshift(ifft2(fftshift(imafft)));
ima_out =ima_out-ima;
fftshow(ima_out,'abs');
title('Filtered image (Ideal)');
subplot(223)
fftshow(imafft3, 'log');
title('Fourier Spectrum of Image')
subplot(224)
fftshow(z,'log');
title('Filtered');
Butterworth Bandpass Filter

This filter can be derived mathematically by multiplying the transfer functions of a a low and

high pass filter. The low pass filter will have the higher cut off frequency.

( )
( )⁄

( )
( )⁄

( ) ( ) ( )

where are the cut frequencies of the low and high pass filters respectively; is

the order of the filter and ( ) 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 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 30 and 120 to a 256×256 grey scale image.

Figure 2a: Original Image Figure 2b: Filtered Image


Figure 2c: Frequency domain filter function

The result shows that the ringing and blurring effect that was observed with the ideal banpass

filter does not exist for the Butterworth filter. The Matlab code is presented below:

% Bandpass Butterworth Filter


clear all;
close all;
clc
micro = imread('grass2.jpg');
micro = double(micro);
[nx ny] = size(micro);
nx
ny
u = micro;
micro = uint8(u);
imwrite(micro, 'grass5.jpg');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);

subplot(2,2,1)
imshow(micro,[]);

subplot(2,2,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/120)^(2*n));
filter2(i,j) = 1/(1 + (dist/30)^(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;

subplot(2,2,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);

subplot(2,2,4)
imshow(fil_micro,[])

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 lowpass filter

( ) ( )

( ) ( )

( ) ( ) ( )

where are the cut frequencies of the low and high pass filters respectively;

( ) is the distance from the origin.

The example below shows that the Gaussian bandpass filter is the best among the three

discussed filters
Figure 3a: Original Image Figure 3b: Filtered Image

Figure 3c: Frequency domain filter function

The Matlab code is presented below

% Gaussian Bandpass Filter


clear all;
close all;
clc
micro = imread('grass2.jpg');
micro = double(micro);
[nx ny] = size(micro);
nx
ny
u = micro;
micro = uint8(u);
imwrite(micro, 'grass5.jpg');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);

subplot(2,2,1)
imshow(micro,[]);

subplot(2,2,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 Gaussian filter.
filter1(i,j) = exp(-dist^2/(2*120^2));
filter2(i,j) = exp(-dist^2/(2*30^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;

subplot(2,2,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);

subplot(2,2,4)
imshow(fil_micro,[])

Conclusion

From the human perspective, the Gaussian filter would be the most appropriate filter for

filtering. Determining which filter to use is dependent on the application so desired. The

figure below shows the different filters and the result of their application to the grey scale

image.

Figure 4a: Ideal Bandpass Figure 4b: Butterworth Figure 4c: Gaussian
Filter Bandpass Filter Bandpass Filter
Figure 4d: Original Image

Figure 4f: Butterworth


Figure 4e: Ideal Filtered Figure 4g: Gaussian Filtered
Filtered

References

[1] McAndrew A., “An Introduction to Digital Image Processing with Matlab Notes for
SCM2511 Image Processing 1”, School of Computer Science and Mathematics,
Victoria University of Technology.

[2] Demirel H., “Digital Image Processing Notes (Lecture 4)” Electrical and Electronic
Engineering, Eastern Mediterranean University, Spring 2010-2011.

[3] Filters (Frequency). (n.d.). Medical Image Processing, Analysis and Visualization.
Retrieved March 26, 2011, from
http://mipav.cit.nih.gov/documentation/HTML%20Algorithms/FiltersFrequency.html

[4] domain., t. f. (n.d.). Digital Filters - Frequency Filters. Informatics Homepages


Server. Retrieved March 26, 2011, from
http://homepages.inf.ed.ac.uk/rbf/HIPR2/freqfilt.htm

[5] Anbajafari S., “Bandpass Filter for Image Processing”, Mathworks file exchange, 30
Nov 2008.

You might also like