Professional Documents
Culture Documents
Image Enhancement
(Spatial Filtering)
2 Contents
In this lecture we will look at spatial filtering
techniques:
– Neighbourhood operations
– Spatial filtering : Correlation and convolution
– Smoothing operations
– Sharpening filters
• 1st derivative filters
• 2nd derivative filters
– Combining filtering techniques
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
3 Neighbourhood Operations
Neighbourhood operations simply operate on a larger
neighbourhood of pixels than point operations
Origin x
Main idea
– Define a neighborhood of a pixel
– Define an operation on pixels in the
neighborhood (x, y)
Neighbourhood
– Output pixel value = result of the
operation
y Image f (x, y)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
5 The Spatial Filtering Process
Origin x
a b c r s t
d e f u v w
g h i * x y z
Original Image Filter
Simple 3*3 e 3*3 Filter Pixels
Neighbourhood
eprocessed = v*e +
r*a + s*b + t*c +
y Image f (x, y) u*d + w*f +
x*g + y*h + z*i
This is repeated for every pixel in the original image to generate the filtered image
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
6 Linear Spatial Filtering
• Create a filter or mask w, size m x n
• Apply to image f, size M x N
• Compute the sum of products of mask coefficients with corresponding
pixels under mask
• Slide mask over image, apply at each point
m/ 2 n/ 2
g(x, y) = w(s, t) f (x + s, y + t)
s=−m/ 2 t =−n/ 2
Also called “cross-correlation”
= w(x, y) f (x, y)
• It is a linear operation
w(x, y) ( a f (x, y)) = a w(x, y) f (x, y)
w(x, y) ( f (x, y) + g(x, y)) = w(x, y) f (x, y) + w(x, y) g(x, y)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
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 neighbourhood
around a central value
– Especially useful 1/
9
1/
9
1/
9
in removing noise
from images 1/
9
1/
9
1/
9 Simple
– Also useful for 1/ 1/ 1/
averaging
highlighting gross
9 9 9
‘box’ filter
detail
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
9 Averaging in 1D
Replace each pixel with an average of all the values
in its neighborhood – a moving average:
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
10 Averaging in 2D
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
11 What Happens At The Edges!
At the edges of an image we are missing
pixels to form a neighbourhood
Origin x
e e
e
e
e e e
y Image f (x, y)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
12 What Happens At The Edges! (cont…)
There are a few approaches to dealing with missing edge
pixels:
– Omit missing pixels
• 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
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
13
What Happens At The Edges!
Averaged Image:
Zero Padding
Averaged Image:
Wrap Around Edge Pixels
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
14 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 Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
15 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
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
16 Averaging as noise removal
Why Averaging
would help to
remove noise?
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
18 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 1/
16
2/
16
1/
16
0.5
2
0.4 Values should sum to 1
0.3
0.2 Mask size should be at
least ±3
0.1
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
• 2D − x +y2
2 2
h(x, y) = 1 e 2 0.01
2 2 0.008
0.006
0.004
0.002
0 30 40
40 20 30
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
20 Smoothing with a Gaussian
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
21 Smoothing with not a Gaussian
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
22 Gaussian filters
Variance (𝝈𝟐) or standard deviation (𝝈) – determines
extent of smoothing
>> surf(h);
>> imagesc(h);
im outim
>> outim = imfilter(im, h);
>> imshow(outim);
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
24 Varying Sigma
for sigma=1:3:10
h = fspecial('gaussian’, fsize, sigma);
out = imfilter(im, h);
imshow(out); pause;
end
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
25 Quiz
When filtering with a Gaussian, which is true:
A.The sigma is most important – it defines the blur
kernel’s scale with respect to the image.
B.The kernel size is most important – because it
defines the scale.
C.Altering the normalization coefficient does not
effect the blur, only the brightness.
D. A and C.
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
26
Filters as Templates
• Another use of filters can be as templates of certain
“interesting” regions in an image (like some object,
shape etc)
• By doing a spatial correlation of the filter/template
with the image one can localize/detect those
“interesting” areas in an image by looking at strong
responses of the filter at various locations in the
image
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
27 Vector representation of correlation
• Correlation is a sum of m/ 2 n/ 2
and f mn
= wk f k = w T f
• If images w and f are k=1
similar, i.e vectors are
aligned, then the
correlation score c is high
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
28 Template matching - A toy example
Template (mask)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
29 Weaknesses of correlation
Suppose we correlate a 1D filter:
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
30 Solution
• SSD – take sum of squared difference
w f
c = normxcorr2(onion,peppers);
figure, surf(c), shading flat;
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
32 Line Detection
How can we detect straight lines in a binary image ?
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
33 Line Detection (cont…)
Binary image of a wire
bond mask
After Result of
processing thresholding
with -45° line filtering result
detector
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
34 Template matching
Template
Scene
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
35 Template matching
Template
Scene
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
36 Quiz
Which of the following case would template matching work?
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
37 Correlation and Convolution
• Correlation
m/2 n/2
g(x, y) =
s=−m/ 2 t=−n/ 2
w(s, t) f (x + s, y + t)
= w(x, y) f (x, y)
• Convolution is very similar
• Convolution to correlation, except that
m/ 2 n/ 2 we flip one of the functions
g(x, y) = w(s,t) f (x − s, y − t)
• For symmetric filters it
s=−m/ 2 t=−n/ 2
s=−m/ 2 t=−n/ 2
w(s,t) f (x − s, y − t) =
s=−m/2 t=−n/ 2
w(−s, −t) f (x + s, y + t)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
39 Convolving with two Gaussians
• You can convolve a function with a Gaussian, then convolve again
with a Gaussian. The effect is the same as convolving with a single,
larger Gaussian
• Why? Since convolution is associative
g1 ( g2 f ) = (g1 g2 ) f
− x2 − x2
• and if
g1 = 1 e 212
, g2 = 1 e 2 22
2 1 2 2 i.e., it is a Gaussian
•
2
then − x with standard
e ( )
1 12+ 22
g1 g 2 =
2 deviation
2 ( + 2
1
2
2 ) new = 12 + 22
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
40 Computational Complexity
• If an image is NxN and a kernel (filter) is WxW,
how many multiplies do you need to compute a
convolution?
• You need O(N*N*W*W) = O(N2W2 )
• This can get huge if W is big enough
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
41 Separable kernels (filter masks)
• If a 2D convolution kernel can be separated into two 1D kernels,
the operation is much faster
– If h(x, y) = h1(x)h2 ( y)
– then h(x, y) f (x, y) = h1 (x) (h2 ( y) f (x, y) )
• Namely, do a 1D correlation with h2(y) along the individual columns
of the input image, then do a 1D correlation with h1(x) along the
rows of the result from the previous step
• If h(x,y) is nxn, and f(x,y) is NxN
– Cost of a 2D correlation?
– Cost of two 1D correlations?
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
42 Non Linear Filters
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
43 Max and Min Filter
Max Filter:
fˆ ( x, y) = max {g ( s, t )}
( s ,t )S xy
Min Filter:
fˆ ( x, y ) = min {g ( s, t )}
( s ,t )S xy
Image Image
Corrupted Corrupted
By Pepper By Salt
Noise Noise
Result Of Result Of
Filtering Filtering
Above Above
With A 3*3 With A 3*3
Max Filter Min Filter
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
45 Median Filter
Median Filter:
fˆ ( x, y) = median{g ( s, t )}
( s ,t )S xy
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
46 Median Filter
Median Filter:
– Doesn’t blur sharp edges in the image
12 12
10
10
8
8
6
6
4
2
2
0
-2 0
0 5 10 15 20 25 30 35 40 45 50 0 5 10 15 20 25 30 35 40 45 50
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
47 Averaging Filter Vs. Median Filter Example
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
48 Noise Removal Examples
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
49 Noise Removal Examples
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
50 Noise Removal Examples (contd)
Image Result of 1
Corrupted Pass With A
By Salt And 3*3 Median
Pepper Noise Filter
Result of 2 Result of 3
Passes With Passes With
A 3*3 Median A 3*3 Median
Filter Filter
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
51 Adaptive Median Filtering
• The median filter performs relatively well on
impulse noise as long as the spatial density of the
impulse noise is not large
• The adaptive median filter can handle much more
spatially dense impulse noise with less distortion
• The key insight in the adaptive median filter is that
the filter size changes depending on the
characteristics of the image region
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
52 Adaptive Median Filtering (cont…)
The adaptive median filter has following purposes:
– Remove spatially dense impulse noise
– Reduce distortion
First examine the following notation:
– zmin = minimum grey level in Sxy
– zmax = maximum grey level in Sxy
– zmed = median of grey levels in Sxy
– zxy = grey level at coordinates (x, y)
– Smax =maximum allowed size of Sxy
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
53 Adaptive Median Filtering (cont…)
Level A: A1 = zmed – zmin
A2 = zmed – zmax
If A1 > 0 and A2 < 0, Go to level B
Else increase the window size
If window size ≤ Smax repeat level A
Else output zmed
Level B: B1 = zxy – zmin
B2 = zxy – zmax
If B1 > 0 and B2 < 0, output zxy
Else output zmed
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
54 Simple Adaptive Median Filtering Example
Using only
level B and
filter 7 x 7
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
55 Adaptive Median Filtering Examples
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
56 Image sharpening
How about sharpening the image to highlight fine detail
– Remove blurring from images
– Highlight discontinuities (edges etc.)
How can you accomplish Sharpening by spatial filters?
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
57 Unsharp masking & Highboost filtering
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
58 Spatial Differentiation
• Image differentiation can help enhance edges
and other discontinuities (such as noise)
• Differentiation measures the rate of change of a
function.
• Thus, the strength of a derivative operator at any
location should be proportional to the degree of
discontinuity of the image at that location.
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
59 Spatial Differentiation
For 2D function, f(x,y), the partial derivative is defined as:
f ( x , y) f (x +,y) − f(x ,y )
= lim
x →0
For discrete data, we can approximate using finite
differences:
f (x, y)
f (x +1, y) − f (x, y)
• Right difference x
f (x, y)
f (x , y) − f (x-1, y)
• Left difference x
f
• Central difference ( f (x +1) − f (x −1)) / 2
x
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
60 2nd Derivative
The formula for the 2nd derivative of a function is as
follows:
2 f
= f ( x + 1, y ) + f ( x − 1, y ) − 2 f ( x, y )
x
2
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
61 1D example
Let’s consider a
simple 1
dimensional
example
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
63 The Laplacian
The first sharpening filter we will look at is the Laplacian
which is defined as follows: 2
f 2
f
2
f = +
x2
2 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
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
66 The Laplacian (cont…)
Applying the Laplacian to an image we get a new
image that highlights edges and other discontinuities
- =
Original Laplacian Sharpened
Image Filtered Image Image
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
70 Simplified Image Enhancement
The entire enhancement can be combined into a
single filtering operation
g ( x, y) = f ( x, y) − f 2
= 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)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
71 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
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
72 Simplified Image Enhancement (cont…)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
73 Summary
• We discussed various spatial domain image
enhancement techniques for
- contrast stretching
- removal of different types of noise
- Image smoothing and sharpening
• We also discussed using spatial filters as templates
for detecting objects and interesting shapes
• Next we will discuss methods for detecting edges
for image representation
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT