Digital Image Processing 2 Lecture 4 PDF
Digital Image Processing 2 Lecture 4 PDF
Digital Image Processing 2 Lecture 4 PDF
lecture 4
By
Dr. Mohannad k. Sabir Al Lami
2019-2020
5- Fundamentals of Spatial Filtering
• Spatial filtering is used in a broad spectrum of
image processing applications, so a solid
understanding of filtering principles is important.
• The name filter is borrowed from frequency domain
processing where “filtering” refers to passing,
modifying, or rejecting specified frequency
components of an image.
• A filter that passes low frequencies is called a
lowpass filter. The net effect produced by a
lowpass filter is to smooth an image by blurring it.
• Spatial filtering modifies an image by replacing the
value of each pixel by a function of the values of the
pixel and its neighbors.
• If the operation performed on the image pixels is
linear, then the filter is called a linear spatial filter.
• Otherwise, the filter is a nonlinear spatial filter. We
will focus attention first on linear filters.
The Mechanics of Linear Spatial Filtering
• A linear spatial filter performs a sum-of-products
operation between an image f and a filter kernel,
w.
• The kernel is an array whose size defines the
neighborhood of operation, and whose coefficients
determine the nature of the filter.
• Other terms used to refer to a spatial filter kernel
are mask, template, and window. We use the term
filter kernel or simply kernel.
• Observe that the center coefficient of the kernel,
w(0, 0), aligns with the pixel at location (x, y). For a
kernel of size mxn we assume that m=2a+1 and
n=2b+1, where a and b are nonnegative integers.
This means that our focus is on kernels of odd size
in both coordinate directions. In general, linear
spatial filtering of an image of size MxN with a
kernel of size mxn is given by the expression
a b
g(x,y) = ∑ ∑ w(s,t) f(x+s,y+t) ------- (1)
s = -a t = -b
where x and y are varied so that the center (origin) of
the kernel visits every pixel in f once.
• For a fixed value of (x, y), Eq. 1, implements the
sum of products of the form shown below:
g(x,y)= w(-1,-1)f(x-1,y-1)+w(-1,0)f(x-1,y) + …… +
w(0,0)f(x,y) + …. + w(1,1)f(x+1,y+1)
• but for a kernel of arbitrary odd size.
• As you will learn in the following section, this
equation is a central tool in linear filtering.
5-1 Spatial Correlation and Convolution
• Spatial correlation is illustrated
graphically in Fig. and it is
described mathematically by Eq.
1.
• Correlation consists of moving
the center of a kernel over an
image, and computing the sum of
products at each location.
• The mechanics of spatial
convolution are the same, except
that the correlation kernel is
rotated by 180⁰.
• Thus, when the values of a kernel
are symmetric about its center,
correlation and convolution yield
the same result.
• The reason for
rotating the kernel
will become clear
in the following
discussion. The
best way to explain
the differences
between the two
concepts is by
example
• We begin with a 1-
D illustration, in
which case Eq. 1.
a
g(x)=∑ w(s)f(x+s) – (2)
S = -a
• The first thing we notice is that part of w lies
outside f, so the summation is undefined in that
area.
• A solution to this problem is to pad function f with
enough 0’s on either side.
• In general, if the kernel is of size 1xm, we need (m-
1)/2 zeros on either side of f in order to handle the
beginning and ending configurations of w with
respect to f.
• In this starting configuration, all coefficients of the
kernel overlap valid values.
• The first correlation value is the sum of products in this
initial position, computed using Eq. 2 with x=0:
2
g(0)=∑ w(s)f(s+0) =0
S =-2
• To obtain the second value of correlation, we shift the
relative positions of w and f one pixel location to the
right [i.e., we let x=1 in Eq. 1] and compute the sum of
products again.
• The result is g(1)=8, as shown in the leftmost, nonzero
location in Fig. above (g). When x=2 we obtain g(2)=2,
When x=3, we get g(3)=4.
• Proceeding in this manner by varying x one shift at a
time, we “build” the correlation result in Fig. above (g)
• Note that it took 8 values of x (i.e., x=0, 1,2,…,7 ) to
fully shift w past f so the center coefficient in w
visited every pixel in f.
• Sometimes, it is useful to have every element of w
visit every pixel in f. For this, we have to start with
the rightmost element of w coincident with the
origin of f, and end with the leftmost element of w
being coincident the last element of f (additional
padding would be required).
• There are two important points to note from the
preceding discussion.
1. Correlation is a function of displacement of the filter
kernel relative to the image.
• In other words, the first value of correlation
corresponds to zero displacement of the kernel, the
second corresponds to one unit displacement, and so
on.
2. That correlating a kernel w with a function that
contains all 0’s and a single 1 yields a copy of w, but
rotated by 180⁰.
• A function that contains a single 1 with the rest being
0’s is called a discrete unit impulse.
• The 1-D concepts just discussed extend
easily to images
• For a kernel of size m x n we pad the
image with a minimum of (m-1)/2 rows of
0’s at the top and bottom and (n-1)/2
columns of 0’s on the left and right. In this
case, m and n are equal to 3, so we pad f
with one row of 0’s above and below and
one column of 0’s to the left and right,
• Figure (c) shows the initial position of
the kernel for performing correlation, and
Fig. (d) shows the final result after the
center of w visits every pixel in f,
computing a sum of products at each
location. As before, the result is a copy of
the kernel, rotated by 180⁰.
• For convolution, we pre-rotate the kernel as
before and repeat the sliding sum of products
just explained. Fig. (f) through (h) show
the result. You see again that convolution of a
function with an impulse copies the function
to the location of the impulse. As noted
earlier, correlation and convolution yield the
same result if the kernel values are
• A discrete impulse of strength (amplitude) A
located at coordinates (x0,y0) is defined as:
A if x=x0 and y=y0
δ(x-x0, y- y0) = ---- (3)
0 otherwise
• Recall that A=1 for a unit impulse.
• Summarizing the preceding discussion in equation
form, the correlation of a kernel w of size m x n
with an image f(x, y), denoted as (w f)(x,y),
• is given by Eq. 1, which we repeat here for
convenience:
a b
(w f)(x,y) = ∑ ∑ w(s,t) f(x+s,y+t) ---- (4)
s = -a t = -b
box kernels 3 x3
• The simplest,
separable lowpass
filter kernel is the box
kernel, whose
coefficients have the
same value (typically
1).
• The name “box
----- (16)
• This form also reminds us that the function is
circularly symmetric.
• Variable r is the distance from the center to any
point on function G.
• Figure shows values of r for several kernel sizes
using integer values for s and t. Because we work
generally with odd kernel sizes, the centers of such
kernels fall on integer values, and it follows that all
values of r2 are integers also.
• Note in particular that the distance squared to the
corner points for a kernel of size m x m is:
------ (17)
Sampling a
Gaussian function
to obtain a discrete
Gaussian kernel.