Lec 5 Image Processing
Lec 5 Image Processing
(CSC 352)
2022 – 2023
Lecture-5
Dr: Mary monir saied
Email: mmh04@[Link]
Digital Image Fundamentals
Image Enhancement
Fundamental Steps of DIP
3
Main points of Image Enhancement
Image
Enhancement
Spatial Frequency
domain Domain
Smoothing Sharpening
First Second
Linear Nonlinear
Derivatives Derivatives 4
Fundamentals of Spatial Filtering
The name filter is borrowed from frequency domain processing
where “filtering” refers to passing, modifying, or rejecting
specified frequency components of an image.
6
The Mechanics of Linear Spatial Filtering
Spatial Filtering
• Use filter (can also be called as mask/kernel/template
or window)
• The values in a filter sub-image are referred to as
coefficients, rather than pixel.
• Masks of odd sizes, e.g. 3x3, 5x5,…
7
The Mechanics of Linear Spatial Filtering
The spatial domain processes are based on the expression
8
The Mechanics of Linear Spatial Filtering
Neighbourhood operations simply operate on a larger
neighbourhood of pixels than point operations
Neighbourhoods are mostly a rectangle around a central pixel
Any size rectangle and any shape filter are possible
9
The Mechanics of Linear Spatial Filtering
- Neighborhood
- Operator (filter/mask)
Origin Origin
x x
Operator
(x, y) (x, y)
11
The Mechanics of Linear Spatial Filtering
12
The Mechanics of Linear Spatial Filtering
The center coefficient of the kernel, w(0, 0), aligns with the
pixel at location (x, y).
w( s, t ) f ( x s, y t )
14
g ( x, y )
s at b
The Mechanics of Linear Spatial Filtering
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
15
The above is repeated for every pixel in the original image to
generate the filtered image
The Mechanics of Linear Spatial Filtering
Dealing with Image Borders
At the edges of an image we are missing pixels to
form a neighbourhood
Origin x
e e
16
e e e
y Image f (x, y)
The Mechanics of Linear Spatial Filtering
What happens at the edge of the image, where the mask partly
falls outside the image?
17
The Mechanics of Linear Spatial Filtering
What happens at the edge of the image, where the mask partly
falls outside the image?
The mask is only applied to those pixels in the image for with
the mask will lie fully within the image.
The output is smaller than the original.
If the mask is very large, a significant amount of information
may be lost by this method.
18
The Mechanics of Linear Spatial Filtering
What happens at the edge of the image, where the mask partly
falls outside the image?
2. Zero padding.
All values outside the image are zero.
Output image of the same size as the original.
It may consider unwanted artifacts like edges around the
image
19
The Mechanics of Linear Spatial Filtering
What happens at the edge of the image, where the mask partly
falls outside the image?
2. Zero padding.
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 2 3 4 5
0 0 1 2 3 4 5 0 0
6 7 8 9 10 0 0 6 7 8 9 10 0 0
11 12 13 14 15 0 0 11 12 13 14 15 0 0
0 0 16 17 18 19 20 0 0
16 17 18 19 20
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 20
The Mechanics of Linear Spatial Filtering
What happens at the edge of the image, where the mask partly
falls outside the image?
What happens at the edge of the image, where the mask partly
falls outside the image?
What happens at the edge of the image, where the mask partly
falls outside the image?
4. Replicate padding.
values outside the boundary are set equal to the nearest image
border value.
Is useful when the areas near the border of the image are
constant.
23
The Mechanics of Linear Spatial Filtering
What happens at the edge of the image, where the mask partly
falls outside the image?
4. Replicate padding.
1 1 1 2 3 4 5 5 5
1 1 1 2 3 4 5 5 5
1 2 3 4 5
1 1 1 2 3 4 5 5 5
6 7 8 9 10 6 6 6 7 8 9 10 10 10
11 12 13 14 15 11 11 11 12 13 14 15 15 15
16 16 16 17 18 19 20 20 20
16 17 18 19 20
16 16 16 17 18 19 20 20 20
16 16 16 17 18 19 20 20 20 24
The Mechanics of Linear Spatial Filtering
Filtered Image:
Zero Padding
Original
Image
Filtered Image:
Replicate Edge
Pixels 25
The Mechanics of Linear Spatial Filtering
Zero padding.
26
The Mechanics of Linear Spatial Filtering
Replicate padding.
27
Main points of Image Enhancement
Image
Enhancement
Spatial Frequency
domain Domain
Smoothing Sharpening
First Second
Linear Nonlinear
Derivatives Derivatives 28
Smoothing (Lowpass) Spatial Filters
❑ Smoothing spatial filters are used to reduce sharp transitions in
intensity.
31
Smoothing (Lowpass) Spatial Filters
Smoothing filters are called low-pass filters.
33
Smoothing (Lowpass) Spatial Filters
34
Smoothing (Lowpass) Spatial Filters
Smoothing Linear Filters
Box(Average )filter:
The simplest, separable lowpass filter
kernel is the box kernel, whose coefficients
have the same value (typically 1).
Simple averaging filter
An 𝒎 × 𝒏 box filter is an 𝒎 × 𝒏 array of
1’s, with a normalizing constant in front,
whose value is 1 divided by the sum of the
values of the coefficients (i.e., 1/𝑚𝑛 when
all the coefficients are 1’s).
For example: 3x3 average filter.
35
Smoothing (Lowpass) Spatial Filters
Smoothing Linear Filters
Box(Average )filter:
Origin x
104 100 108 1/ 1/ 1/
9 9 9
1/ 1/ 1/
99 106 98
95 90 85
* 1/
9 9
1/ 1/
9
9 9 9
1/ 100
104
9
1/ 108
9
1/
9
Original Image Filter
Simple 3*3 1/
999 1106
/9 198
/9
3*3 Smoothing Pixels
Neighbourhood 195
/9 190
/9 185
/9
Filter
e = 1/9*106 +
1/ *104 + 1/ *100 + 1/ *108 +
9 9 9
1/ *99 + 1/ *98 + 36
9 9
y Image f (x, y) 1/ *95 + 1/ *90 + 1/ *85
9 9 9
= 98.3333
Smoothing (Lowpass) Spatial Filters
Smoothing Linear Filters
Box(Average )filter:
Origin x
104 0 0 1/ 1/ 1/
9 9 9
1/ 1/ 1/
100
102
0
0
0
0
* 1/
9 9
1/ 1/
9
9 9 9
1/
104 1 1
9 0/9 0/9
Original Image Filter
Simple 3*3 1/ 1 1
3*3 Smoothing Pixels
1009 0/9 0/9
Neighbourhood 1/
102 1
9 0/9
1/ Filter
09
e = 1/9*104 +
1/ *100 + 1/ *102 + 1/ *0 +
9 9 9
1/ *0 + 1/ *0 + 37
9 9
y Image f (x, y) 1/ *0+ 1/ *0 + 1/ *0
9 9 9
= 34
Smoothing (Lowpass) Spatial Filters
Smoothing Linear Filters
38
Smoothing (Lowpass) Spatial Filters
Example
•The image at the top left
is an original image of
size 500*500 pixels
original image
40
Smoothing (Lowpass) Spatial Filters
Example
Average Filter
3x3
41
Smoothing (Lowpass) Spatial Filters
Example
Average Filter
5x5
42
Smoothing (Lowpass) Spatial Filters
Example
Average Filter
9x9
43
Smoothing (Lowpass) Spatial Filters
Example
Average Filter
15x15
44
Smoothing (Lowpass) Spatial Filters
Example
Average Filter
35x35
45
Smoothing (Lowpass) Spatial Filters
Smoothing Linear Filters
Weighted filter(Gaussian Filter):
Allowing different pixels in the
neighbourhood different weights in the
averaging function.
Pixels closer to the central pixel are more
important.
More effective in smoothing. Weighted averaging
filter
46
Smoothing (Lowpass) Spatial Filters
Origin x
104 100 108 1/ 2/ 1/
16 16 16
99 106 98
95 90 85
* 2/
1/
16
16
4/
2/
16
16
2/
1/
16
16
1/
104
16
2/
100 1/
16 10816
Original Image Filter
Simple 3*3 2/
/16 2/98
16 4106
99
3*3 Smoothing Pixels
16
Neighbourhood 1/
95
16
2
/16 1/
90 85
Filter
16
e = 4/16*106 +
1/ *104 + 2/ *100 + 1/ *108 +
16 16 16
2/ *99 + 2/ *98 +
16 16
y Image f (x, y) 1/ *95 + 2/ *90 + 1/ *85
16 16 16
= 99.375
47
Smoothing (Lowpass) Spatial Filters
Origin x
104 0 0 1/ 2/ 1/
16 16 16
100
102
0
0
0
0
* 2/
1/
16
16
4/
2/
16
16
2/
1/
16
16
1/
10416
2/
0 16 10/16
Original Image Filter
Simple 3*3 2/
10016 40/16 20/16
3*3 Smoothing Pixels
Neighbourhood 1/
102
2/16
16 0
1/ Filter
0 16
e = 4/16*0 +
1/ *104 + 2/ *100 + 1/ *102 +
16 16 16
2/ *0 + 2/ *0 +
16 16
y Image f (x, y) 1/ *0 + 2/ *0 + 1/ *0
16 16 16
= 25.375
48
Smoothing (Lowpass) Spatial Filters
49
Smoothing (Lowpass) Spatial Filters
Smoothing Linear Filters
• Highlighting gross detail: the size of the mask determines the relative
size of the objects blended.
50
Original Image Smoothed Image Thresholded Image
Main points of Image Enhancement
Image
Enhancement
Spatial Frequency
domain Domain
Smoothing Sharpening
First Second
Linear Nonlinear
Derivatives Derivatives 51
Smoothing Spatial Filters
Smoothing Nonlinear Filters
• Spatial filters whose response is based on ordering
(ranking) the pixels in the image area encompassed by
the filer, and replacing the center pixel with the value
determined by the ranking result.
• Examples:
– min filter, max filter, median filter.
52
Smoothing Spatial Filters
Smoothing Nonlinear Filters
Median Filter: sort pixels in neighborhood,
determine median, assign to center pixel.
Excellent salt-and-pepper noise
reduction(Impulse noise).
53
Smoothing Spatial Filters
Origin x
104 100 108
99 106 98
95 90 85
5th
y Image f (x, y)
The above is repeated for every pixel in the original image 54to
generate the smoothed image
Smoothing Spatial Filters
Median filter :
55
Smoothing Spatial Filters
Smoothing Nonlinear Filters
• Excellent salt-and-pepper noise reduction.
56
Original Image Image After Image After
With Noise Averaging Filter Median Filter
Smoothing Spatial Filters
Smoothing Nonlinear Filters
• Original
57
Smoothing Spatial Filters
Smoothing Nonlinear Filters
• Average
58
Smoothing Spatial Filters
Smoothing Nonlinear Filters
• Median
59
Main points of Image Enhancement
Image
Enhancement
Spatial Frequency
domain Domain
Smoothing Sharpening
First Second
Linear Nonlinear
Derivatives Derivatives 60
Sharpening Spatial Filters
Main objectives:
Highlight transitions in intensities
highlight edges.
Remove blurring enhance details.
Sharpening filters are based on spatial
differentiation, which measures the rate of
change of a function.
First and second derivatives are used for
image enhancement. 62
Sharpening Spatial Filters
63
Sharpening Spatial Filters
64
Sharpening Spatial Filters
1st Derivative:
The formula for the 1st derivative of a function
is as follows:
f
f ( x 1) f ( x)
x
It’s just the difference between subsequent
values and measures the rate of change of the
function. 65
Sharpening Spatial Filters
2nd Derivative:
The formula for the 2nd derivative of a
function is as follows:
f
2
f ( x 1) f ( x 1) 2 f ( x )
x
2
67
Sharpening Spatial Filters
Example in 1D:
68
Sharpening Spatial Filters
Example in 1D:
• Note that the sign of the 2nd derivative changes
at the onset and end of ramp and step zero
crossing useful in locating edges.
Image
Enhancement
Spatial Frequency
domain Domain
Smoothing Sharpening
First Second
Linear Nonlinear
Derivatives Derivatives 70
Sharpening Spatial Filters
Using the Second Derivative for image
sharpening- The Laplacian
The Laplacian, for a function (image) f(x, y)
of two variables, is defined and given below:
2
f 2
f
f 2 2
2
x y
71
Sharpening Spatial Filters
Second Derivative (Laplacian)
where in the x direction:
2 f
f ( x 1, y ) f ( x 1, y ) 2 f ( x, y )
x
2
f(x,y-1)
f(x-1,y) f(x,y) f(x+1,y)
f(x,y)
y
2
Sharpening Spatial Filters
Second Derivative (Laplacian)
Hence the Laplacian is given by:
f [ f ( x 1, y ) f ( x 1, y )
2
f ( x, y 1) f ( x, y 1)]
4 f ( x, y )
.
73
Sharpening Spatial Filters
Second Derivative (Laplacian)
Hence the Laplacian is given by:
0 1 0
1 -4 1
0 1 0 74
Sharpening Spatial Filters
Explain the effect of such mask on…
background region eliminate it
edge region show it
Ex.: 3×3 Laplacian filter on 3×3 image
X1 X2 X3 0 -1 0
X4 Y X5 -1 4 -1
X6 X7 X8 0 -1 0
Y Y Y Y
Result Explanation
-X2 -X4 -X5 -X7
•Post-processing: NORMALIZATION
Sharpening Spatial Filters
Second Derivative (Laplacian)
76
Sharpening Spatial Filters
Second Derivative (Laplacian)
Applying the Laplacian gives a new image that highlights
edges and other discontinuities.
Not enhanced
- =
79
Original Laplacian Sharpened
Image Filtered Image Image
Sharpening Spatial Filters
Second Derivative (Laplacian)
80
Sharpening Spatial Filters
Composite Laplacian Filter
This gives us a new filter which does the whole job for us in one
step.
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 ) 81
f ( x, y 1) f ( x, y 1)
Sharpening Spatial Filters
Composite Laplacian Filter
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
82
Sharpening Spatial Filters
Explain the effect of such mask on…
background region leave it
edge region sharp it
Ex.: 3×3 Laplacian filter on 3×3 image
X1 X2 X3 0 -1 0
X4 Y X5 -1 5 -1
X6 X7 X8 0 -1 0
Y Y Y Y Y
Result Explanation
-X2 -X4 -X5 -X7
If region is BG ≈0 Y ≈Y Leave BG region as it’s
Increase the difference
If Y > its
≈ large +ve value Y ≈ Y + value between Y and its neighbors
neighbor
(i.e. sharp it)
Increase the difference 83
If Y < its
≈ large -ve value Y ≈ Y - value between Y and its neighbors
neighbor
(i.e. sharp it)
Sharpening Spatial Filters
Second Derivative (Laplacian)
There are lots of slightly different versions:
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 84
-1 -1 -1
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
Laplacian vs. Composite Laplacian
Example: Apply the Laplacian and composite Laplacian filters
on the following blurred edge.
Sharpening Spatial Filters
2- First Derivative (Gradient)
The Gradient of f(x, y) at coordinates (x, y) is
defined as the column vector:
f
Gx x
f f
G y
y
96
Main points of Image Enhancement
Image
Enhancement
Spatial Frequency
domain Domain
Smoothing Sharpening
First Second
Linear Nonlinear
Derivatives Derivatives 97
Sharpening Spatial Filters
2- First Derivative (Gradient)
Approximating the partial derivative in the x-
direction == third row – first row
Approximating the partial derivative in the y-
direction == third col – first col
M ( x, y) z7 2 z8 z9 z1 2 z2 z3
z3 2 z6 z9 z1 2 z4 z7 z1 z2 z3
z4 z5 z6 99
z7 z8 z9
Sharpening Spatial Filters
2- First Derivative (Gradient)
Based on the previous equations we can derive the Sobel Operators
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
1 2 1 -1 0 1
An image is filtered using both operators and the results are added
together.
100
Sharpening Spatial Filters
2- First Derivative (Gradient)
101
Sharpening Spatial Filters
2- First Derivative (Gradient)
102
Sharpening Spatial Filters
2- First Derivative (Gradient)
103
Sharpening Spatial Filters
Comparing the 1st and 2nd derivatives we
can conclude the following:
– 1st order derivatives generally produce
thicker edges.
– 2nd order derivatives have a stronger
response to fine detail e.g. thin lines.
– 2nd order derivatives produce a double
response at step changes in grey level.
104
Thank You
105