MT390 (DIP) : Tutorial 3 Intensity Transformations and Spatial Filtering
MT390 (DIP) : Tutorial 3 Intensity Transformations and Spatial Filtering
Chapter 3:
Intensity Transformations and
Spatial Filtering
2020-11-11 1
3.4 Fundamentals of Spatial Filtering
▪ Filtering operations that are performed directly on
the pixels of an image are called spatial filtering to
differentiate this type of process from the more
traditional frequency domain filtering.
▪ Spatial filters are also called spatial masks, kernels,
templates, and windows.
The spatial filter mask is moved from point to point in an
image. At each point (x,y), the response of the filter is
calculated
2020-11-11 2
The mechanics of spatial filtering
2020-11-11 3
The mechanics of linear spatial filtering using a 3×3 kernel. The pixels are shown
as squares to simplify the graphics. Note that the origin of the image is at the top left,
but the origin of the kernel is at its center. Placing the origin at the center of spatially
symmetric kernels simplifies writing expressions for linear filtering.
Linear Spatial Filtering
2020-11-11 5
Terminology:
The coefficients are arranged as a matrix called:
• Filter
• Mask
• Filter Mask
• Kernel
• Template
• Window
Also called:
▪ Convolution filter
▪ Convolution mask
▪ Convolution kernel
2020-11-11 6
Spatial Correlation and Convolution
▪ Correlation: It is the process of moving a filter mask
over the image and computing the sum of products at
each location.
𝑎 𝑏
2020-11-11 7
1-D
2020-11-11 8
Let y= [2 1 1 2 1 1 0] and h= [1 1 0];
Solution:
Step1: 2112110
011 →2
Step 2: 2112110
011 →3
Step 3: 2112110
011 →2
Step4: 2112110
011 →3
Step5: 2112110
011 →3
Step6: 2112110
011 →2
Step7: 2112110
011 →1
Step8: 2112110
011 →0
Step9: 2112110
011 →0
2020-11-11 9
Final answer: x = [2 3 2 3 3 2 1 0 0]
Some fundamental properties of convolution and correlation. A
dash means that the property does not hold.
Vector representation of linear filtering
▪ When interest lies in the response, R, of an mxn mask
at any point (x, y), it is common practice to simplify the
notation by using the following expression:
In Matrix form:
𝑅=𝒘𝑇𝒛
2020-11-11 11
Generating Spatial Filter Masks
▪ Generating mxn Linear Spatial Filter requires that we
specify mn mask coefficients.
▪ These coefficients are selected based on what the
filter does.
▪ E.g: 3x3 averaging filter
2020-11-11 12
3.5 Smoothing Spatial Filters
Used for:
1. Blurring: removing small details
2. Noise Reduction
2020-11-11 13
Smoothing Linear Filters (low pass)
▪ How it works? The value of every pixel is replaced by the average
of the gray levels in the neighborhood. In other words, the output
(response) of a smoothing, linear spatial filter is simply the average
of the pixels contained in the neighbourhood of the filter mask.
▪ These filters sometimes are called averaging filters. They also
are referred to a low pass filters.
1.Standard average
linear
2. weighted average.
2020-11-11 14
Low Pass Filters (LPFs):
What is a LPF?
▪ Allows Low frequencies to pass
▪ Attenuates High Frequencies (edge in images)
Advantages:
▪ Noise removal
▪ Useful for certain object detection purposes
Disadvantages:
▪ Destroys edge information in images.
2020-11-11 15
Spatial Domain Filtering/Masking Procedure:
▪ Select an appropriate Filter Mask.
▪ Imagine the Center of the Mask on top of the Pixel
which needs to be processed
▪ Perform Element by Element multiplication of
Image Pixels values and the corresponding
mask/filter coefficients.
▪ Add the results.
2020-11-11 16
Smoothing Filter: Standard Average
2020-11-11 17
Examples of smoothing kernels: (a) is a box kernel;
(b) is a Gaussian kernel.
(a) Test pattern of size 1024×1024pixels. (b)-(d) Results of lowpass
filtering with box kernels of sizes 3×3,11×11,and 21×21, respectively.
(a) Sampling a Gaussian function to obtain a discrete
Gaussian kernel. The values shown are for K=1andσ=1.
(b) Resulting 3×3kernel [this is the same as Fig. 3.3(b)].
(a) A test pattern of size1024×1024. (b) Result of lowpass filtering the
pattern with a Gaussian kernel of size 21×21. with standard deviations
σ=3.5 (c) Result of using a kernel of size 43×43, withσ=7.
This result is comparable to Fig. 3.39(d). We used K = 1 in all cases.
(a ) A 2566×2758, Hubble Telescope image of the Hickson Compact
2020-11-11 23
Spatial filters : Smoothing Standard and weighted Average- example
2020-11-11 24
General implementation:
General implementation for filtering an MxN image with
a weighted averaging filter of size mxn (m and n odd) is
given by the expression:
2020-11-11 25
What happens when the Values of the Kernel Fall Outside the Image?
2020-11-11 26
First solution :Zero padding:
2020-11-11 27
Second solution :border padding:
2020-11-11 28
Example:
5 x 5 averaging 9 x 9 averaging
15 x 15 averaging 35 x 35 averaging
2020-11-11 29
Order-Statistics (nonlinear) Filters
2020-11-11 30
Spatial filters: Smoothing order statistics: Median filter
91 94 98 200
95
90 95 99 100
82 96 85 90
Steps:
1. Sort the pixels in ascending order:
90,90, 91, 94, 95, 98, 99, 110, 120
2. replace the original pixel value by the median : 95
2020-11-11 31
(a) X-ray image of a circuit board, corrupted by salt-and-pepper
noise. (b) Noise reduction using a 19×19 Gaussian lowpass filter
kernel with σ=3. (c) Noise reduction using a 7×7 median filter.
(Original image courtesy of Mr. Joseph E. Pascente, Lixi, Inc.)
3.6 Sharpening Spatial Filters: (High Pass)
2020-11-11 33
High Pass Filters (HPFs):
What is a HPF?
▪ Allows High frequencies to pass
▪ Attenuates Low Frequencies
Advantages:
▪ Edge detection in images
Disadvantages:
▪ Noise amplification
2020-11-11 34
Examples on High pass filter
Output image
2020-11-11 35
Foundation
▪ The derivatives of a digital function are defined in
terms of differences
▪ Definitions of the first and 2nd-order derivatives of a
1-D function f(x) are the differences:
2020-11-11 36
Comparing first- and second-order derivatives:
2020-11-11 37
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:
2020-11-11 38
(a) Laplacian kernel used to implement Eq. (3-53). (b)
Kernel used to implement an extension of this
equation that includes the diagonal terms. (c) and (d)
Two other Laplacian kernels.
Using the Laplacian for image enhancement is
as follows:
2020-11-11 40
(a) Blurred image of the North Pole of the moon. (b) Laplacian image
obtained using the kernel in Fig. 3.45(a). (c) Image sharpened using Eq. (3-
63) with c = −1. (d) Image sharpened using the same procedure, but with
the kernel in Fig. 3.45(b). (Original image courtesy of NASA.)
Example: apply the following Laplace
on the highlighted pixel:
2020-11-11 42
Examples of Laplacian operator:
2020-11-11 43
Un-sharp masking and high-boost filtering
2020-11-11 44
Unsharp masking and Highboost filtering expressions:
2020-11-11 45
2020-11-11 46
(a) Unretouched “soft-tone” digital image of size 469×600pixels
(b) Image blurred using a 31×31 Gaussian lowpass filter with σ = 5.
(c) Mask. (d) Result of unsharp masking using Eq. (3-56) with k = 1. (e) and
(f) Results of highboost filtering with k = 2 and k = 3, respectively.
Using First Order Derivatives for (Nonlinear) Image
Sharpening—The Gradient
▪ For a function f(x, y), the gradient of f at coordinates (x, y) is
defined as the two-dimensional column vector
2020-11-11 48
(a ) A 3×3 region of an image, where the z s are intensity
values. (b)–(c) Roberts cross-gradient operators. (d)–
(e) Sobel operators. All the kernel coefficients sum to
zero, as expected of a derivative operator.
(a) Image of a contact lens (note defects on the
boundary at 4 and 5 o’clock). (b) Sobel gradient.
(Original image courtesy of Perceptics Corporation.)
3.8 Combining Spatial Enhancement Methods
▪ Fig. 3.43(a) is a nuclear whole body bone scan image
▪ Our objective is to enhance this image by sharpening it
and by bringing out more of the skeletal detail.
▪ The narrow dynamic range of the gray levels and high
noise content make this image difficult to enhance.
▪ The strategy we will follow is:
➢ to utilize the Laplacian to highlight fine detail, and
➢the gradient to enhance prominent edges.
➢a smoothed version of the gradient image will be used to
mask the Laplacian image
➢Finally, we will attempt to increase the dynamic range of the
gray levels by using a gray-level transformation.
2020-11-11 51
Prepared by: Dr. Farid Jradi 2020-11-11 52
Prepared by: Dr. Farid Jradi 2020-11-11 53
Lab Hints:
Matlab help on following image related
functions:
Histeq
adapthisteq
imadjust
imnoise
medfilt2
fspecial,
filter2,
Imfilter etc
2020-11-11 54