Вы находитесь на странице: 1из 19

Spatial Filtering

Lecture 3

Sankalp Kallakuri
elsanky@gmail.com
Spatial Filtering
• Involves moving a kernel over the image to enhance the
image
• Kernel size is << Image size
• Odd sized masks are more common
Spatial Filtering
m(-1,-1) m(0,-1) m(1,-1)

f(x-1,y-1) f(x,y-1) f(x+1,y-1)


m(-1,0) m(0,0) m(1,0)

f(x-1,y) f(x,y) f(x+1,y) m(-1,1) m(0,1) m(1,1)

f(x-1,y+1) f(x,y+1) f(x+1,y+1)


Mask coefficients

Image pixels under mask

R = m(-1,-1) f(x-1,y-1) + m(0,-1) f(x,y-1) + m(1,-1) f(x+1,y-1) + ………..


a b
g ( x, y)    m(s, t ) f ( x  s, y  t )
s   at   b
Spatial Filtering
• The operation is similar to Convolution. hence the masks are also
called convolution masks.

• Non linear operations such as finding median may also be done on


a neighborhood.

• Near the edges parts of the masks may lie beyond the image
boundary.

• To avoid this either a smaller filtered image is accepted.

• Or zeros are padded along the image boundary.


Smoothing Spatial Filters
• Averaging or Low pass filters.
• Used to remove unwanted detail.
• Smooth out unwanted sharp noise transitions.
• May smooth out edges which should be sharp.

Examples

Box filter Weighted Average

1 1 1 1 2 1
1/9 x 1 1 1 1/16 x 2 4 2
1 1 1 1 2 1
Smoothing Spatial Filters

• The box filter does a simple average.


• The weighted average filter weighs the contribution by the
distance from the central image pixel.
Original Mathematical expression
a b

  m( s , t ) f ( x  s , y  t )
g ( x, y )  s   at   b
a b

  m( s , t )
s   at   b

3 6 9 12
Order Statistic Filters

• Median, Max and Min Filters


• Median Filters are particularly effective with salt pepper
noise or impulse noise.
• The image pixels under the mask need to be sorted.
• Min and max filters are used to find the brightest and
darkest points.

original Noise added Median filtered image


Sharpening Spatial Filters

• Use of first and second order derivatives.


• We need to know how they behave at discontinuities,
edges, ramps, noise points.
• First Derivative
1) zero in flat segments
2) must be non zero at onset of edge or ramp.
3)non zero along ramps
• Second Derivative
1) zero in flat segments
2) must be non zero at onset of edge or ramp.
3) must be zero along ramps of constant slope.
Sharpening Spatial Filters
First order derivative approximation f
 f ( x  1)  f ( x)
x
second order derivative approximation
2 f
 f ( x  1)  f ( x  1)  2 f ( x)
x 2

5432100060000131000077777
-1-1-1-1-1 0 0 6 -6 0 0 0 1 2 -2 -1 0 0 0 7 0 0 0 0
-1 0 0 0 0 1 0 6 -12 6 0 0 1 1 -4 -1 1 0 0 7 -7 0 0
Summary of First and Second Order
Derivatives
• First Order Derivatives Generally Produce Thicker
Edges.

• Second Order Derivatives have a stronger response to


finer detail.

• First Order response is generally stronger for a grey


level step.

• Second Order derivatives produce a double response at


step changes in grey level .
2nd Derivative - Laplacian
 2
f  2
f
• isotropic 2nd order operator  f  2  2
2

x y

Partial Double 2 f
Derivative in Y direction  f ( x, y  1)  f ( x, y  1)  2 f ( x, y )
y 2

2 f
Partial Double  f ( x  1, y )  f ( x  1, y )  2 f ( x, y )
Derivative in X direction x 2

2 f  [ f ( x  1, y)  f ( x  1, y)  f ( x, y  1)  f ( x, y  1)]  4 f ( x, y)
Digital implementation of the laplacian operator
Laplacian

0 1 0 1 1 1
1 -4 1 1 -8 1
0 1 0 1 1 1
Isotropic for 900 Isotropic for 450

Effect of diagonal elements is not considered in the first case


The image of the laplacian filter is usually added to the original to
improve the Features of the original.

f ( x, y)   2 f ( x, y)
g ( x, y ) =
{ f ( x, y)   2 f ( x, y)
-ve center coeff
+ve center coeff
Example : Laplacian

Laplacian Matlab Laplacian Hand Coded

Original Difference
Combined Implementation
g ( x, y)  f ( x, y)  [ f ( x  1)  f ( x 1, y) 
f ( x, y  1)  f ( x, y 1)]  4 f ( x, y)

g ( x, y)  5 f ( x, y )  [ f ( x  1)  f ( x  1, y ) 
f ( x, y  1)  f ( x, y  1)]

0 -1 0 -1 -1 -1
-1 5 -1 -1 9 -1
0 -1 0 -1 -1 -1

Isotropic for 900 Isotropic for 450


Example: combined implementation

laplacian

Original

Combined
Unsharp Masking and High Boost Filtering

Unsharp masking is to substract a smoothed out image from the original


f s ( x, y)  f ( x, y)  f ( x, y)

High boost is to boost up the original by factor A


f hb ( x, y)  Af ( x, y)  f ( x, y)
We can express the high boost in terms of the original and the sharpened
Image.
f hb ( x, y)  ( A  1) f ( x, y)  f ( x, y)  f ( x, y)
f hb ( x, y)  ( A  1) f ( x, y)  f s ( x, y)
0 -1 0

A f ( x, y)   f ( x, y)
2

{
-ve center coeff
-1 A+4 -1 f hb =
A f ( x, y)   f ( x, y)
2
+ve center coeff
0 -1 0

High boost filtering for A  1


Use of First Derivatives - Gradient

 f  The gradient as a vector


Gx  x 
f    =  
Gy  f 
 y 

f  [Gx  Gy ]
2 2 1/ 2
Magnitude of gradient

2 1/ 2
 f   f  
2

=      
 x   y  

f | Gx |  | Gy | Gradient approximated
By absolute values
Gradient Masks

0 -1 1 0 Robert cross
operators
1 0 0 -1

Sobel
-1 -2 -1 -1 0 1 Operators

0 0 0 -2 0 2

1 2 1 -1 0 1
HW 2-B
• Repeat HW 2 with a triangle cropping.
• Equilateral with the coordinates of
centroid same as coordinates of 3L/4,3L/4.
• One edge of the triangle is at 125 degrees
to the bottom edge of the image.
• Edge length of the triangle is 30.
• DUE at same time as HW -2.