Unit 4
Unit 4
IMAGE FILTERING
OPERATIONS IN SPATIAL
DOMAIN
4.1 INTRODUCTION
In the previous units of this course, we had seen how image transformation
carried out on a specific pixel depends on the gray value of that pixel. In this
unit, we shall consider more powerful transformation where the gray values of
the neighbouring pixels also play a useful part. We have discussed point
processing operations of the type s T ( r ), where r is the grey level at a single
pixel in the input and s is the new value of that pixel. The capabilities of point
operations are limited as the relation between a pixel and its neighbours is not
exploited.
In this unit, we will be considering a neighbourhood of pixels from the input
image. This is termed as spatial filtering. We introduce image enhancement
with the help of image sharperning (spatial high pass) and image smoothing
(low pass filters). Spatial filtering as generally used in preprocessing
operations for noise removal or for edge detection/enhancement applications.
Objectives
After studying this unit, you should be able to
apply spatial filtering
perform linear filtering in spatial domain
Use differentiate low pass & high pass filtering in spatial domain
Use differentiate non-linear filtering and linear filtering
Perform median, min & max filters in spatial domain
define histogram;
generate histogram form given image;
perform histogram equalization;
perform histogram specification.
By this process, we create a new image where grey level values of the pixels
are calculated from the values under the mask. The values under the mask
are modified by a function called ‘filter’. If this filter function is a linear function
of all grey level values in the mask, then filter is called a ‘linear filter’, else it is
called ‘non linear filter’.
Spatial filters can be of various types as shown in Fig. 2.
Spatial Filters
131
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
Fig. 4
The right hand side of Eqn. (1) denotes the set of coordinates covered by filter.
For a 3 3 filter, with coefficients w (i, j) the output image at coordinate ( x , y) is
calculated as
1 1
g ( x , y) f ( x i, y j) w (i, j) (2)
i 1 j 1
Step 1: Position the mask over the current pixel such that hotspot w(0,0)
coincides with current pixel.
Step 2: Form all products of filter elements with the corresponding elements in
the neighbourhood. 132
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
Step 3: Add up all the products and store it at current position in the output
image.
1
Output [5 1 1 1 2 1 4 1 4 1 7 1 2 1 6 1 20 1]
9
49
5
9
5 1 2 6 7 5 1 2 6 7
4 4 7 5 8 4 4 7 5 8
2 6 20 6 7 2 6 20 6 7
3 1 2 4 5 3 1 2 4 5
10 2 1 1 3 10 2 1 1 3
(a) (b)
Fig. 6
Shift the mask one pixel towards right such that grey level 7 coincides with hot
spot (Fig. 6 (b)). Calculate output for that pixel position and replace in output
image. Shift it right again and locate the mask on pixel with grey value 5, and
repeat the process.
1
Output [1 1 2 1 6 1 4 1 7 1 5 1 6 1 20 1 6 1]
9 133
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
57
6
9
Now go back to the first position of the mask and shift it down so that it is now
centered on pixel with grey value 6 (just below 4), and repeat the process.
1
Output [2 1 6 1 7 1 7 1 5 1 8 1 20 1 6 1 7 1]
9
68
8
9
Continue doing it over all the possible pixels in the given image.
1 49
Output [4 4 7 2 6 20 3 1 2] 5
9 9
1 55
Output [4 7 5 6 20 6 1 2 4] 6
9 9
1 64
Output [7 5 8 20 6 7 2 4 5] 7
9 9
1 38
Output [2 6 20 3 1 2 10 2 1] 4
9 9
1 44
Output [6 20 6 1 2 4 2 1 2] 5
9 9
1 59
Output [20 6 7 2 4 5 1 2 3] 6
9 9
So, by shifting the mask over the whole image a new image is generated.
These values are rounded off to nearest integer values and put in the output
image as given below at corresponding pixel locations.
* * * * *
* 5 6 8 *
g ( x , y) * 5 6 7 *
* 4 5 6 *
* * * * *
***
The filtering effect is visible in the new image where the noisy values 10, 20 in
original image have been replaced by smoothed values.
Remark: There is a small problem in applying a filter at the edge of the image,
where the mask partly falls outside the image. In Example 1, input image size
5 5, and the mask size 3 3. Therefore, the output image size 3 3.
The size of output image is smaller than input image size as the mask does
not overlap fully in the 1st row, 1st column last column and last row as shown
in image g ( x , y).
E1) For a 3 3 mask, explain the mechanics of linear filtering with necessary
equations.
Now, we discuss one of the commonly used linear filter known as a mean
filter.
The aim of image smoothing is to diminish the effect of camera noise, spurious
pixel values, missing pixel values etc. This is done by a neighborhood
averaging filter. Each pixel in the smoothened image g ( x , y) is obtained from
the average pixel value in the neighborhood of ( x , y) in input image. Such a
mask is also known as a Mean filter.
1
g ( x , y) f ( m, n ) w ( m , n )
M ( m ,n )Sxy
(3)
Two masks averaging and weighted averaging masks are shown in Fig. 7. The
output is average of pixels contained in the neighbourhood of filter mask.
Therefore, smoothing filters are also called averaging filters or low pass filters.
Fig. 7 (a) and Fig. 7 (b) shows 3 3 smoothing filter masks. Spatial filter, where
all coefficients are equal, is called box filter. However, to give maximum
importance to pixel at the centre of the mask, it is given maximum weight, and
the weights of the neighnouring pixels are progressively reduced.
ii) Bridging small gaps in lines or curves of the boundaries of objects and
text. Small gaps in boundary can lead to enormous results in object
extraction. Smoothing fills up all smaller gaps and helps in producing
correct result.
5 1 2 6 7
5 6 8
4 4 7 5 8
5 6 7
2 6 20 6 7
4 5 6
3 1 2 4 5
g ( x , y)
10 2 1 2 3
f ( x , y)
Fig. 9
Solution: Smoothing filter replaces every pixel of the input image by the
average of the grey levels in the neighborhood. It reduces sharp transitions in
grey levels. Note that pixel value 20 in f ( x , y) has been changed to 6 in the
output image.
Smoothing filter reduces noise (desirable) and also blurs edges (undesirable).
In the image f ( x , y), pixel value 20(high value as compared to neighborhood)
becomes 6, pixel value 1 (low value as compared to neighborhood) becomes
4, whereas pixel value 7(similar value as compared to neighborhood)
becomes 6.
Thus, noisy pixel or edges (20, 1: arbitrarily high/ low values) are reduced to
average value, closer to neighborhood values. Note that smoothing effect will
be more if instead of 3x3 mask, a large sized mask such as 7 7 or 11 11
mask is used. The size of the mask will depend on amount of noise and size of
the image.
***
Try these exercises.
E3) With various spatial masks and equations, explain spatial averaging
technique for enhancement.
Here, we discuss some simple non linear filters such as median filter and
Min/Max filters.
Median Filters
Median filters are edge preserving smoothing filters, where the level is set to
the median of pixel values in the neighborhood of that pixel. It is impossible to
design a filter that removes only noise and retains all the important image
structures intact, because no filter can discriminate which image content is
important to the viewer and which is not. Median filter replaces every image
pixel by median of the pixels in the corresponding filter region Sxy.
137
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
If the number of elements are even (2k) the median of the sorted sequence
(p 0 , p1,...., p k 1 , p k ,...., p 2 k 1 ) is defined as arithmetic of two middle values
p k 1 p k
Thus, median ( p 0 , p1 ,...., p k 1 , p k ,..., p 2 k 1 )
2
Fig. 10 shows median filtering process and generating output image. Median
filter is very popular because for impulse noise (salt and pepper noise,
randomly placed white and black dots), it provides excellent noise reduction
capabilities with considerably less blurring than linear smoothing filter of same
size. Median filters force the points with distinct levels to be more like their
neighbors. It eliminates isolated clusters of pixels that are dark or light with
respect to their neighbors and whose area is less than k 2 / 2 (one half of filter
area).
Disadvantages
Step 1: Sort the values of the pixel in question and its neighbours.
Step 2: Determine their median value.
Step 3: Assign median value to that pixel.
Example 3: Compute the median value of the marked pixels show below
using a 3 3 mask
18 22 33 25 32 24
34 128 24 172 26 33
22 19 32 31 28 26
Solution: i) Median (18, 22, 33, 34, 128, 24, 22, 19, 32)
Median (18, 19, 22, 22, 24, 32, 33, 34, 128) 24
ii) Median (22, 33, 25, 128, 24, 172,19, 32, 31)
median (19, 22, 24, 25, 31, 32, 33,128,172) 31
iii) Median (33, 25, 32, 34,172, 26, 32, 31, 28)
138
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
median (24, 25, 26, 28, 31, 32, 32, 33,172) 31
iv) Median (25, 32, 24, 172, 26, 23, 31, 28, 26)
median (23, 24, 25, 26, 26, 28, 31, 32,172) 26
***
Minimum and Maximum Filter
Apply
a) Smoothing filter
b) Weighted average filter
c) Median filter
d) Min filter
e) Max filter
1 31
g1 (2, 2) [3 1 2 2 7 5 0 6 5] 3.44 3
9 9
139
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
1
g 2 (2, 2) [0 1 0 6 5 2 3 1 2 5 1 2 7 5 4 1 0 6 5
25
81
2 2 3 5 5 7 6] 9
9
1 2 1
1
2 4 2
16
1 2 1
1
g 3 (2, 2) [3 1 1 2 2 1 2 2 7 4 5 2 0 1 2 6 5 1]
16
66
4.125 4
16
g 6 (2, 2) 0
g 7 (2, 2) 0
g 8 (2, 2) 7
***
Try these exercises.
f
f ( x 1) f ( x ) (8)
x
2f
f ( x 1) f ( x 1) 2f ( x ) (9)
x 2
f
Solution: [3 4, 2 3, 5 2, 9 5]
x
[1, 1, 3, 4]
f
2
[ 4 2 6, 3 5 4, 9 2 10]
2x
[0, 4, 1]
***
With respect to the first and second derivatives, following things are observed
141
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
Here, we will discuss some sharpening filters base on first derivatives and
second derivatives.
An edge is the boundary between two regions with distinct grey level
properties. Derivative operators are used for most edge detection techniques.
The magnitude of first derivative calculated within a neighborhood around the
pixel of interest is used to detect presence of edge in an image. First
derivatives are directional operators and is defined as
f
g x x
f f (10)
g y
y
1 0 1 1
1 0 0 0
1 0 0 1
0 1 1 0
(c)
Fig. 12
g x z 5 z8 (13)
142
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
g y z5 z6 (14)
f z 5 z8 z5 z6 (15)
f ~
z 5 z 9 z 6 z8 (16)
Equations can be implemented by the masks in Fig. 12 (a) and Fig. 12 (c). The
original image is convolved with both the masks separately and the absolute
values of the two outputs of convolution are added.
Prewitt Operator
The above mask in Fig. 13 can be used to identify the presence of a vertical
edge in the following image:
30 30 30 80 80 80
30 30 30 80 80 80
30 30 30 80 80 80
30 30 30 80 80 80
30 30 30 80 80 80
30 30 30 80 80 80
f ~
(z 7 z 8 z 9 ) (z1 z 2 z 3 ) (z 3 z 6 z 9 ) (z1 z 4 z7 )
Sobel Operator
Another sharpening first order filter is the Sobel Operator. It is slightly different
from the Prewitt filter, in that the middle pair of pixels are given higher weight
compared to other two pairs of pixels. Both the filters are also called EDGE
FILTERS. Sobel is the most popular 3 3 Edge operator. It is described by
f (z 7 2z 8 z 9 ) (z1 2z 2 z 3 ) (z 3 2z 6 z 9 ) (z1 2z 4 z7 )
Sobel mask is shown in Fig. 14. Notice that all mask coefficients sum up to
zero, thus giving no response in the area of constant intensity.
143
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
You can observe from the masks given in Fig. 14, that first masks has only
one difference in the members of first and third rows that is of sign, that is it
has “2” and “–2” values in center of first and third rows. When applied on an
image this mask will highlight the horizontal edges. Similarly, the second mask
in Fig. 14 shows the vertical edges.
When we apply this mask on the image it prominent vertical edges. It simply
works like as first order derivative and calculates the difference of pixel
intensities in an edge region.
As the center column is of zero so it does not include the original values of an
image but rather it calculates the difference of right
f ( x , y) f f
f
x y x y
144
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
(a) (b) (c) (d)
1 2 1 0 1 0 1 1 1
2 4 2 1 4 1 1 8 1
1 2 1 0 1 0 1 1 1
0 1 0 0 0 0 0 1 0
1 5 1 0 1 0 1 4 1
0 1 0 0 0 0 0 1 0
Similarly
1 1 1 0 0 0 1 1 1
1 9 1 0 1 0 1 8 1
1 1 1 0 0 0 1 1 1
These are high frequency enhancement filters that pass all frequencies and
enhances high frequencies . Notice that all the coefficients in the laplacian
mask add up to one, so convolution with this mask will not change the pixel 145
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
values in the part of the image that has constant grey levels. But if the pixels
have higher or low grey level values than its neighbours, this contrast will be
enlarged by the filter.
Example 5: For the image segment f ( x , y) given below. Apply laplacian filter
of size 3 3 and 5 5 on the centre pixel.
0 1 0
1 4 1
0 1 0
g 9 (2, 2) [1 2 5 6 4 7] 14
1 1 1
1 8 1
1 1 1
E8) How are various filter mask generated to sharpen images in spatial
filtering?
E9) Explain spatial filtering in image enhancement.
E11) What are image sharpening filters? Explain the various types of q
sharpening filters.
0 0 0 0
0 1 2 3
0 2 4 6
Fig. 1: Image
rk 0 1 2 3 4 5 6
nk 6 1 2 1 1 0 1
Fig. 2: Histogram
***
Normalized histogram is obtained by dividing the frequency of occurrence of
each gray level rk by the total number of pixels in an image.
nk
p(rk ) , where k 0........ L 1 , where n total number of pixels in the
n
image p( rk ) gives the probability of occurrence of grey level rk . The sum of all
components of a normalized histogram is equal to 1.
Example 2: Find normalized histogram of the image
0 0 0 0
0 1 2 3
0 2 4 6
147
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
You can visualize histograms of different images when you will be doing your
scilab sessions as given in Block 5 (Lab Manual). Now let us discuss various
applications of histogram.
Applications of Histogram
Histogram is widely used in image processing applications.
Fig. 4
ii) Histogram indicates if clipping is a problem in the image.
iii) It is extensively used in thresholding to separate contrasting objects from
background.
iv) It tells us about image contrast.
149
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
Now, try the following exercises.
Goal: To obtain a uniform histogram for the output image. To devise a point
operation s T ( r ) that maps input image f ( x , y) to an output image g( x , y)
that has a flat histogram as shown in Fig. 5.
s T (r ), 0 r 1 … (1)
Fig. 6
The grey levels in an image may be viewed as random variables in the interval
[0,1] . Probability density function (PDF) is one of the fundamental descriptors
of a random variable. PDF of a random variable x is defined as the derivative
of cumulative distribution function (CDF)
dF( x )
p( x )
dx
F( x ) P ( X x )
Where p( x ) PDF of x
F( x ) CDF of x
p Probability of x
PDF satisfies the following properties:
i) P( x ) 0 for all x
ii) p(x) dx 1
151
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
x
iii) p(x) d where is a dummy variable
x2
iv) p( x 1 x x 2 ) p(x ) dx
x1
ds dt (r ) d
r
p r ( w ) dw
dr dr dr 0
p r (r )
dr 1
p s (s) p r (r ) p r (r ) 1
ds p r (r )
Fig. 7
152
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
p s (s) is PDF of output image, whose value is 1 for all values of s. Thus p s (s)
is a uniform PDF independent to the form of p r (r) as shown in Fig. 7.
Fig. 8
Example 3: For the given 4 4 image having grey scales between [0, 9] , carry
out histogram equalization. Also, draw the histogram of image before and after
equalization.
153
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
2 3 3 2
4 2 4 3
3 2 3 5
2 4 2 4
Solution: Given is
Step 1: Find the histogram of grey levels of input image by making a table of
rk (grey levels) and number of pixels (n k ) in the input image.
k
Step 2: For each input grey level, compute the cumulative n
j 0
j sum by
Step 3: Divide the cumulative sum by the total number of pixels in the image
(4 × 4 = 16) to generate the CDF of the given image.
Step 4: Scale the output by multiplying step 3 to maximum grey level value (9
in this case)
Step 5: Round off the output of step 4 to the nearest integer value
Grey pixel r j 0 1 2 3 4 5 6 7 8 9
Step No. of pixels 0 0 6 5 4 1 0 0 0 0
1 nj
Step Commulative
2 k 0 0 6 11 15 16 16 16 16 16
sum n
j 0
j
Step k 0 0 6 11 15 16 16 16 16 16
s nj
n
3 16 16 16 16 16 16 16 16
j 0
Step s9 0 0 6 11 15 16 16 16 16 16
4 9 9 9 9 9 9 9 9
16 16 16 16 16 16 16 16
Step 6: Map input levels (rk ) to output levels s k in a tabular form from 1st and
last row of table above to generate
rk 0 1 2 3 4 5 6 7 8 9
sk 0 0 3 6 9 9 9 9 9 9
Fig. 9
It may be noted that while in the original image the histogram spans between
grey values 2 to 5, in the output image, the grey values span from 3 to 9.
Although some equalization has been achieved, some of the grey values are
still missing. It is because the image has only 9 levels. Histogram equalization
will be very effective in an image with 256 grey levels.
4 4 4 4 4 4 4 0
4 5 5 5 5 5 4 0
4 5 6 6 6 5 4 0
4 5 6 7 6 5 4 0
4 5 6 6 6 5 4 0
4 5 5 5 5 5 4 0
4 4 4 4 4 4 4 0
4 4 4 4 4 4 4 0
In the next section we shall see how to find transformation for an image, such
that the new histogram follows a specific shape. It is called Histogram
Specification.
155
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
Histogram equalization has a disadvantage that it can generates only one type
of output image where histogram is flat, which may not be the best approach
under all circumstances. Histogram specification is more general than
equalization. It is an interactive enhancement technique where user can draw
desired histogram. We can specify the shape of histogram which need not be
uniform. Thus, histogram specification gives us the flexibility to choose
histogram for reference and output image is mapped accordingly. The basic
idea is to carry out histogram equalization of input image histogram, then carry
out histogram equalization of specified histogram. The two transformations are
used to find the required transformation.
The transformation is s T ( r ) p r ( r ) dr
0
s
Histogram equalization of input image G ( z) p z ( z) dz
0
To find the histogram equalization of specified image, we equate G ( z) is
equated to s and an inverse transformation is computed as given below:
156
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
G (z) s T(r )
z G 1[ s ] G 1[T (r )]
Assuming that G 1 exists, we can map input grey levels r to output grey levels
s.
Procedure for histogram specification
Step 3: G ( z) p z ( z) dz
0
Equate G (z) s T ( r )
Step 4: Obtain inverse transformation function G 1 .
z G 1[ s ] G 1[T(r )]
Step 5: Obtain the output image by applying inverse transformation function
to all pixels of input image.
Let us now apply these steps in the following example.
2r 1; 0 r 1 2 z ; 0 z 1
p r (r ) p s (r )
0 ; otherwise 0; otherwise
Apply histogram specification with the desired PDF function p z ( z) given in Fig.
10:
(a) (b)
Fig. 10
Solution: We shall solve it step wise.
Step 1: Obtain transformation function T ( r ) by doing histogram equalization
of input image.
r r
s T (r ) p r (r ) dr (2r 2) dr [ r 2 2r ]0r r 2 2r
0 0
Step 2: Obtain transformation function G ( z)
157
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
z z
G (z) p z (z) dz 2z dz [z 2 ]0z z 2
0 0
Step 3: Equate
s T(r ) G (z)
r 2r z 2
2
z G 1[T(r )]
z r 2 2r
***
Now, we shall find the discrete transformation function for an actual image.
k
s k T(rk ) p r (rj ), k 0,..., L 1
j0
k nj
sk
j0 n
q
v q G (z q ) p z (z i ), q 0,.., L 1
i 0
Equate G ( z q ) s k T ( rk )
0 1 0 2
ri z i 0,1, 2, 3
2 3 3 2
p r (ri ) .25 for i 0,1, 2, 3
3 1 0 1
p z (z 0 ) 0, p z (z1 ) .5, p z (z 2 ) .5, p z (z 3 ) 0
1 3 2 0
rk 0 1 2 3
p r (rk ) .25 .25 .25 .25
sk .25 .5 .75 1
zq 0 1 2 3
p z (rz ) 0 .5 .5 0
vq 0 .5 1 1
rk p r (rk ) sk zq p z (z q ) vq v4 p
0 .25 .25 0 0 0 .5 1
1 .25 .5 1 .5 .5 .5 1
2 .25 .75 2 .5 1 1 2
3 .25 1 3 0 1 2 2
a) q 0, k 0 ( v 0 s 0 ) (0 .25) 0 No increase q
q 1, k 0 ( v1 S0 ) (.5 .25) 0 Yes v * 0 v1 .5 and p 0 z1 1
c) q 1, k 2 ( v1 s 2 ) (0 .75) 0 No increase q
q 2, k 2 ( v 2 s 2 ) (1 .75) 0 Yes v * 2 v 2 1 and p 2 z 2 2
rk 0 1 2 3
p 1 1 2 2
Step 5: Map input pixels to new values to get new image using step 4 output.
0 1 0 2 1 1 1 2
2 3 3 2 2 2 2 2
0 1 0 1 2 1 1 1
1 3 2 0 1 2 2 1
f ( x , y) g ( x , y)
This pixel mapping is used to map the pixels of input image to generate output
image with specified histogram. Histogram specification is a trial and error
process. There are no rules for specifying histogram and one must resort to
analysis on a case by case basis for any given enhancement task.
***
Try the following exercises.
r 0,1, 2, 3, 4
p(ri ) 0.2 for i 0.......4
z i 0, 2, 4, 5, 7
p(z 0 ) 0, p(z 2 ) 0.2, p(z 4 ) 0.4, p(z 5 ) 0.4,
p( z 7 ) 0
4.8 SUMMARY
In this unit, we have discussed the following points.
2) A filter that passes low frequencies is called a lowpass filter. This type of
filter is used to blur (smooth) the image, therefore called smoothing filter
also. These filters are also used for noise reduction. Noise reduction can
be done by blurring with a linear filter, (in which operation performed on
the image pixels is linear) or a non-linear filter.
160
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
4) Linear filtering is a spatial domain process where a filter (mask/ kernel/
template) with some integer coefficient values is applied to input image to
generate the filtered/ output image.
6) Like all other spatial filters, non linear filters compute the result at some
position ( x , y) from the pixels inside the moving region S of the original
image. These filters are called non-linear because source pixels are
processed by some non-linear function.
7) Median filters are edge preserving smoothing filters, where the level is set
to the median of pixel values in the neighborhood of that pixel.
8) Min filter removes salt noise (white dots with large grey level values)
because any large grey level with in a 3 3 filter region is replaced by one
of its surrounding pixels with smallest value. As a side effect, min filter
introduces dark structures in the image. The reverse effect is expected
from a max filter. It removes pepper noise (black dots with small grey
level values) because any black dot within 3 3 filter region is replaced by
one of its surrounding pixels with the largest value. White dots/bright
structures are widened as a side effect and black dots (pepper noise) will
disappear.
4.9 SOLUTIONS/ANSWERS
E1) Linear filtering is a spatial domain process where a filter
(mask/kernel/template/window) with some coefficient values is applied to
input image to generate the filtered/ output image. Generally, filter size is
either 3 3 or 5 5, 7 7 or 21 21 (odd sizes) and filter is centered at a
coordinate ( x , y) called ‘ Hot Spot’.
E2) Smoothing linear filters are used to reduce ‘sharp’ transitions in grey
levels, reduce noise, bluredges, help in smoothing false colours, reduce
‘irrelevant’ details in an image.
Sharpening linear filters are used to detect edges, lines, points in the
image. It is also used to highlight all high frequency components in the
image.
161
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
1
g ( x , y) f ( m, n ) w ( m, n )
M ( m ,n )Sxy
1 1 1 1 2 1
1 1
Various masks are 1 1 1
9 16 2 4 2
1 1 1 1 2 1
1) Position the mask over the current pixel such that hotspot w(0,0)
coincides with current pixel.
2) Form all products of filter elements with the corresponding elements
in the neighbourhood.
3) Add up all the products and store it at current position in the output
image. This must be repeated for every pixel in the image.
E4) The aim of image smoothing is to diminish the effect of camera noise,
spurious pixel values, missing pixel values etc.
E5) Median filters are edge preserving smoothing filters, where the level is
set to the median of pixel values in the neighborhood of that pixel. It is
impossible to design a filter that removes only noise and retains all the
important image structures intact, because no filter can discriminate
which image content is important to the viewer and which is not. Median
filter replaces every image pixel by median of the pixels in the
corresponding filter region Sxy.
g ( x , y) min{f ( x i, y j) (i, j) S xy }
g ( x , y) max {f ( x i, y j) (i, j) S xy }
E7) Linear filters produce an output that is a linear combination of the input.
For example, smoothing linear filters produce output as the average of
input while sharpening linear filters produce output as the first or second
derivative of the input.
Non linear filters produce a statistical output where median filter produce
output as median of the input values, min filter produces output as
minimum of all input values and max filter produces output that is
maximum of all input values.
2 f ( x , y) 2 f f 2
E8) 2 f 2 2
x 2 y 2 x y
162
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
E9)
1 2 1 0 1 0 1 1 1
2 4 2 1 4 1 1 8 1
1 2 1 0 1 0 1 1 1
1 2 1 0 1 0 1 1 1
2 4 2 1 4 1 1 8 1
1 2 1 0 1 0 1 1 1
f ( x , y) f f
f
xy x y
2 f ( x, y) 2 f f 2
2 f 2 2
x 2 y 2 x y
E13)
1 0 1 1 1 0 0 1
1 0 0 0 0 1 1 0
Robert operator
E15) Histogram is a graph between various grey levels on x -axis and the
number of times a grey level has occurred in an image, on y -axis.
Histogram of an image is defined as
nk
h (rk ) , 0.............L 1
k
rk k th grey level
n k number of birds grey level values as rk .
E16) The histogram of an image represents the relative frequency of
occurrence of the various gray levels in the image. It also provides a
useful image statistics which are helpful in various image processing
applications, for example, thresholding, intensity level slicing,
segmentation etc. Besides it is very simple to calculate histogram.
Intuitively, it tells how vivid or washed out an image appears.
Histograms are the basis for many spatial domain processing
techniques.
E17) Histogram equalization is done by the transformation
k
s k T (rk ) p r (rj ) , k 0,.......L 1
j 0
164
UNIT 4 IMAGE FILTERING OPERATIONS IN SPATIAL DOMAIN
Goal: To obtain a uniform histogram for the output image. To device a
point operation s T ( r ) that maps input image f ( x , y) to an output
image g ( x, y) that has a flat histogram
Step 3 k nj 8 8 8 8 39 55 63 64
Sk
j 0 n 64 64 64 64 64 64 64 64
Step 4 sk 7 8 8 8 8 39 55 63 64
7 7 7 7 7 7 7 7
64 64 64 64 64 64 64 64
Step 6: Map input levels rk to output levels s k using 1st and last row of
table above to make table below.
rk 0 1 2 3 4 5 6 7
sk 1 1 1 1 4 6 7 7
Step 7: Formulate a new image g ( x , y) by replacing each level of
f ( x , y) by a new one using table.
rk 0 1 2 3 4
p(rk ) .2 .2 .2 .2 .2
sk .2 .4 .6 .8 1
rk 0 1 2 3 4
p(rk ) .2 .2 .2 .2 .2
sk .2 .4 .6 .8 1
1) q 0, k 0 ( v 0 s 0 ) (0 .2) 0 No increase q
q 1, k 0 ( v1 s 0 ) (.2 .2) 0 Yes v *0 v1 .2 and p 0 z1 2
5) q 3, k 4 ( v 3 s 4 ) (1 1) 0 Yes v * 4 v 3 1 and p 4 z 3 5
Step 4: Input and output grey level mapping from 2nd and last row of
previous output
r 0 1 2 3 4
p 2 4 4 5 5
166