0% found this document useful (0 votes)
13 views73 pages

DIP-L4

The document discusses spatial filtering techniques used in image enhancement, focusing on neighbourhood operations like min, max, and median filtering. It explains the process of smoothing images to reduce noise and highlights the importance of weighted averaging filters. Additionally, it addresses challenges encountered at image edges and the concepts of correlation and convolution in filtering.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views73 pages

DIP-L4

The document discusses spatial filtering techniques used in image enhancement, focusing on neighbourhood operations like min, max, and median filtering. It explains the process of smoothing images to reduce noise and highlights the importance of weighted averaging filters. Additionally, it addresses challenges encountered at image edges and the concepts of correlation and convolution in filtering.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 73

DIP-Image Enhancement

Spartial Filtering
Neighbourhood Operations
•Neighbourhood operations simply operate on a
larger neighbourhood of pixels than point
operations Origin x

•Neighbourhoods are
mostly a rectangle
around a central pixel
•Any size rectangle Neighbourhood
(x, y)

and any shape filter


are possible
y Image f (x, y)
Simple Neighbourhood Operations
•Some simple neighbourhood operations
include:
– Min: Set the pixel value to the minimum in the
neighbourhood
– Max: Set the pixel value to the maximum in the
neighbourhood
– 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
Simple Neighbourhood Operations Example

Original Image x Enhanced Image x


123 127 128 119 115 130

140 145 148 153 167 172

133 154 183 192 194 191

194 199 207 210 198 195

164 170 175 162 173 151

y y
The Spatial Filtering Process
Origin x
a b c r s t
d
g
e
h
f
i
* u
x
v
y
w
z
Original Image Filter
Simple 3*3 Pixels
e 3*3 Filter
Neighbourhood
eprocessed = v*e +
r*a + s*b + t*c +
u*d + w*f +
y Image f (x, y) x*g + y*h + z*i

The above is repeated for every pixel in the original image to


generate the filtered image
Spatial Filtering: Equation Form
a b
g ( x, y )    w( s, t ) f ( x  s, y  t )
s  at  b

Filtering can be given


in equation form as
shown above
Notations are based on
the image shown to
the left
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
neighbourhood around a central value
– Especially useful
in removing noise
from images 1
/9 1
/9 1
/9
– Also useful for Simple
highlighting gross 1
/9 1
/9 1
/9 averaging
detail filter
1
/9 1
/9 1
/9
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
= 98.3333
The above is repeated for every pixel in the original image to
generate the smoothed image
Image Smoothing Example
•The image at the top left
is an original image of
size 500*500 pixels
•The subsequent images
show the image after
filtering with an averaging
filter of increasing sizes
– 3, 5, 9, 15 and 35
•Notice how detail begins
to disappear
Image Smoothing Example
Image Smoothing Example
Image Smoothing Example
Image Smoothing Example
Image Smoothing Example
Image Smoothing Example
Weighted Smoothing Filters
•More effective smoothing filters can be
generated by allowing different pixels in the
neighbourhood different weights in the
averaging function 1
/ 2
/ 1
/
16 16 16
– Pixels closer to the
central pixel are more 2
/16 4
/16 /16
2

important
– Often referred to as a 1
/16 2
/16 /16
1

weighted averaging
Weighted
averaging filter
Another Smoothing Example
•By smoothing the original image we get rid of
lots of the finer detail which leaves only the
gross features for thresholding

Original Image Smoothed Image Thresholded Image


Averaging Filter Vs. Median Filter Example

Original Image Image After Image After


With Noise Averaging Filter Median Filter

•Filtering is often used to remove noise from


images
•Sometimes a median filter works better than an
averaging filter
Averaging Filter Vs. Median Filter Example
Averaging Filter Vs. Median Filter Example
Averaging Filter Vs. Median Filter Example
Simple Neighbourhood Operations Example

x
123 127 128 119 115 130

140 145 148 153 167 172

133 154 183 192 194 191

194 199 207 210 198 195

164 170 175 162 173 151

y
Strange Things Happen At The Edges!
At the edges of an image we are missing pixels
to form a neighbourhood
Origin x
e e

e e e
y Image f (x, y)
Strange Things Happen At The Edges! (cont…)

•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
Simple Neighbourhood Operations Example

x
123 127 128 119 115 130

140 145 148 153 167 172

133 154 183 192 194 191

194 199 207 210 198 195

164 170 175 162 173 151

y
Strange Things Happen At The Edges!
(cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Filtered Image: Zero


Padding

Original Filtered Image:


Image Replicate Edge Pixels

Filtered Image:
Wrap Around Edge Pixels
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

(cont…)
Strange Things Happen At The Edges!
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

(cont…)
Strange Things Happen At The Edges!
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

(cont…)
Strange Things Happen At The Edges!
Correlation & Convolution
•The filtering we have been talking about so far
is referred to as correlation with the filter itself
referred to as the correlation kernel
•Convolution is a similar operation, with just one
subtle difference
a b c r s t eprocessed = v*e +
d
f
e
g
e
h
* u
x
v
y
w
z
z*a + y*b + x*c +
w*d + u*e +
t*f + s*g + r*h
Original Image Filter
Pixels

•For symmetric filters it makes no difference


Smoothing/ blurring Example
1/9 1/9 1/9
3 3 1 4 2 1 5 7
1/9 1/9 1/9
4 5 5 7 1 1 6 0
1/9 1/9 1/9
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3 filter
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1

image
Smoothing/ blurring Example….
1/9* 1/9 1/9
0 *0 *0
1/9 1/9
1/9*
*3 *3
1 4 2 1 5 7
0
1/9 1/9
1/9*
*4 *5
5 7 1 1 6 0
0
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9

1/9 1/9 1/9


1/9
*3(2 *3 *1
4 2 1 5 7
)
1/9
1/9 1/9 1/9
*4 *5 *5
7 1 1 6 0
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9

1/9 1/9 1/9


2 1/9
*3(2 *1 *4
2 1 5 7
)
1/9
1/9 1/9 1/9
4 *5 *5 *7
1 1 6 0
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9

1/9 1/9 1/9


2 2 1/9
*1(3 *4 *2
1 5 7
)
1/9
1/9 1/9 1/9
4 5 *5 *7 *1
1 6 0
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9

1/9 1/9 1/9


2 2 3 1/9
*4(2 *2 *1
5 7
)
1/9
1/9 1/9 1/9
4 5 5 *7 *1 *1
6 0
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9

1/9 1/9 1/9


2 2 3 2 1/9
*2(2 *1 *5
7
)
1/9
1/9 1/9 1/9
4 5 5 7 *1 *1 *6
0
2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9

1/9 1/9 1/9


2 2 3 2 2 1/9
*1(2 *5 *7
)
1/9
1/9 1/9 1/9
4 5 5 7 1 *1 *6 *0

2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9

1/9 1/9
2 2 3 2 2 2 1/9
*5(2 *7
1/9
)
1/9 1/9
1/9 1/9
4 5 5 7 1 1 *6 *0

2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
1/9 1/9 1/9

2 2 3 2 2 2 2 2 1/9 1/9 1/9

4 5 5 7 1 1 6 0 1/9 1/9 1/9

2 5 7 5 6 1 0 7
2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….

1/9 1/9
1/9 1/9
1/9
*3(2 *3(2
3 2 2 2 2 1
) )
1/9 1/9 1/9
1/9 1/9
*4 *5
5 7 1 1 6 0
1/9 1/9 1/9
1/9 1/9
*2 *5
7 5 6 1 0 7

2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….

1/9 1/9 1/9


*3(2
1/9 1/9
*3(2 *1(3
2
1/9 2 2 2 1
) ) )
1/9 1/9 1/9
1/9 1/9 1/9
*4(2 *5 *5
7 1 1 6 0
) 1/9 1/9 1/9
1/9 1/9 1/9
*2 *5 *7
5 6 1 0 7

2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….

1/9 1/9 1/9


2 *3(2 *1(3 *4(2
2 2 2 1
) ) )
1/9 1/9 1/91/91/9 1/9
2 *5(4 *5 *7
1 1 6 0
)
1/9 1/9 1/9
1/9 1/9 1/9
2 *5 *7 1/9 *5 1/9
6 1/9 1 0 7

2 3 4 1 7 2 7 5
1 1 6 6 2 1 3 3
0 7 5 4 3 4 3 6
6 5 4 3 2 1 0 0
2 3 4 2 1 6 0 1
Smoothing/ blurring Example….
3 3 1 4 2 1 5 7 2 2 3 2 2 2 2 2

4 5 5 7 1 1 6 0 2 4 5 4 3 3 2 1

2 5 7 5 6 1 0 7 1/9 1/9 1/9


2 4 4 4 4 3 3 3

2 3 4 1 7 2 7 5 1/9 1/9 1/9 1 3 4 5 4 3 3 2

1 1 6 6 2 1 3 3 1/9 1/9 1/9 1 3 5 4 3 3 3 2

0 7 5 4 3 4 3 6 2 4 4 4 3 2 2 1
filter
6 5 4 3 2 1 0 0 2 3 3 3 3 2 1 1

2 3 4 2 1 6 0 1 1 2 2 2 2 2 1 1

Smoothed
Image
Original
image
Assignment
• Write an algorithm to
smoothen any grayscale image
using a 5*5 smoothing mask.
Sharpening Spatial Filters
Previously we have looked at smoothing filters
which remove fine detail
Sharpening spatial filters seek to highlight fine
detail
– Remove blurring from images
– Highlight edges
Sharpening filters are based on spatial
differentiation
Spatial Differentiation
Differentiation measures the rate of change of a
function
Let’s consider a simple 1 dimensional example
Spatial Differentiation

A B
1 Derivative
st

The formula for the 1st derivative of a function is


f
as follows:
 f ( x  1)  f ( x)
x

It’s just the difference between subsequent


values and measures the rate of change of the
function
1 Derivative (cont…)
st

5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7

0 -1 -1 -1 -1 0 0 6 -6 0 0 0 1 2 -2 -1 0 0 0 7 0 0 0
2 Derivative (cont…)
nd

5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7

-1 0 0 0 0 1 0 6 -12 6 0 0 1 1 -4 1 1 0 0 7 -7 0 0
Using 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
– We will come back to the 1st order derivative later
on
The first sharpening filter we will look at is the
Laplacian
– Isotropic
– One of the simplest sharpening filters
– We will look at a digital implementation
The Laplacian
The Laplacian is defined as follows:
2 2
2  f  f
 f  2  2
 x  y

where the partial 1st order derivative in the x


direction is defined as follows:
2 f
2
 f ( x  1, y )  f ( x  1, y )  2 f ( x, y )
 x

and in the y direction as follows:


2 f
2
 f ( x, y  1)  f ( x, y  1)  2 f ( x, y )
 y
The Laplacian (cont…)
So, the Laplacian can be given as follows:
2
 f [ f ( x  1, y )  f ( x  1, y )
 f ( x, y  1)  f ( x, y  1)]
 4 f ( x, y )
We can easily build a filter based on this
0 1 0

1 -4 1

0 1 0
The Laplacian (cont…)
Applying the Laplacian to an image we get a new
image that highlights edges and other
discontinuities

Original Laplacian Laplacian


Image Filtered Image Filtered Image
Scaled for Display
But That Is Not Very Enhanced!
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
Laplacian
the original image to generate our Filtered Image
final sharpened enhanced 2image Scaled for Display

g ( x, y )  f ( x, y )   f
Laplacian Image Enhancement

- =
Original Laplacian Sharpened
Image Filtered Image Image

In the final sharpened image edges and fine


detail are much more obvious
Simplified Image Enhancement
The entire enhancement can be combined into a
single filtering operation
2
g ( x, y )  f ( x, y )   f
 f ( x, y )  [ f ( x  1, y )  f ( x  1, y )
 f ( x, y  1)  f ( x, y  1)
 4 f ( x, y )]
5 f ( x, y )  f ( x  1, y )  f ( x  1, y )
 f ( x, y  1)  f ( x, y  1)
Simplified Image Enhancement (cont…)
This gives us a new filter which does the whole
job for us in one step

0 -1 0

-1 5 -1

0 -1 0
Variants On The Simple Laplacian
There are lots of slightly different versions of the
Laplacian that can be used:
0 1 0 1 1 1
Simple Variant of
1 -4 1 1 -8 1
Laplacian Laplacian
0 1 0 1 1 1

-1 -1 -1

-1 9 -1

-1 -1 -1
1 Derivative Filtering
st

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:

 f 
 Gx   x 
f    f 
 Gy   
 y 
1st Derivative Filtering (Gradient
operations..…)
The magnitude of this vector is given by:
f mag (f )

 G G2
x
2
y  1
2

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

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

For practical reasons


f this
Gx can
 G ybe simplified as:
Gradient filters variables
• Roberts
• Based on implementing the diagonal
differences
f
gx
x
 ( z 9  z 5)
-1 0 0 -1
0 1 1 0
f
g y 
x
 ( z 8  z 6)
Gradient filters variables…
• Perwitt
• The differences between the third and first
rows of the 3*3 mask approximates the
derivative in the x-direction
• Likewise, the difference between the third
and first column approximate the derivative in
the y-direction.
Gradient filters variables……
f
gx 
x
 ( z 7  z 8  z 9 )  ( z1  z 2  z 3 )

f
g y 
y
 ( z 3  z 6  z 9 )  ( z1  z 4  z 7 )

-1 -1 -1 -1 0 1

0 0 0 -1 0 1

1 1 1 -1 0 1
Gradient filters variables…..
• Sobel operators
• Slight difference to Perwitt’s operators by
weighing the center locations which provides
smoothing effect.

-1 -2 -1 -1 0 1
f
g x 
x
 ( z 7  2 z 8  z 9 )  ( z1  2 z 2  z 3 )
0 0 0 -2 0 2

gy  fy  ( z 3  2 z 6  z 9 )  ( z1  2 z 4  z 7 )
1 2 1 -1 0 1
Sobel Example
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

An image of a
contact lens which
is enhanced in
order to make
defects (at four
and five o’clock in
the image) more
obvious

Sobel filters are typically used for edge detection


Combining Spatial Enhancement Methods
Successful image
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

enhancement is typically not


achieved using a single
operation
Rather we combine a range of
techniques in order to achieve
a final result
This example will focus on
enhancing the bone scan to
the right
Combining Spatial Enhancement Methods
(cont…)
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

(a)
Laplacian filter of
bone scan (a)
(b)
Sharpened version of
bone scan achieved (c)
by subtracting (a)
and (b) Sobel filter of bone
scan (a) (d)
Combining Spatial Enhancement Methods
(cont…)
Result of applying a (h)
power-law trans. to
Images taken from Gonzalez & Woods, Digital Image Processing (2002)

Sharpened image (g)


which is sum of (a)
and (f) (g)
The product of (c)
and (e) which will be (f)
used as a mask
(e)

Image (d) smoothed with


a 5*5 averaging filter
Combining Spatial Enhancement Methods
(cont…)
Compare the original and final images
Images taken from Gonzalez & Woods, Digital Image Processing (2002)
Assignment
• Read a grayscale image of your choice,
identify any defects or areas of concentration
and choose appropriate image enhancement
techniques to enhance the image. Use C, C++
or JAVA.
END OF IMAGE ENHANCEMENT

You might also like