Nothing Special   »   [go: up one dir, main page]

Image Enhancement: Spatial Filtering

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 80

Image Enhancement:

Spatial Filtering 1 :
Introduction

1
Spatial domain

Techniques for improving contrast (Image


enhancement):

1.Gray-level transformations
2.Histogram processing
3.Spatial domain filters

2
What is filtering ?

 Refers to accepting or rejecting certain


frequencies
 Removing/eliminating any unwanted
information like noise

3
4
5
Filtering approaches

Spatial domain Frequency domain

6
Spatial domain filtering

7
Spatial domain filtering - Methods

 Smoothing linear filters


 Order-Statistic (Non-linear) filters
 Image sharpening filters

8
Smoothing linear filters

9
Averaging is a smoothing filter - an analogy
using 1 D signal?

10
Averaging

K can take any value


11
K =10 samples (window length)
12
Filtering using moving
average filter Y = [x(n)+x(n-
1)+x(n-2)….x(n-9)]/10

13
In similar way we can filter an image by
Averaging
 Averaging is an example of filtering in the
spatial domain

 In averaging, we choose a ‘mask’ of size m x


n (centered) on the current Pixel

 Replace the current value of the gray level by


the average gray level of all the pixels in the
mask

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

• Filtering can be given in


equation form as shown
below with the notations
based on the image
shown to the left
a b
g ( x, y )    w(s, t ) f ( x  s, y  t )
s   at   b

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)

 Mask of size m × n (odd sizes like 3×3, 5×5),


m=2a+1 and n =2b+1
Where a and b are non-negative integers

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

104 100 108 1 1 1


• Min: Set the pixel value to the minimum in
the neighbourhood 99 106 98

95 90 85
* 1

1
1

1
1

• Max: Set the pixel value to the maximum


Original Image Filter
in the neighbourhood Pixels

• Median: The median value of a set of


numbers is the midpoint value in that set
(e.g. from the set [1, 7, 15, 18, 24] 15 is
the median). Sometimes the median works
better than the average

23
Edges

• At the edges of an image some pixels are


missed to form a neighbourhood
Origin
e e

e e
Image f (x, y)
24
Edges (Cont…)

•There are a few approaches to deal with


missing pixels at the edges:
– Omit missing pixels
– Pad the image (with either all white or all black
pixels)
– Replicate border pixels

25
Need for a smoothing filter

26
Simple averaging mask

•Write down the mask for performing the smoothing


operation

27
Averaging -
Low pass
filtering effect

Why are edges


getting blurred

28
Image Smoothing Example

Mask size (square)


: 3,5,9,15,35

Noisy
rectangles
50*120
Vertical bars 5
pixels wide
and 100 pixels
long
Diameter of
circles 25
pixels

29
Weighted Smoothing Filters

•More effective smoothing filters can be generated by


allowing different pixels in the neighbourhood different
weights in the averaging function
•Pixels closer to the
central pixel are more
important (MORE weight)

•Also called as weighted


averaging

30
Weighted average

Weighted averaging is done to reduce the effect of


blurring of edges (which occurs in simple averaging)

31
32
Weighted average - Gaussian Filter

• For e.g. σ = 1 and


mean =0

33
Weighted average - Gaussian Filter

• For e.g. σ = 1 and


mean =0

34
Weighted average

35
Order-Statistics filters:

 Non-linear based on ordering or ranking

 Median is 50th percentile


 Max filter 100th percentile
 Min filter 0th percentile

36
Median Filtering -Steps

 3 x 3 Median Filter:

 Arrange the 9 gray values which fall within the


filter in ascending/descending order.

 Choose the 5th value in the list. This is by


definition, the median of the 9 numbers

 Replace the current pixel with this median value


37
Order-Statistics filters:
Eg: Median filter: Replace current gray value by the median of all
the gray values in the mask.

38
3 x 3 Median Filter:
• Median filter is very good at eliminating salt and
pepper noise (impulse noise)

•Does not lead to blurring of edges (as much as simple


averaging filters)

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)

 Therefore sharpening is accomplished by


spatial differentiation

43
Image Sharpening Spatial Filters

 Based on first and second derivative filters

 First derivative - Differentiation in the discrete


domain:

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

• zero in the areas of constant gray level

• non-zero at the onset of step/ramp

• non-zero along ramp

49
Second Derivative

2 f
 f ( y  1)  f ( y  1)  2 f ( y )
y 2

50
Properties of 2nd Derivative

• zero in the flat areas of gray level


• non-zero at the start/end of step/ramp
• zero along ramps of constant slope

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

 Second derivative has strong


response to fine detail, thin
lines, noise

 Second derivatives produce


double edges at step and ramp

 The sign of the second


derivative can be used to
identify the transition of an
edge from light to dark or dark
to light

55
Why Second Derivative
• The 2nd derivative is more useful for image
enhancement than the 1st derivative
– Stronger response to fine detail
– Simpler implementation

• The first sharpening filter is the Laplacian


– Isotropic
– One of the simplest sharpening filters
– Easy to implement digitally

56
The Laplacian

The Laplacian is defined as follows:


 2
f  2
f
 f  2  2
2

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

and in the y direction as follows:


2
 f
 f ( x, y  1)  f ( x, y  1)  2 f ( x, y )
y 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

In the final sharpened image edges and other


fine detail are much more obvious

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

Where f(x,y) is the original image

 2 f ( x, y ) is Laplacian filtered image

g(x,y) is the sharpened image

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.

 The Prewitt masks are simpler to implement than the


Sobel masks, but the latter have slightly superior noise-
suppression characteristics.

 Computation of the gradient involves combining the


result of Gx and Gy. However this involves squares and
square roots. An easier implementation is to use:

f  G x  G y
78
79
80

You might also like