CV Lecture 5
CV Lecture 5
CSC-455
Muhammad Najam Dar
Today’s Lecture
Spatial Filtering
i. Basics of Spatial Filtering.
ii. Types of Spatial Filtering: Smoothing and Sharpening.
iii. Mask Operation near border: Pixel Replication and Zero
Padding.
iv. Types of Smoothing Filters: Linear Filters, Non-Linear (Order
Statistic) Filters
v. Linear Filters: Mean (Box, Average) Filtering
vi. Non-Linear (Order Statistic) Filtering: Median Filtering,
Min/Max Filtering.
vii. Sharpening Filters: Laplacian Filters, Sobel Filters.
What types of image filtering can we do?
Point Operation
point processing
Neighborhood Operation
“filtering”
Examples of point processing
Implement these in MATLAB?
original darken lower contrast non-linear lower contrast
• The same kernel is shifted to all pixel locations so that all pixels
use the same linear combination of their neighbors.
Spatial Filtering
Spatial Filtering
52
Spatial Filtering
Spatial Filtering
Spatial Filtering
Examples:
(a) Image of cell corrupted
by noise
(b) The result of averaging
(c) Image of Martian surface
corrupted by interference in
transmission
(d) The result of computer
processing
Spatial Filtering: Basics
The output intensity value at (x,y) depends not
only on the input intensity value at (x,y) but
also on the specified number of neighboring
intensity values around (x,y)
Moving window
(kernel) scans the 3x3
neighborhood of every
pixel in the image
Original Image
Spatial Filtering: Basics
Spatial Filtering: Basics
Spatial Filtering: Basics
Spatial Filtering: Basics
Smoothing Spatial Filters
1/ 1/ 1/
9 9 9
Simple
1/ 1/ 1/
9 9 9 averaging
1/ 1/ 1/
filter
9 9 9
Applications of Smoothing Spatial Filters
Smooth by
Smooth by
35x35 box filter
15x15 box filter
Consider the
output pixel is
positioned at
the center
95 90 85 * 1/
9
1/
9
1/
9
1/ 1/ 1/
9 9 9
104 100 108 Original Image Filter
1/ 9 1/9 1/9
Simple 3*3 99 106 98 3*3 Smoothing Pixels
1/ 1/ 1/
Neighbourhood 959 9
90 9
85 Filter
1/ 9 1/9 1/9
e = 1/9*106 +
1/ *104 +
9
1/ *100 +
9
y Image f (x, y) 1/ *108 +
9
1 /= 98.3333
9*99 + /9*98
1
The above is repeated for every pixel in the original image to generate the smoothed image
+
Example: the box filter
1 1 1
kernel 1 1 1
1 1 1
• replaces pixel with local average
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
shift-invariant:
1 1 1 0 0 0 90 90 90 90 90 0 0 as the pixel
0 0 0 0 0 0 0 0 0 0 shifts, so does
0 0 0 0 0 0 0 0 0 0 the kernel
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0 0 20
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0 0 20 40
1 1 1 0 0 0 90 90 90 90 90 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0 0 20 40 60 60 60 40 20
1 1 1 0 0 0 90 90 90 90 90 0 0 0
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0 0 20 40 60 60 60 40 20
1 1 1 0 0 0 90 90 90 90 90 0 0 0 30
1 1 1 0 0 0 90 0 90 90 90 0 0
1 1 1 0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0 0 20 40 60 60 60 40 20
1 1 1 0 0 0 90 90 90 90 90 0 0 0 30 50 80 80 90 60 30
1 1 1 0 0 0 90 0 90 90 90 0 0 0 30 50 80 80 90 60 30
1 1 1 0 0 0 90 90 90 90 90 0 0 0 20 30 50 50 60 40 20
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 20 10
0 0 0 0 0 0 0 0 0 0 10 10 10 10 0 0 0 0
0 0 90 0 0 0 0 0 0 0 10
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0 0 20 40 60 60 60 40 20
1 1 1 0 0 0 90 90 90 90 90 0 0 0 30 50 80 80 90 60 30
1 1 1 0 0 0 90 0 90 90 90 0 0 0 30 50 80 80 90 60 30
1 1 1 0 0 0 90 90 90 90 90 0 0 0 20 30 50 50 60 40 20
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 20 10
0 0 0 0 0 0 0 0 0 0 10 10 10 10 0 0 0 0
0 0 90 0 0 0 0 0 0 0 10 10 10 10 0 0 0 0
0 0 0 0 0 0 0 0 0 0
kernel 0 0 0 90 90 90 90 90 0 0 0 20 40 60 60 60 40 20
1 1 1 0 0 0 90 90 90 90 90 0 0 0 30 50 80 80 90 60 30
1 1 1 0 0 0 90 0 90 90 90 0 0 0 30 50 80 80 90 60 30
1 1 1 0 0 0 90 90 90 90 90 0 0 0 20 30 50 50 60 40 20
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 20 10
0 0 0 0 0 0 0 0 0 0 10 10 10 10 0 0 0 0
0 0 90 0 0 0 0 0 0 0 10 10 10 10 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Sorted: 0,0,1,1,1,2,2,2,4
Input Output
1 2 0 1
3 1
2 2 4 2
2
1 0 1 0
1 2 1
2 5 1 0
2
Median Filtering
Median = ? 20
Particularly effective when
The noise pattern consists of
strong
impulse noise ( salt-and-pepper)
Salt and Pepper
Noise
Median Filtering
Min/Max Filtering
Sharpening Filters
Sharpening Spatial
Filters
Previously we have looked at smoothing filters which remove
fine detail
edge
edge
How would you detect an edge?
What kinds of filter would you
use?
Edge detection
• Goal: map image from 2d array of pixels to a set of
curves or line segments or contours.
• Why?
Depth discontinuity:
Reflectance change: object boundary
appearance
information,
texture
Cast shadows
Change in surface
orientation: shape
Contrast and invariance
Derivatives and edges
An edge is a place of rapid change in the image
intensity function.
intensity function
image (along horizontal scanline)
Source: L. Lazebnik
Edge Detection
Partial derivatives of an image
f (x, f (x, y)
y) x y
-1 1 -1 ?or 1
1 -1
Which shows changes with respect to x?
(showing flipped filters)
Difference filters
>> My = fspecial(‘sobel’);
>> outim = imfilter(double(im),
My);
>> imagesc(outim);
>> colormap gray;
Spatial
Differentiation
• Let’s consider a simple 1
dimensional example
Spatial
Differentiation
A B
1st
Derivative
The 1st derivative of a function is given by:
f
x f (x 1)
f (x)
Its just the difference between subsequent
values and measures the rate of change of
the function
1st Derivative
2nd Derivative
Simply takes into account the values both before and after the current value
2 f
f (x 1) f (x 1) 2 f
x
2
(x)
2nd Derivative
Edge Detection
• First and second
derivative
for smooth noiseless edge
• The zero crossings of the
second derivative can be
used for locating edges in
an image.
Zero Crossings
zero crossing
f f’ f
’’
34
Examples
35
Edge Detection
• First order derivatives produce thick edges while second
order
derivatives produce finer ones.
• Second order derivatives enhance sharp changes and fine
details more aggressively than first order derivatives see
the isolated point and the line in the same figure. This
can be a problem if the noise is present in the image
• Second derivative changes its sign as it transitions into
and out of a ramp or step edge. See the step edge. This
“double edge” effect can be used to locate edges.
• The sign of the second derivative is also used to
determine whether an edge is a transition from light to dark
(-ve value) or from dark to light (+ve value). See step edge
Computing 2nd Order Derivative
using Laplacian Filter
2nd Derivative for Image Enhancement
0 1 0
Laplacian Filter
Laplacian Filter
Laplacian
Filtered Image
Scaled for
f (x, y) 2 f , w
5
Display
g(x, y)
f0(x, y) 2 f , w
5
0
Laplacian Image Enhancement
- =
Original Laplacian Sharpened
Image Filtered Image Image
-1 5 -1
0 -1 0
Unsharp Masking
Computing 1st Order Derivative
using Sobel Filter
Gradient Operators
Some common gradient operators
Roberts and Prewitt masks are the
simplest but not robust against noise
Sobel edge detection masks are the
most common and give satisfactory
results in presence of noise
Gradient
Operators
The ‘Sobel’
filter 1 0 -1
2 0 -2
1 0 -1
a derivative filter
(with some smoothing)
a derivative filter
(with some smoothing)
a derivative filter
(with some smoothing)
What
1 0 -1 this?
1 0 -1
1
2 0 -2
1 0 -1
= 2 x-
derivative
1
Sobel
weighted
average and
scaling
Gradient
Operators
Sobel Operator
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
1 2 1 -1 0 1
Extract horizontal Extract vertical
edges edges
Emphasize more the current point
(y direction)
Laplace Sobel