Image Enhancement: Spatial Filtering
Image Enhancement: Spatial Filtering
Spatial Filtering 1 :
Introduction
1
Spatial domain
1.Gray-level transformations
2.Histogram processing
3.Spatial domain filters
2
What is filtering ?
3
4
5
Filtering approaches
6
Spatial domain filtering
7
Spatial domain filtering - Methods
8
Smoothing linear filters
9
Averaging is a smoothing filter - an analogy
using 1 D signal?
10
Averaging
13
In similar way we can filter an image by
Averaging
Averaging is an example of filtering in the
spatial domain
14
What is neighbourhood operation ?
15
Neighbourhood Operations
•Neighbourhood operations operate on a larger
neighbourhood of pixels not on the pixel itself
Origin x
•Neighbourhoods are
mostly a rectangle
around a central pixel
(x, y)
•Any size rectangle Neighbourhood
and any shape filter
are possible
y Image f (x, y)
16
Smoothing Spatial Filtering
Origin x
104 100 108 1
/9 1
/9 1
/9
* /9 /9 /9
1 1 1
99 106 98
95 90 85 1
/9 1
/9 1
/9
1
/9 100
104 1
/9 108
1
/9
Original Image Filter
Simple 3*3 /9 106
1
99 1
/9 198
/9
3*3 Smoothing Pixels
Neighbourhood /9 190
1
95 /9 185
/9 Filter
e = 1/9*106 +
1
/9*104 + 1/9*100 + 1/9*108 +
1
/9*99 + 1/9*98 +
y Image f (x, y) 1
/9*95 + 1/9*90 + 1/9*85
The above is repeated for every = 98.3333
pixel in the original
image to generate the smoothed image
17
Spatial Filtering in Equation
Form
18
Mechanics of spatial filtering
Mathematically
g(x,y) = ω (-1, -1)f(x-1, y-1)+ω (-1, 0)f(x-1, y)+
…….. ω (0, 0)f(x, y)+…. ω (1, 1)f(x+1, y+1)
a b
g ( x, y ) w(s, t ) f ( x s, y t )
s at b
Correlation and convolution 1D
Correlation and convolution in 2D (image)
correlation
Neighbourhood Operations
95 90 85
* 1
1
1
1
1
23
Edges
e e
Image f (x, y)
24
Edges (Cont…)
25
Need for a smoothing filter
26
Simple averaging mask
27
Averaging -
Low pass
filtering effect
28
Image Smoothing Example
Noisy
rectangles
50*120
Vertical bars 5
pixels wide
and 100 pixels
long
Diameter of
circles 25
pixels
29
Weighted Smoothing Filters
30
Weighted average
31
32
Weighted average - Gaussian Filter
33
Weighted average - Gaussian Filter
34
Weighted average
35
Order-Statistics filters:
36
Median Filtering -Steps
3 x 3 Median Filter:
38
3 x 3 Median Filter:
• Median filter is very good at eliminating salt and
pepper noise (impulse noise)
39
Other issues with median filter:
•Shifts edges which are not straight.
•Removes small features like thin lines and
points
40
Image sharpening spatial
filters
41
Sharpening spatial filters
Aim:
To highlight fine detail in an image or to enhance
detail that has been blurred for .e.g due to the
process adopted for image acquisition
42
Smoothing was accomplished by averaging
(adding = integration)
43
Image Sharpening Spatial Filters
f
f ( x 1) f ( x)
x
44
To obtain the second derivative, let us look at
the Taylor series of a function:
f h 2 2 f
f ( x h) f ( x ) h ....
x 2! x 2
Similarly
f h f
2 2
f ( x h) f ( x ) h ....
x 2! x 2
45
For a digital image h = 1 (smallest increment
possible along x or y), therefore adding these two
equations, we obtain
2 f
f ( x 1) f ( x 1) 2 f ( x)
x 2
f h 2 2 f
f ( x h) f ( x ) h ....
x 2! x 2
f h f
2 2
f ( x h) f ( x ) h ....
x 2! x 2
46
First derivative
f
f ( y 1) f ( y )
y
47
Edges
• An edge is a set of connected pixels that lie
on the boundary between two regions
48
Properties of 1st Derivative
49
Second Derivative
2 f
f ( y 1) f ( y 1) 2 f ( y )
y 2
50
Properties of 2nd Derivative
51
First derivative -Thick edge
Second derivative - double edge
52
First derivative Mask
f -1
f ( x 1, y ) f ( x, y )
x 1
f
f ( x, y 1) f ( x, y )
y -1 1
53
http://cis.cs.technion.ac.il/Done_Projects/Projects_done/VisionCl
asses/Vision/Edge_Detection/node5.html 54
Summary –First and second
derivatives
First derivatives produce
thicker edges
55
Why Second Derivative
• The 2nd derivative is more useful for image
enhancement than the 1st derivative
– Stronger response to fine detail
– Simpler implementation
56
The Laplacian
x y
where the partial 2nd order derivative in the x direction
is defined as follows:
2 f
f ( x 1, y ) f ( x 1, y ) 2 f ( x, y )
x 2
57
Discrete form of derivative
2 f
f(x,y-1) f(x,y) f(x,y+1) f ( x, y 1) f ( x, y 1) 2 f ( x, y )
y 2
f(x-1,y)
f(x,y)
2 f
f ( x 1, y ) f ( x 1, y ) 2 f ( x, y )
x 2
f(x+1,y)
58
2-Dimentional Laplacian
• The digital implementation of the 2-Dimensional
Laplacian is obtained by summing 2 components
2
f 2
f
2 f 2 2
x x
2 f f ( x 1, y ) f ( x 1, y ) f ( x, y 1) f ( x, y 1) 4 f ( x, y )
1 -4 1
59
Laplacian
2 f f ( x 1, y ) f ( x 1, y ) f ( x, y 1) f ( x, y 1) 4 f ( x, y )
0 1 0 1 1 1
1 -4 1 1 -8 1
0 1 0 1 1 1
1 0 1
0 -4 0
1 0 1
60
Negative Laplacian
2
f 2
f
f ( 2 2 )
2
x y
( f ( x 1, y ) f ( x 1, y ) f ( x, y 1) f ( x, y 1) 4 f ( x, y ))
62
The Laplacian (cont…)
• Applying the Laplacian to an image one can get a new
image that highlights edges as well as other discontinuities:
Original Laplacian
Image Filtered Image
63
The Laplacian (cont…)
Result of the Laplacian filtering is not an enhanced
image
To get the sharpened enhanced image, Laplacian result
has to be added to the original image as:
g ( x, y ) f ( x, y ) c f ( x, y )
2
64
Laplacian Image Enhancement
+ =
Original Laplacian Sharpened
Image Filtered Image Image
65
The Laplacian (cont…)
f ( x , y ) 2 f ( x, y ) If the center coefficient is negative
g ( x, y )
f ( x , y ) 2
f ( x, y ) If the center coefficient is positive
66
67
Application of Laplacian
fm=f-min(f)
Fs=k×(fm/max(fm)
Detecting edges in a particular direction
69
The Image Gradient and its Properties:
Recall:
The gradient of a function f(x,y) is
A vector which points in the direction of the
greatest rate of change of f at (x,y)
f
g x x
f f
g y
y
M ( x, y ) mag (f ) g x2 g 2y
1 g y
( x, y ) tan f G x G y
gx
70
1 1
0 0 1
0
71
Masks for this
operator
z1 z2 z3
z4 z5 z6
z7 z8 z9
Z8 - f(x,y)
-1 1
1
-1
Robert’s Cross gradient
Operators
z1 z2 z3
z4 z5 z6
z7 z8 z9
Gradient Operators
74
75
Gradient Operators
76
77
A weight of 2 is used in the Sobel mask to achieve
some smoothing.
f G x G y
78
79
80