0% found this document useful (0 votes)
9 views47 pages

DIP04-Image Enhancement-Spatial Filtering

This lecture covers spatial domain filtering in image processing, including concepts of correlation, convolution, and various types of filters such as smoothing linear filters and median filters. It explains how filtering replaces pixel values based on operations applied to neighborhoods of pixels to enhance or suppress certain image features. The lecture also discusses limitations of point operations and the importance of spatial context in image filtering techniques.

Uploaded by

Vu Phan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views47 pages

DIP04-Image Enhancement-Spatial Filtering

This lecture covers spatial domain filtering in image processing, including concepts of correlation, convolution, and various types of filters such as smoothing linear filters and median filters. It explains how filtering replaces pixel values based on operations applied to neighborhoods of pixels to enhance or suppress certain image features. The lecture also discusses limitations of point operations and the importance of spatial context in image filtering techniques.

Uploaded by

Vu Phan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Lecture 04

Spatial Domain Filtering


Contents

In this lecture we will look at spatial domain filtering:


 What is image filtering?
 Correlation
 Convolution
 Smoothing linear filter
 Median filter
 Sharpening

Image Filtering 2
What is filtering

 Filtering is the process of replacing a pixel with a value


based on some operations or functions.
 The operations/functions used on the original image
are called filters.
• or masks, kernels, templates, windows…
 In digital image processing, filters are usually used to
• suppress the high frequencies in an image
- i.e., smoothing the image

• suppress the low frequencies in an image


- i.e., enhancing or detecting edges in the image

Image Filtering 3
What is image filtering

f(x,y) g(x,y)

filtering

filtering

Image Filtering 4
Image filtering methods

 Spatial domain

 Frequency domain

Image Filtering 5
Spatial domain methods

Image Filtering 6
Point processing methods

 Convert a given pixel value to a new pixel value


based on some predefined function

Image Filtering 7
Point processing methods-Example

Negative Contrast stretching

Thresholding

Histogram
Equalization

Image Filtering 8
Limitations of Point Operations

 They don’t know where they are in an image


 They don’t know anything about their neighbors
 Most image features (edges, textures, etc) involve a
spatial neighborhood of pixels
 If we want to enhance or manipulate these features,
we need to go beyond point operations

Image Filtering 9
What Point Operations Can’t Do

 Blurring/Smoothing

Image Filtering 10
What Point Operations Can’t Do

 Sharpening

Image Filtering 11
Spatial Filters

 The general processes can be denoted by the


expression:
g ( x, y)  T [ f ( x, y)]
• f(x,y) is the input image
• g(x,y) is the processed image
 T is an operator on f, defined over some
neighborhood of (x,y)
• use a subimage area centered at (x,y)
• shapes of the neighborhood
- Circle, square, rectangular

Image Filtering 12
Spatial Filters

 Example: 3×3 neighborhood about a point (x,y) in an


image
x

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

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

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

Image Filtering 13
Spatial Filters
x

w(-1,-1) w(0,-1) w(1,-1)


Mask

w(-1,0) w(0,0) w(1,0)


Image f(x,y)
w(-1,1) w(0,1) w(1,1)
y
f(x-1,y-1) f(x,y-1) f(x+1,y-1) Mask coefficients

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

Pixels under mask


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

Image Filtering 14
Operation

 Typically linear combinations


of pixel values.
• e.g., weight pixel values and
add them together.

 Different results can be


obtained using different
weights.
• e.g., smoothing, sharpening,
edge detection

Image Filtering 15
Example

10 5 3 0 0 0
4 6 1 0 0.5 0 8
1 1 8 0 1 0.5
Local image mask Modified image data
neighborhood

Image Filtering 16
Correlation

 A filtered image is generated as the center of the mask


visits every pixel in the input image.
n x n mask
h(i,j)

g(i,j)

f(i,j)

filtered
image

Image Filtering 17
Handling pixels close to boundaries

pad with zeroes wrap around


0 0 0 ……………………….0
0 0 0 ……………………….0

or

Image Filtering 18
Geometric interpretation of correlation

 Suppose x and y are two n-dimensional vectors:

x  ( x1 , x2 ,..., xn ) y  ( y1 , y2 ,..., yn )

 The dot product of x with y is defined as


x
x. y  x1 y1  x2 y2  ...  xn yn
using vector
θ
notation:
x . y | x || y |cos( )
y

 Correlation generalizes the notion of dot product

Image Filtering 19
Geometric interpretation of correlation

 cos(θ) measures the similarity between x and y


xy
x . y | x || y |cos( ) or cos( ) 
| x || y |
 Normalized correlation (i.e., divide by lengths)
n n
2 2

  h(k , l ) f (i  k , j  l )
n n
k  l 
N (i, j )  n n
2 2
n n
2 2 2 2
[  h 2 (k , l )]1/ 2 [   f 2 (i  k , j  l )]1/ 2
n n n n
k  l  k  l 
2 2 2 2

Image Filtering 20
Normalized correlation

 Measure the similarity between images or parts of


images.

mask

Image Filtering 21
Application example

Image Filtering 22
Convolution

 Same as correlation except that the mask is flipped,


both horizontally and vertically.
1 2 3 7 8 9 9 8 7
H V
4 5 6 4 5 6 6 5 4
7 8 9 1 2 3 3 2 1
n n n n
2 2 2 2
g (i, j )    h(k , l ) f (i  k , j  l )    h(i  k , j  l ) f (k , l )
n n n n
k  l  k  l 
2 2 2 2

 For symmetric masks (i.e., h(i,j)=h(-i,-j)),


convolution is equivalent to correlation! Notation:
h*f=f*h

Image Filtering 23
Convolution example

Convolution kernel, h Input Image, f

1 -1 -1 2 2 2 3
1 2 -1 2 1 3 3
1 1 1 2 2 1 2
Rotate 180o 1 3 2 2
1 1 1
-1 2 1
-1 -1 1

Image Filtering 24
Convolution example

1 1 1 2 2 2 3

-1 2 1 2 1 3 3

-1 -1 1 2 2 1 2
1 3 2 2
1 1 1
2
-1 4 2 2 3 5
Output
2 1
-1 -2 3 3 Image, g
2 2 1 2
1 3 2 2
Input Image, f
Image Filtering 25
Convolution example
2 2 2 3
1 1 1
2 1 3 3
-1 2 1
2 2 1 2
-1 -1 1
1 3 2 2
1 1 1
2 4
-2 2 2 3 5 4
Output
2 -1
-2 1 3 3 Image, g
2 2 1 2
1 3 2 2
Input Image, f
Image Filtering 26
Convolution example
1 1 1 2 2 2 3
 Same
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
1 3 2 2
1 1 1
2 -2
2 4
2 3 5 4 4
Output
2 -1
1 -3
3 3 Image, g
2 2 1 2
1 3 2 2
Input Image, f
Image Filtering 27
Convolution example
1 1 1 2 2 2 3
 Same
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
1 3 2 2
1 1 1
2 2 2 3
-2 6 1 5 4 4 -2
Output
2 1 3 -3
-3 3 1 Image, g
2 2 1 2
1 3 2 2
Input Image, f
Image Filtering 28
Convolution example
2 2 2 3
1 1 1
2 1 3 3
-1 2 1
2 2 1 2
-1 -1 1
1 3 2 2

1 2 2 2 3 5 4 4 -2
Output
-1 4
2 1 3 3 9 Image, g
-1 -2
2 2 1 2
1 3 2 2
Input Image, f
Image Filtering 29
Convolution example

5 4 4 -2
9 6 14 5
11 7 6 5
9 12 8 5
Final output Image, g

Image Filtering 30
Linear spatial filtering

R   (1, 1) f ( x  1, y  1)   (1, 0) f ( x  1, y)  ...


  (0, 0) f ( x, y )  ...   (1, 0) f ( x  1, y)   (1,1) f ( x  1, y  1)

 Smoothing Linear Filters : known as averaging


filter or lowpass filters.

Box filter Weighted


average filter
Image Filtering 31
Smoothing linear filters
Original 3x3

5x5 9x9

15x15 35x35

Image Filtering 32
Smoothing linear filters

original blurred thresholded

Image Filtering 33
Spatial Filters – smoothing, linear

 Mean filters 1 1 1
• example: 1
 1 1 1
9
1 1 1

Gaussian noise
Original 5×5
3×3 mean
mean filter
filter
Image Filters 34
Spatial Filters – smoothing, linear

 Mean filters 1 1 1
• example: 1
 1 1 1
9
1 1 1

Salt and pepper 3×3 mean filter


5×5
Image Filters 35
Spatial Filters – smoothing, linear

 Weighted average filters


• example: 1 2 1
1
 2 4 2
• general expression: 16
1 2 1

a b

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

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

Image Filters 36
Spatial Filters – smoothing, nonlinear

 Order-statistic filters

• nonlinear spatial filters

• order/rank the pixels contained in the image area


encompassed by the filter

Image Filters 37
Spatial Filters – smoothing, nonlinear

 Median filters
• replace a pixel value with the median of its neighboring
pixel values
• example:

23 25 26 30 40
Neighborhood values: 22 24 26 27 35
15, 19, 20, 23, 18 20 50 25 34
24, 25, 26, 27, 50 19 15 19 23 33
Median value: 24 11 16 10 20 30

Image Filters 38
Spatial Filters – smoothing, nonlinear

 Median filters
• have excellent noise-reduction capabilities

V.S.

Gaussian noise removed Gaussian noise removed


by 3×3 mean filter By 3×3 median filter

Image Filters 39
Spatial Filters – smoothing, nonlinear

 Median filters
• are particularly effective in salt & pepper

V.S.

Salt & pepper removed Salt & pepper removed


by 3×3 mean filter By 3×3 median filter
Image Filters 40
Spatial Filters – smoothing, nonlinear

 Max filters
• maximum of neighboring pixel values
• useful for finding the brightest points in an image

 Min filters
• minimum of neighboring pixel values
• useful for finding the darkest points in an image

Image Filters 41
Spatial Filters – sharpening

 Principal objective
• highlight fine detail in an image
• enhance detail that has been blurred
 Sharpening can be accomplished by spatial
differentiation
 For one dimensional function f(x)
• first order derivative
f
 f ( x  1)  f ( x)
x
• second order derivative
2 f
 f ( x  1)  f ( x  1)  2 f ( x)
x 2

Image Filters 42
Spatial Filters – sharpening

• A sample

(a) a scan line (b) image strip


(c) first derivative (d) second derivative

Image Filters 43
Spatial Filters – sharpening

 The Laplacian: second derivative of a two dimensional


function f(x,y)
 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)] -4f(x,y)
 Use a convolution mask to approximate

0 1 0 1 1 1 -1 2 -1
1 -4 1 1 -8 1 2 -4 2
0 1 0 1 1 1 -1 2 -1

Image Filters 44
Spatial Filters – sharpening

 The Laplacian example

Image Filters 45
Spatial Filters – sharpening

 The Laplacian
• example:

Image Filters 46
Summary

 We have looked at:


• Spatial domain operations
• Convolution and correlation
• Smoothing linear filter
• Median filter
• Laplacian

 Next time we will continue our talk about frequency domain


filtering

Image Filtering 47

You might also like