0% found this document useful (0 votes)
114 views

MT390 (DIP) : Tutorial 3 Intensity Transformations and Spatial Filtering

This document discusses spatial filtering of images. It begins by defining spatial filtering as operations performed directly on pixel values, as opposed to frequency domain filtering. It then discusses the mechanics of spatial filtering, including how a filter mask is moved across an image to calculate the response at each point. Linear spatial filtering involves multiplying pixel values by coefficients in the filter mask and summing the results. Smoothing spatial filters are used for blurring and noise reduction by replacing pixel values with averages of neighboring pixels. Examples of smoothing filters include standard average filters and weighted average filters. Order-statistics filters like median filters provide noise reduction while preserving edges compared to linear smoothing filters.

Uploaded by

Yousef Aboamara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

MT390 (DIP) : Tutorial 3 Intensity Transformations and Spatial Filtering

This document discusses spatial filtering of images. It begins by defining spatial filtering as operations performed directly on pixel values, as opposed to frequency domain filtering. It then discusses the mechanics of spatial filtering, including how a filter mask is moved across an image to calculate the response at each point. Linear spatial filtering involves multiplying pixel values by coefficients in the filter mask and summing the results. Smoothing spatial filters are used for blurring and noise reduction by replacing pixel values with averages of neighboring pixels. Examples of smoothing filters include standard average filters and weighted average filters. Order-statistics filters like median filters provide noise reduction while preserving edges compared to linear smoothing filters.

Uploaded by

Yousef Aboamara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

MT390 (DIP): Tutorial 3

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

▪The mechanics of spatial filtering are illustrated in Fig. 3.28.


▪In general, linear filtering of an image f of size MxN with a
filter mask of size mxn is given by the expression:

▪For a mask of size mxn, we assume that m=2a+1 and n=2b+1,


where a and b are positive integers.

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

Linear Spatial Filtering Ops:


▪ The Linear Operations consist of multiplying
each pixel in the neighborhood by a
corresponding Coefficient.
▪ Results are then summed to obtain the
response at each point (x,y)

Number of Coefficients Required:


If the neighborhood is of size mxn, then mn
coefficients are required,.

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.
𝑎 𝑏

𝑤 𝑥,𝑦 .𝑓(𝑥,𝑦) = 𝑤 𝑠,𝑡 𝑓(𝑥 + 𝑠,𝑦 + 𝑡)


𝑠=−𝑎 𝑡=−𝑏

▪ Convolution: It is the same as above except that the


filter mask is rotated by 180 degrees.
𝑎 𝑏

𝑤 𝑥,𝑦 ∗𝑓(𝑥,𝑦) = 𝑤 𝑠,𝑡 𝑓(𝑥 −𝑠,𝑦 −𝑡)


𝑠=−𝑎 𝑡=−𝑏

2020-11-11 7
1-D

2020-11-11 8
Let y= [2 1 1 2 1 1 0] and h= [1 1 0];

Find x as convolution of y and h. show your intermediate steps.

Solution:

First, we need to invert h to become h1; h1= [0 1 1]

The steps for finding the convolution of y and h are as follows:

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

▪ Each coefficient value is 1/9.

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.

 Type of smoothing filters:

1.Standard average
linear
2. weighted average.

3. Median filter Order statistics

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

Standard Average Spatial Filter Mask:


• Produces Smoothing Effect
• Low Pass Filtering Effect
• The Mask is given by:

➢ A spatial averaging filter in


which all coefficients are equal
is sometimes called a box filter.

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

Group. (b) Result of lowpass filtering with a Gaussian kernel. (c)


Result of thresholding the filtered image (intensities were scaled
to the range [0, 1]). The Hickson Compact Group contains dwarf
galaxies that have come together, setting off thousands of new
star clusters. (Original image courtesy of NASA.)
Weighted Average Spatial Filter Mask:
• Produces Smoothing Effect
• Low Pass Filtering Effect
• The Mask values represent Filter Coefficients
• The Mask is given by :

2020-11-11 23
Spatial filters : Smoothing Standard and weighted Average- example

110 120 90 130


The mask is moved
91 94 98 200 from point to point in
an image. At each
90 91 99 100
point (x,y), the
82 96 85 90 response of the filter
is calculated

Standard averaging filter:


(110 +120+90+91+94+98+90+91+99)/9 =883/9 = 98.1

Weighted averaging filter:


(110 +2 x 120+90+2 x 91+4 x 94+2 x 98+90+2 x 91+99)/16=97.81

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:

Disadvantage: black border

2020-11-11 27
Second solution :border padding:

2020-11-11 28
Example:

Original image 3 x 3 averaging

5 x 5 averaging 9 x 9 averaging

15 x 15 averaging 35 x 35 averaging

2020-11-11 29
Order-Statistics (nonlinear) Filters

▪ Response is based on ordering (ranking) the pixels and


then replacing the value of the center pixel with the value
determined by the ranking result.
▪ The best-known example in this category is the median
filter, which, as its name implies, replaces the value of a
pixel by the median of the gray levels in the neighbourhood
of that pixel
▪ Median filters are quite popular because, for certain types
of random noise, they provide excellent noise-reduction
capabilities, with considerably less blurring than linear
smoothing filters of similar size.
▪ Median filters are particularly effective in the presence of
impulse noise, also called salt-and-pepper noise because of
its appearance as white and black dots superimposed on an
image.

2020-11-11 30
Spatial filters: Smoothing order statistics: Median filter

110 120 90 130

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)

 The principal objective of sharpening is to highlight transition


in intensity.
 Sharpening is accomplished by spatial differentiation.
 Image differentiation enhances edges and other
discontinuities (such as noise) and deemphasizes areas with
slowly varying gray-level values

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:

(1) First-order derivatives generally produce thicker


edges in an image.
(2) Second-order derivatives have a stronger response to
fine detail, such as thin lines and isolated points.
(3) First order derivatives generally have a stronger
response to a gray-level step.
(4) Second-order derivatives produce a double response
at step changes in gray level.

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:

153 157 156 153 155

159 156 158 156 159

155 158 154 156 160

154 157 158 160 160


154x4 – 158- 156-158-158 = -14

157 157 157 156 155

So the value after filter = -14


We call the resultant image: sharpened image.
Filtered image=original + sharpened image
(+ : because: center coefficient of Laplacian mask is positive)

The value in the filter image=154-14 =140

2020-11-11 42
Examples of Laplacian operator:

Original image Laplace Sharpened image Laplace filtered image

2020-11-11 43
Un-sharp masking and high-boost filtering

▪ To sharpen images by subtracting a blurred or un-


sharp (smoothed) version of an image from the
original image itself.
▪ This process, called un-sharp masking, consists of:
1. Blur the original image.
2. Subtract the blurred image from the original image
(the resulting difference is called the mask).
3. Add the mask to the original.

2020-11-11 44
Unsharp masking and Highboost filtering expressions:

• Let 𝑓(𝑥,𝑦) denote the blurred image.


• The mask is obtained as:
𝑔𝑚𝑎𝑠𝑘 𝑥,𝑦 = 𝑓 𝑥,𝑦 − 𝑓(𝑥,𝑦)
• A weighted portion of mask is added to original:
𝑔 𝑥,𝑦 = 𝑓 𝑥,𝑦 + 𝑘 ∗𝑔𝑚𝑎𝑠𝑘 𝑥,𝑦
• Unsharp Making: when k=1
• Highboost filtering: when 𝑘 > 1

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

▪ The magnitude (length) of above vector is:

▪ The masks are given in next slide.

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

You might also like