260 Digital Image Processing
5.8 LOCAL OR NEIGHBOURHOOD OPERATION
In neighbourhood operation * the pixels in an image are modified based on some function of the pixels in their
neighbourhood. Linear spatia! filtering is often referred to as convolving a mask with an image. The fitter
masks are sometimes called convolution masks or convolution kernels *
5.8,1 Spatial Filtering
Spatial filtering involves passing a weighted mask , or kernel over the image and replacing the original image
pixel value corresponding to the centre of the kernel with the sum of the original pixel values in the region
corresponding to the kernel multiplied by the kernel weights.
5,8.2 Linear Filtering
In linear filtering * each pixel in the input image is replaced by a linear combination of intensities of neigh -
bouring pixels. That is, each pixel value in the output image is a weighted sum of the pixels in the neighbour-
hood of the corresponding pixel in the input image. Linear filtering can be used to smoothen an image as
well as sharpen the image. A spatially invariant linear filter can be implemented using a convolution mask .
If different filter weights are used for different parts of the image then the linear filter is spatially varying.
5,83 Mean Filter or Averaging Filter or Low pass Filter-
The mean filter replaces each pixel by the average of all the values in the local neighbourhood . The size of
the neighbourhood controls the amount of filtering. In a spatial averaging operation * each pixel is replaced by
-
a weighted average of its neighbourhood pixels, The low pass filter preserves the smooth region in the image
and it removes the sharp variations leading to blurring effect , The 3 by 3 spatial mask which can perform the
averaging operation is given below:
1 I l
3 by 3 low- pass spatial mask —— *1 1 1
1 i 1
l i l t 111
1 1 1 1 1
Similarly* the 5 by 5 averaging mask = —I
25
x1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
It is to be noted that the sum of the elements is equal to 1 in the case of a low-pass spatial mask . The blurring
effect will be more with the increase in the size of the mask. Normally* the size of the mask will be odd so
that the central pixel can be located exactly.
5J& A Limitations of Averaging Filter
The limitations of averaging filters are given below :
L Averaging operation leads to the blurring of an Image. Blurring affects feature localisation,
2. If the averaging operation is applied to an image corrupted by impulse noise then the impulse noise is
attenuated and diffused but not removed .
Copyrighted material
Image Enhancement 261
3, A single pixel, with a very unrepresentative value can affect the mean value of all the pixels in its
neighbourhood significantly.
Example 5.2 Analyse 3 x 3 mean filter in the frequency domain and prove that it behaves like a low-
pass filter.
1 1 1 "2
1
Solution The 3 x 3 mean filter is given by -9 x 1 1 1 . The 1 • 1
•19
9 9
1 1 1
. •49
1
* 9 9 ni
pictorial representation is given in ?ig. 5.29. 1 1
1
9 • 9 9
The representation of the pattern shown in Fig. 5.29 is given
below: .
Fig 5.29 3 x 3 Mean filter mask
I I
x ( n) , n 2 ) = -ft( «|, «2 ) + -
^ ( nl - > +-
l
l 1 I
6( H1 + 1) + - b( n2 - l + fi( n2
) - - 1) + -I £(« - 1, « 2 - l )
]
+ y
+ l, n 2-l) + -tf( n, + 1, n y
2+ 1 >+ “
y
<S( «1
-
Taking Z transform on both sides, we get
_+ _
_ i l _i 1 i _i 1 1 1 1 1
Xw[ Zy ,
. i
- i
+ *2 + 1 *2 + *1*2
]
o *l*2 + 9 *1
i
+!eM
9
1
9
+9
1
- e
1
9
9
^^ 9
+1
9
*2
eA>i
+ ie-MeM
9
=1+1 -^ + eM ) + (e M + eMl ) + >
^ ^ e~M eM ( + e~ M VM (eM + e )
"
~
9 9 9
.
= - + - X 2 COSUJ1 + - x 2 co$ w , + - e U l x 2 cosu 1
" 9
. + —9 x 2 c o s u;,
=9"* f -
9 9
.
x 2 cosu)1 + - X 2 CO$ UJ 3J + - x 2 cosu , f e~M + e^ i )
9
= - + - X 2 COSU;! -j- - x 2 cosu;? -h - x 2 cos x 2 cos
9 9 9 ‘
9 ^ s
^
X [^ } t U2
Case ( i ) Substitute
] = — + — X 2 C08 U j +
*
*
^ X 2 C0 SUJ2
= u2 = Oin the expression of
[0, 0 ] =
+ - X 4 C0 SUJ| COSUJ 2
-9 + -9 x 2 cos(0) + -9 x 2 cos(0) +-9 x 4 cos(0)cos(0)
, UJ 2 ] given in Eq. ( S .9 ).
(5.9)
12 2 4 9
[0, 0) = - + ~ + - + - ^ ~ = l ,
^ 9 9 9 9 9
Copyrighted material
Image Enhancement 263
,
, i 1
Example 5.3: Programming task Read an image, convolve the image with the mask _ 1 1 \ and
9*
1 1 1J
show that it performs averaging operation which results in blurring of the image. Also, analyse the impact
1 1 1 1 1]
1 1 1 1 1
1
of increasing the size of the mask to 5 x 5 , that is. mask is X 1 1 1 1 1
25
1 1 1 1 1
1 1 1 1 1
j
Solution The MATLAB code to perform spatial-domain operation filtering is shown in Fig. 5.32,
The corresponding results are shown in Fig. 5.33 . From the result, it is obvious that as the size of
the mask increases, the blurring increases. Also, one can observe that a spatial filter is implemented
.
through convolution. The masks A 1 and h2 are generally called as box / titers It is to be noted that the
masks h \ and A 2 basically perform the averaging operation.
Editoi - D:\woikVspotiallpfl . in
File Edit Text Cel Tools Debug Desktop Window Help
-
f l|n|[ x
f f X
! E £| I < jBsse I "O
1
2
3 M
VThis program performs spatial domain filtering
clc
f w.
*
3 clear all
4 close all
5 - a^ imread ( 1 r o s e . j p g ' ) J
6 - h l * l/ 9 * o n e s ( 3 , 3 ) ; k 3 x 3 mask
7 - h2 »l/ 2 5 * o n e s ( 5, 5 ) ; % 5 x 5 mask
8 - bl * conv2 ( a / h l, ' s a m e * ) ;
9 - b 2 = c o n v2 ( a, h2 , 1 s a m e ' ) ;
10 - i m s h o w ( a ] , t i t l e ( 1 original i m a g e * ) ,
11 f i g u r e , i m s h o w ( u i n t 8 (b l ) ) ,titleE Output using 3 x 3 m a s k )
1
12 , f i g u r e , i m5 h o u ( u i n t 3 ( b 2 ) ) , t i t l e ( * O u t p u t using 5 x 5 m a s k * )
13
14
< >
spatiallpt .m spatialhpfm histl m rough 4 m spatiallpfl m x
* * * * i
script Ln 14 Col 1 / r.
Fig. 5.32 Spatial domain filtering
Copyrighted material
278 Digital Image Processing
5.12 IMAGE ENHANCEMENT IN THE FREQUENCY DOMAIN
Frequency refers la the rate of repetition of some periodic event. In image processing, spatial frequency refers
to the variation of image brightness with its position in space A varying signal can be transformed into a
,
series of simple periodic variations. The Fourier transform decomposes a signal into a set of sine waves of
different characteristics like frequency and phase. If we compute the Fourier Transform of an image, and
then immediately inverse transform the result , we can regain the same image because a Fourier transform is
perfectly reversible. On the other hand, if we multiply each element of the Fourier coefficient by a suitably
chosen weighting function then we can accentuate certain frequency components and attenuate others. The
corresponding changes in the spatial form can be seen after an inverse transform is computed . This selective
enhancement or suppression of frequency components is termed Fourier filtering or frequency domain filter
ing , The spatial representation of image data describes the adjacency relationship between the pixels. On the
-
other hand the frequency domain representation clusters the image data according to their frequency distribu-
tion. In frequency domain filtering, the image data is dissected into various spectral b3nds, where each band
depicts a specific range of details within the image . The process of selective frequency inclusion or exclusion
is termed frequency domain filtering.
It is possible to perform filtering in the frequency domain by specifying the frequencies that should be
kept and the frequencies that should be discarded . Spatial domain filtering is accomplished by convolving the
image with a filter kernel. We know that
Convolution in spatial domain » Multiplication in the frequency domain
If filtering in spatial domain is done by convolving the input imagef( my n ) with the filter kernel A(m , n )9
Filtering in spatial domain = f ( nu n)
In the frequency domain * filtering corresponds to the multiplication of the image spectrum by the Fourier
transform of the filter kernel, which is referred to as the frequency response of the filter
Filtering in the frequency domain = F( A, / ) x // ( A , J )
m
Here , F( A; / ) is the spectrum of the input image and k I ) is the spectrum of the filter kernel. Thus, frequency
domain filtering is accomplished by taking the Fourier transform of the image and the Fourier transform of
the kernel , multi pi ing the two Fourier transforms, and taking the inverse Fourier transform of the result . The
multiplication of the Fourier transforms need to be carried out point by point . This point-by-point multiplica-
tion requires that the Fourier transforms of the image and the kernel themselves have the same dimensions.
As convolution kernels are commonly much smaller than the images they are used to filter, it is required to
zero pad out the kernel to the size of the image to accomplish this process.
. -
5.12 1 Low pass Filtering in Frequency Domain
The low - pass filter mask can be broadly classified into two types: ( i ) non -separable filter mask , and
(ii ) separable filter mask.
(i ) Non- separable Filter Transfer Function The non -sepcrable filter transfer function is given by
H( k ,i ) l, for < D0
= 0, otherwise
(5.16 )
Copyrighted material
Image Enhancement 279
Editoi D : \wotk \fieqlptf . m ini
File Edit Text Cell Tools Debu Desktop Window Help
r> i »
^
n r* &
* & X
1 HLOTT pass f i l t e r t r a n s f e r f u n c t i o n LPF transfer function
2 [ a b ] = f reqspace ( 256 , 1 roeshgtricL ) ;
3 H = 2e r o s ( 2 5 6 , 2 5 6 ) ;
4 - ci^s q r t f a . ^ 2 +b . A 2 ) < 0 * 5 ;
5 - H [ cl] = 1;
6 - imshotr( H) , t it l e ( r L F F transfer f r • ' )
< >
script Ln 6 Coi 36 vr
4 V V
Fig, 5 , 51 MMTUB code for Q low -pass filter transfer function Fig. 5,52 Low-pass filter transfer function
Here, D0 is the cut-off frequency of the low- pass filter. The cut-off frequency determines the amount of
frequency components passed by the filter.. The smaller the value of D0 , the greater the number of image
components eliminated by the filter. The value of Z)0 is chosen in such a way that the components of interest
are passed through .
The MATLAB code to generate the low-pass filter transfer function and the corresponding output arc
shown in Figs 5.51 and 5.52 respectively.
Separable Low- pass Filter Transfer Function The expression for a separable low- pass filter transfer
(1i )
function is given below:
1, for k < Dk and / < Dt
//( /) = (5.17 )
* 0, otherwise
The MATLAB code and the corresponding output are shown in Fig. 5.53 and Fig. 5.54 respectively.
Butterworth Low- pass Filter The transfer function of a two-dimensional Butterworth low -pass filter is
given by
1
H( k J ) = 2n
(5.18)
r1 M
1+ S +
T 2
A>
In the above expression , n refers to the filter order and Z)n represents the cut -off frequency.
Copyrighted material
280 Digital Imag e Processing
Editor * D: \work\freqhptf.m
File Edit Text Cell Tools Debug Desktop Window Hetp ^ ? X
C* m £4 K »
1 clc
2 clear all
3 close all
A [ a b ] = ± reqspace ( 256 , 1 irieshgrld 1 ) ;
5 - H= E e r o s [ 2 5 6 , 2 5 6 ) ;
6 - d^abs ( a) <0.5 sabs (b ) < 0, 5;
7 - H( d ) = l;
8 - lmshDw ( H) , title ( Separable LPF transfer i n' )
< >
;* Untitled freqhptf .m x
*
“if"
script Ln 8 Col 31 OVR *
p
Fig, 5.53 MATLAB code to generate a separable low - pass Biter Transfer function
Separable LPF transfer function
Fig* 5.54 Separable LPF Transfer function
MATLAB Example 5 Write a MATLAB program that performs a fwo- d/mens/ona/ Butterworth
low - pass filter of the given image for two different cuf - off frequencies .
Solution: The MATLAB code to perform a two- dimensional Butterworth low- pass filter is shown in
Fig. 5 55*
,
The outputs corresponding to the MATLAB code for different cut -off frequencies are shown in Fig. 5 , 56.
The 2 D and 3 D views of the transfer function for the three cut-off frequencies £>0 = 10, D0 — 20 and D0 = 30
are illustrated in Fig. 5.57 ( a ) to ( f ) respectively.
Copyrighted material