4. Image Processing
in Spatial Domain
• Image processing approaches fall into two broad categories:
1. Spatial domain methods
2. Frequency domain methods
• Spatial domain refers to the image itself, approaches in this category
are based on direct manipulation of the pixels in an image
• Frequency domain methods are based on modifying the Fourier
transform of an image
• Filtering is one of many image processing techniques that can be
used for image enhancement or feature extraction such as edge,
line, etc.
5. Image Filtering
Image filtering: compute function of local neighborhood at each position
Examples:
• Smoothing by averaging
form the average of pixels in a neighborhood
• Smoothing with a Gaussian
form a weighted average of pixels in a neighborhood
• Finding a derivative
form a weighted average of pixels in a neighborhood
6. Linear Filters
• General process:
Form new image whose pixels are a weighted sum of original pixel values
(neighborhood), using the same set of weights at each point.
h(k,l) called the weight kernel or filter coefficients. The above correlation operator
can be notated as
• Properties:
1. Output is a linear function of the input
2. Output is a shift-invariant function of the input (i.e. shift the input image two
pixels to the left, the output is shifted two pixels to the left)
g(i, j) = f (i+k, j +l)h(k,l)
k,l
å
g = f Ä h
7. Spatial Filtering Process
r s t
u v w
x y z
Origin x
y Image f (x, y)
eprocessed = v*e +
r*a + s*b + t*c +
u*d + w*f +
x*g + y*h + z*i
Filter
Simple 3*3
Neighbourhood
e 3*3 Filter
a b c
d e f
g h i
Original
Image Pixels
*
The above step is repeated for every pixel in the original image to
generate the filtered image
8. Smoothing Spatial Filters
• One of the simplest spatial
filtering operations we can
perform is a smoothing
operation
• Simply average all of the
pixels in a neighborhood
around a central value
• Especially useful
in removing noise
from images or highlighting
gross detail
1/9
1/9
1/9
1/9
1/9
1/9
1/9
1/9
1/9
3x3 filter
Input image After 11x11 averaging
filter applied
9. Weighted Smoothing Filters
• More effective smoothing
filters can be generated by
allowing different pixels in
the neighborhood different
weights (example: Gaussian)
in the averaging function
• Pixels closer to the
central pixel are more
important
• Often referred to as a
weighted averaging
1/16
2/16
1/16
2/16
4/16
2/16
1/16
2/16
1/16
3x3 filter
Input image After 11x11 Gaussian
filter applied
10. Dealing with Edge Pixels
• At the edges of an image we
are missing pixels to form a
neighborhood
Origin x
y Image f (x, y)
e
e
e
e
e e
e
11. Dealing with Edge Pixels
• There are a few approaches to
dealing with missing edge pixels:
– Omit missing pixels
• Only works with some filters
• Can add extra code and slow
down processing
– Pad the image
• Typically with either all white
or all black pixels
– Replicate border pixels
– Truncate the image
– Allow pixels wrap around the
image
• Can cause some strange image
artefacts
Original
Image
Filtered Image:
Zero Padding
Filtered Image:
Wrap Around
Edge Pixels
Filtered Image:
Replicate Edge
Pixels
14. First Derivative
• The formula for the 1st derivative of a function is as follows:
• It’s just the difference between subsequent values and measures the
rate of change of the function
)
(
)
1
( x
f
x
f
x
f
17. Second Derivative
• The formula for the 2nd derivative of a function is as follows:
• Simply takes into account the values both before and after the current
value
)
(
2
)
1
(
)
1
(
2
2
x
f
x
f
x
f
x
f
19. Second Derivatives
For Image Enhancement
• The 2nd derivative is more useful for image enhancement
than the 1st derivative
– Stronger response to fine detail
– Simpler implementation
• The first sharpening filter we will look at is the Laplacian
– Isotropic
– One of the simplest sharpening filters
20. The Laplacian
• The Laplacian is defined as follows:
• where the partial 2st order derivative in the x direction is defined as
follows:
• and in the y direction as follows:
y
f
x
f
f 2
2
2
2
2
)
,
(
2
)
,
1
(
)
,
1
(
2
2
y
x
f
y
x
f
y
x
f
x
f
)
,
(
2
)
1
,
(
)
1
,
(
2
2
y
x
f
y
x
f
y
x
f
y
f
21. The Laplacian
• So, the Laplacian can be given as follows:
• We can easily build a filter based on this
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 -4 1
0 1 0
22. The Laplacian (example)
• Applying the Laplacian to an image we get a new image that highlights
edges and other discontinuities
Original Image Laplacian filtered Image
23. Laplacian Filter
for Image Sharpening
• The result of a Laplacian filtering is not an enhanced image
• We have to do more work in order to get our final image
• Subtract the Laplacian result from the original image to generate our
final sharpened enhanced image f
y
x
f
y
x
g 2
)
,
(
)
,
(
- =
24. Simplification of
Sharpening Steps
• The entire enhancement can be combined into a single filtering
operation
)
,
1
(
)
,
1
(
[
)
,
( y
x
f
y
x
f
y
x
f
)
1
,
(
)
1
,
(
y
x
f
y
x
f
)]
,
(
4 y
x
f
f
y
x
f
y
x
g 2
)
,
(
)
,
(
)
,
1
(
)
,
1
(
)
,
(
5 y
x
f
y
x
f
y
x
f
)
1
,
(
)
1
,
(
y
x
f
y
x
f
27. First Derivative Filtering
• Implementing 1st derivative filters is difficult in practice
• For a function f(x, y) the gradient of f at coordinates (x, y) is given as
the column vector:
y
f
x
f
G
G
y
x
f
28. First Derivative Filtering
• The magnitude of this vector is given by:
• For practical reasons this can be simplified as:
)
f
(
mag
f
2
1
2
2
y
x G
G
2
1
2
2
y
f
x
f
y
x G
G
f
29. First Derivative Filtering
• There is some debate as to how best to calculate these gradients but
we will use:
• which is based on these coordinates
3
2
1
9
8
7 2
2 z
z
z
z
z
z
f
7
4
1
9
6
3 2
2 z
z
z
z
z
z
z1 z2 z3
z4 z5 z6
z7 z8 z9
30. Sobel Operators
• Based on the previous equations we can derive the Sobel Operators
• To filter an image it is filtered using both operators the results of
which are added together
-1 -2 -1
0 0 0
1 2 1
-1 0 1
-2 0 2
-1 0 1
32. Acknowledgment
Some of slides in this PowerPoint presentation are adaptation from
various slides, many thanks to:
1. Dr. Brian Mac Namee, School of Computing at the Dublin Institute
of Technology (http://www.comp.dit.ie/bmacnamee/gaip.htm)