You are on page 1of 11

Lecture 8: Filters in Frequency Domain

If b is an image, then its Fourier transform will reflect the frequencies of the
periodic parts of the image. By masking or filtering out the unwanted frequencies one can
obtained a new image by applying the inverse Fourier transformation. A filter is a matrix
with the same dimension as the Fourier transform of the padded image. The components
of the filter usually vary from 0 to 1. If the component is 1, then the frequency is allowed
to pass; if the component is 0, then the frequency is tossed out. Let Filter represent such a
matrix. Then the filtered image is given by Newb in

2( .* 2( ,2* 1,2* 1)). Newb ifft Filter fft b nx ny =

The Matlab codes fftsine.m and fftsine2d.m in the previous lectures illustrated this for a
low frequency sine wave with higher frequency sine noise.
Three important types of filters are low pass, high pass and band-reject. They are
depicted in the following graphic where the low frequencies have been shifted to the
center and one is viewing the diagonal section of the filter matrix.


low pass
high
pass
band-reject
frequency


The above filters have jumps and are often called ideal filters. The important
filters that make use of polynomial and exponential approximations are the Butterworth
and Gaussian filters. Let w, d0 be given and dist(i,j) = ((i-(nx+1))
2
+ (j-(ny+1))
2
)
1/2

Butterworth Band-reject Filter:

2 2 2
1
( , )
1 ( ( , ) /( ( , ) 0 ))
n
Filter i j
dist i j w dist i j d
=
+
.

Gaussian Bandreject Filter:

2 2 2
1
(( ( , ) 0 )/ ( , ) )
2
( , ) 1 .
dist i j d dist i j w
Filter i j e


=

The parameters w and d0 control the width and location of the band. In the Butterworth
band-reject filter the exponent n controls the steepest of the boundaries in the band of
frequencies to be rejected. There are similar versions of high and low pass filters.
The Matlab code filter_ncsu.m uses a low pass filter to mask the noise from sine
and cosine functions with frequency equal to 80 (160 in the padded Fourier transform).
























Matlab Code filter_ncsu.m






























cl ear ;
ncsu = [ bi gn( 5) bi gc( 7) bi gs( 9) bi gu( 11) ] ;
newncsu = 20*ncsu;
[ nx ny] = si ze( newncsu) ;
nx
ny
newncsu = newncsu( nx: - 1: 1, : ) ;
newncsu1 = ui nt 8( newncsu) ;
i mwr i t e( newncsu1, ' ncsu. j pg' ) ;
u = newncsu;
f or i = 1: nx %Thi s i s NCSU wi t h per i odi c noi se.
f or j = 1: ny
u( i , j ) = u( i , j ) + . . .
15. *( 1+si n( 2*pi *( ( i - 1) / nx) *80) ) +. . .
15. *( 1+si n( 2*pi *( ( j - 1) / ny) *80) ) ;
end
end
si nencsu = ui nt 8( u) ;
i mwr i t e( si nencsu, ' si nencsu. j pg' ) ;
f f t u = f f t 2( u, 2*nx- 1, 2*ny- 1) ;
f f t u = f f t shi f t ( f f t u) ;
subpl ot ( 2, 2, 1) ;
mesh( u' ) ;
subpl ot ( 2, 2, 2) ;
mesh( l og( 1+( abs( f f t u) ) ) ) ;
f i l t er = ones( 2*nx- 1, 2*ny- 1) ;
d0 = 150; %Use i deal l ow pass f i l t er .
f or i = 1: 2*nx- 1
f or j =1: 2*ny- 1
di st = ( ( i - ( nx+1) ) ^2 + ( j - ( ny+1) ) ^2) ^. 5;
i f di st > d0
f i l t er ( i , j ) = 0;
end
end
end
subpl ot ( 2, 2, 3) ;
mesh( f i l t er ) ;
f i l _ncsu = f i l t er . *f f t u;
subpl ot ( 2, 2, 4) ;
mesh( l og( 1+abs( f i l _ncsu) ) ) ;
f i l _ncsu = i f f t shi f t ( f i l _ncsu) ;
f i l _ncsu = i f f t 2( f i l _ncsu, 2*nx- 1, 2*ny- 1) ;
f i l _ncsu = r eal ( f i l _ncsu( 1: nx, 1: ny) ) ;
f i l _ncsu = ui nt 8( f i l _ncsu) ;
i mwr i t e( f i l _ncsu, ' si nencsu_f i l . j pg' ) ;













The filtered image sinencsu_fil.jpg is not entirely satisfactory. Another approach
is to use a band-reject filter where one may need to experiment with the width, w, and the
location, d0, of the band. The Matlab code filter_bu.m uses the Butterworth band-reject
filter and may be applied to either the noisy big sine wave or the noisy ncsu.























Matlab Code filter_bu.m
cl ear ;
ncsu = [ bi gn( 5) bi gc( 7) bi gs( 9) bi gu( 11) ] ;
newncsu = 20*ncsu;
[ nx ny] = si ze( newncsu) ;
nx
ny
newncsu = newncsu( nx: - 1: 1, : ) ;
newncsu1 = ui nt 8( newncsu) ;
i mwr i t e( newncsu1, ' ncsu. j pg' ) ;
u = newncsu;
f or i = 1: nx
f or j = 1: ny
%Thi s i s t he bi g wave wi t h per i odi c noi se.
u( i , j ) = 0+100*( 1+si n( 2*pi *( ( j - 1) / ny) *5) ) + . . .
15. *( 1+si n( 2*pi *( ( i - 1) / nx) *80) ) +. . .
15. *( 1+si n( 2*pi *( ( j - 1) / ny) *80) ) ;
%Thi s i s NCSU wi t h per i odi c noi se.
%u( i , j ) = u( i , j ) + . . .
15. *( 1+si n( 2*pi *( ( i - 1) / nx) *80) ) +. . .
15. *( 1+si n( 2*pi *( ( j - 1) / ny) *80) ) ;
end
end
si nencsu = ui nt 8( u) ;
i mwr i t e( si nencsu, ' si nencsu. j pg' ) ;
f f t u = f f t 2( u, 2*nx- 1, 2*ny- 1) ;
f f t u = f f t shi f t ( f f t u) ;
subpl ot ( 2, 2, 1) ;
mesh( u' ) ;
subpl ot ( 2, 2, 2) ;
mesh( l og( 1+( abs( f f t u) ) ) ) ;
f i l t er = ones( 2*nx- 1, 2*ny- 1) ;
d0 = 160; %Use But t er wor t h band- r ej ect f i l t er .
n = 4;
w = 20;
f or i = 1: 2*nx- 1
f or j =1: 2*ny- 1
di st = ( ( i - ( nx+1) ) ^2 + ( j - ( ny+1) ) ^2) ^. 5;
i f di st ~= d0
f i l t er ( i , j ) = 1/ ( 1 + ( di st *w/ ( di st ^2 - d0^2) ) ^( 2*n) ) ;
el se
f i l t er ( i , j ) = 0;
end
end
end
subpl ot ( 2, 2, 3) ;
mesh( f i l t er ) ;
f i l _ncsu = f i l t er . *f f t u;
subpl ot ( 2, 2, 4) ;
mesh( l og( 1+abs( f i l _ncsu) ) ) ;
f i l _ncsu = i f f t shi f t ( f i l _ncsu) ;
f i l _ncsu = i f f t 2( f i l _ncsu, 2*nx- 1, 2*ny- 1) ;
f i l _ncsu = r eal ( f i l _ncsu( 1: nx, 1: ny) ) ;
f i l _ncsu = ui nt 8( f i l _ncsu) ;
i mwr i t e( f i l _ncsu, ' si nencsu_f i l . j pg' ) ;





































Another application of the band-reject filter is to the noisy aerial photograph. This
image suffers from too much light an exposure and from banded sine and cosine noise.
The light is modified by use of the power transformation with the power equal to two,
and then the Butterworth band-reject filter is used to reduce to noise.























Matlab Code filter_aerial.m
cl ear ;
aer i al = i mr ead( ' Fi g3. 09( a) . j pg' ) ;
aer i al = doubl e( aer i al ) ;
[ nx ny] = si ze( aer i al ) ;
nx
ny
u = aer i al ;

f or i = 1: nx
f or j = 1: ny
%Thi s i s aer i al wi t h per i odi c noi se.
u( i , j ) = u( i , j ) + . . .
5. *( 1+si n( 2*pi *( ( i - 1) / nx) *200) ) +. . .
5. *( 1+si n( 2*pi *( ( j - 1) / ny) *200) ) +. . .
5. *( 1+cos( 2*pi *( ( i - 1) / nx+( j - 1) / ny) *141) ) +. . .
5. *( 1+si n( 2*pi *( ( i - 1) / nx- ( j - 1) / ny) *141) ) ; ;
end
end
si neaer i al = ui nt 8( u) ;
i mwr i t e( si neaer i al , ' si neaer i al . j pg' ) ;

c = 1. ; %Use t he power t r ansf or mat i on t o dar ken.
gamma = 2;
f _f p =255*c*( u/ 255) . ^gamma;
u = f _f p;
f f t u = f f t 2( u, 2*nx- 1, 2*ny- 1) ;
f f t u = f f t shi f t ( f f t u) ;
subpl ot ( 1, 2, 1)
mesh( l og( 1+( abs( f f t u) ) ) ) ;

f i l t er = ones( 2*nx- 1, 2*ny- 1) ;
d0 = 400; %Use But t er wor t h band r ej ect f i l t er .
n = 4;
w = 20;
f or i = 1: 2*nx- 1
f or j =1: 2*ny- 1
di st = ( ( i - ( nx+1) ) ^2 + ( j - ( ny+1) ) ^2) ^. 5;
i f di st ~= d0
f i l t er ( i , j ) = 1/ ( 1 + ( di st *w/ ( di st ^2 - d0^2) ) ^( 2*n) ) ;
el se
f i l t er ( i , j ) = 0;
end
end
end
f i l _aer i al = f i l t er . *f f t u;
subpl ot ( 1, 2, 2)
mesh( l og( 1+abs( f i l _aer i al ) ) ) ;

f i l _aer i al = i f f t shi f t ( f i l _aer i al ) ;
f i l _aer i al = i f f t 2( f i l _aer i al , 2*nx- 1, 2*ny- 1) ;
f i l _aer i al = r eal ( f i l _aer i al ( 1: nx, 1: ny) ) ;
f i l _aer i al = ui nt 8( f i l _aer i al ) ;
i mwr i t e( f i l _aer i al , ' si neaer i al _f i l . j pg' ) ;







































































The Matlab code filter_micro.m uses a high pass filter to give emphasis to the
higher frequencies in the image of a damaged electronic chip. The high pass image is
then added to the original image so as to obtain a sharper image. The reader may find it
interesting to experiment with width and frequency threshold of the Butterworth or the
Gaussian high pass filters. Also, it is interesting to compare this sharpening, which is
done in the frequency domain, with the sharpening done in the space domain as in the
third lecture .

FFT of Image High Pass Filter of FFT Image



























Matlab Code filter_micro.m

cl ear ;
mi cr o = i mr ead( ' Fi g4. 04( a) . j pg' ) ;
mi cr o = doubl e( mi cr o) ;
[ nx ny] = si ze( mi cr o) ;
nx
ny
u = mi cr o;
mi cr o = ui nt 8( u) ;
i mwr i t e( mi cr o, ' mi cr o. j pg' ) ;
f f t u = f f t 2( u, 2*nx- 1, 2*ny- 1) ;
f f t u = f f t shi f t ( f f t u) ;
subpl ot ( 1, 2, 1)
mesh( l og( 1+( abs( f f t u) ) ) ) ;
%Use But t er wor t h or Gaussi an hi gh pass f i l t er .
f i l t er = ones( 2*nx- 1, 2*ny- 1) ;
d0 = 100;
n = 4;
f or i = 1: 2*nx- 1
f or j =1: 2*ny- 1
di st = ( ( i - ( nx+1) ) ^2 + ( j - ( ny+1) ) ^2) ^. 5;
%Use But t er wor t h hi gh pass f i l t er .
f i l t er ( i , j ) = 1/ ( 1 + ( di st / d0) ^( 2*n) ) ;
f i l t er ( i , j ) = 1. 0 - f i l t er ( i , j ) ;
%Use Gaussi an hi gh pass f i l t er .
%f i l t er ( i , j ) = exp( - di st ^2/ ( 2*d0^2) ) ;
%f i l t er ( i , j ) = 1. 0 - f i l t er ( i , j ) ;
end
end
%Updat e i mage wi t h hi gh f r equenci es.
f i l _mi cr o = f f t u + f i l t er . *f f t u;
subpl ot ( 1, 2, 2)
mesh( l og( 1+abs( f i l _mi cr o- f f t u) ) ) ;
f i l _mi cr o = i f f t shi f t ( f i l _mi cr o) ;
f i l _mi cr o = i f f t 2( f i l _mi cr o, 2*nx- 1, 2*ny- 1) ;
f i l _mi cr o = r eal ( f i l _mi cr o( 1: nx, 1: ny) ) ;
f i l _mi cr o = ui nt 8( f i l _mi cr o) ;
i mwr i t e( f i l _mi cr o, ' mi cr o_f i l . j pg' ) ;

You might also like