Image Enhancement
(Local Operations)
Awais Muhammad Lodhi
Contents
In this lecture we will look at spatial filtering techniques:
Neighbourhood operations
What is spatial filtering?
Smoothing operations
What happens at the edges?
Correlation and convolution
Neighbourhood Operations
Neighbourhood operations simply operate on a larger
neighbourhood of pixels than point operations
Neighbourhoods are Origin x
mostly a rectangle
around a central pixel
Any size rectangle
and any shape filter Neighbourhood
(x, y)
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 e 3*3 Filter Pixels
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
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 1
/9 1
/9 1
/9
Especially useful Simple
in removing noise 1
/9 1
/9 1
/9 averaging
from images
1
/ 1
/ 1
/ filter
Also useful for 9 9 9
highlighting gross
detail
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
Simple 3*3 /9 106
1
99 1
/9 198
/9 3*3 Smoothing Original Image Filter
Neighbourhood /9
1
95 1
/9
90 1
/9
85 Filter Pixels
e= /9*106
1
+
1
/9*104 + 1/9*100 + 1/9*108 +
y Image f (x, y) 1
/9*99 + 1/9*98 +
1
/9*95 + 1/9*90 + 1/9*85
= 98.3333
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
/16 2/16 1/16
Pixels closer to the
central pixel are more 2
/16 4/16 2/16
important 1
/16 2/16 1/16
Often referred to as a
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
Filtering is often used to remove noise from images
Sometimes a median filter works better than an averaging
filter
Original Image Image After Image After
With Noise Averaging Filter Median 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
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 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 artifacts
Different Types of Padding
• None
• Zero Padding ( Black borders)
• 255 Padding (White Borders)
• Fixed Value Padding (Gray Padding)
• Replicate
• Mirror
• Wrap around
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
Strange Things Happen At The Edges!
(cont…)
Strange Things Happen At The Edges!
(cont…)
Strange Things Happen At The Edges!
(cont…)
Strange Things Happen At The Edges!
(cont…)
Strange Things Happen At The Edges!
(cont…)
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
eprocessed = v*e +
a
d
b
e
c
e
*
r
u
s
v
t
w z*a + y*b + x*c +
f g h x y z w*d + u*e +
t*f + s*g + r*h
Original Image Filter
Pixels
For symmetric filters it makes no difference
applyFilter (image, filter, padding_type, convolve=False)
Image: 2d np array
Filter:2d np array
Padding Type:
None
0-Padding
255-Padding
Convolve: boolean
Return: 2d np array (output image)
applyStatisticalFilter (image, filter_size, padding_type, method)
Image: 2d np array
Filter_size: shape (rows, cols)
Padding Type:
None
0-Padding
255-Padding
Method: Min
Max
Median
Average
Return: 2d np array (output image)
padImage(Image, filter_shape, padding_type)
1/2 of cols on left + 1/2 cols on right
1/2 of rows on top + 1/2 rows on bottom
Zero Padding
Padded_img = np.zeros(shape = (new_row_count, new_col_count), dtype = np.uint8)
255 Padding
Padded_img = 255 * np.ones(shape = (new_row_count, new_col_count), dtype =
np.uint8)
Padded_img[2:-2, 2:-2] = image
padImage(Image, filter_shape, padding_type)
1/2 of cols on left + 1/2 cols on right
1/2 of rows on top + 1/2 rows on bottom
Zero Padding
Padded_img = np.zeros(shape = (new_row_count, new_col_count), dtype = np.uint8)
255 Padding
Padded_img = 255 * np.ones(shape = (new_row_count, new_col_count), dtype =
np.uint8)
Padded_img[2:-2, 2:-2] = image
Get_window(pad_img, filter_shape, row, col)
Pad_image: 2d np array for the padded image
Filter_shape: window shape to be extracted
Row, col: point in the padded image where the window must be centered
Return: 2d np array of size filter_shape
corelate(window, filter)
Both window and filter are of same shape
Point multiply and sum and return single value
corelate(window, filter)
Both window and filter are of same shape
Invert the filter
Point multiply and sum and return single value
minFilter(window, filter_shape)
window shape is same as filter_shape
Finds and returns the min value from the window
maxFilter
medianFilter
averageFilter
Summary
In this lecture we have looked at the idea of spatial filtering
and in particular:
Neighbourhood operations
The filtering process
Smoothing filters
Dealing with problems at image edges when using
filtering
Correlation and convolution
Next time we will looking at sharpening filters and more
on filtering and image enhancement