0% found this document useful (0 votes)
221 views65 pages

Lecture No 7 Spatial Filtering

This document discusses spatial filtering techniques in digital image processing. It covers topics such as: - Linear spatial filtering which involves convolving an image with a filter mask through a weighted sum operation. - Smoothing filters which are used to blur images and reduce noise through averaging or order-statistic filters like median filters. - Sharpening filters which aim to enhance details and fine structures in images by using differentiation-like filters. Various filter masks for smoothing and their effects are demonstrated. The mechanics and implementations of different filtering methods are explained.

Uploaded by

Abdul Rehman
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)
221 views65 pages

Lecture No 7 Spatial Filtering

This document discusses spatial filtering techniques in digital image processing. It covers topics such as: - Linear spatial filtering which involves convolving an image with a filter mask through a weighted sum operation. - Smoothing filters which are used to blur images and reduce noise through averaging or order-statistic filters like median filters. - Sharpening filters which aim to enhance details and fine structures in images by using differentiation-like filters. Various filter masks for smoothing and their effects are demonstrated. The mechanics and implementations of different filtering methods are explained.

Uploaded by

Abdul Rehman
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/ 65

CS-407:

Digital Image Processing

U M E R J AV E D
( U M E R . J AV E D @ I I U . E D U . P K )

D E PA R T M E N T O F E L E C T R O N I C
ENGINEERING,
FA C U LT Y O F E N G I N E E R I N G & T E C H N O L O G Y,
IIUI

SPRING 2017
CS-407:
Digital Image Processing

U M E R J AV E D
( U M E R . J AV E D @ I I U . E D U . P K )

CHAPTER: 3
I N T E N S I T Y T R A N S F O R M AT I O N A N D S PAT I A L
F I LT E R I N G

S L I D E # 3 , PA R T B
S PAT I A L F I LT E R I N G
Background

 Filter term in “Digital image processing” is referred


to the subimage
 There are others term to call subimage such as mask,
kernel, template, or window
 The value in a filter subimage are referred as
coefficients, rather than pixels.

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 3


Basics of Spatial Filtering

 The concept of filtering has its roots in the use of the


Fourier transform for signal processing in the so-
called frequency domain.
 Spatial filtering term is the filtering operations that
are performed directly on the pixels of an image

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 4


Mechanics of spatial filtering

 The process consists simply of moving the filter mask


from point to point in an image.
 At each point (x,y) the response of the filter at that
point is calculated using a predefined relationship

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 5


Linear spatial filtering
The result is the sum of
Pixels of image products of the mask
coefficients with the
corresponding pixels
w(-1,-1) w(-1,0) w(-1,1) directly under the mask
f(x-1,y-1) f(x-1,y) f(x-1,y+1)
Mask coefficients
w(0,-1) w(0,0) w(0,1) w(-1,-1) w(-1,0) w(-1,1)
f(x,y-1) f(x,y) f(x,y+1)
w(1,-1) w(1,0) w(1,1) w(0,-1) w(0,0) w(0,1)
f(x+1,y-1) f(x+1,y) f(x+1,y+1)

w(1,-1) w(1,0) w(1,1)

f ( x, y)  w(1,1) f ( x  1, y  1)  w(1,0) f ( x  1, y )  w(1,1) f ( x  1, y  1) 


w(0,1) f ( x, y  1)  w(0,0) f ( x, y )  w(0,1) f ( x, y  1) 
w(1,1) f ( x  1, y  1)  w(1,0) f ( x  1, y )  w(1,1) f ( x  1, y  1)
Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 6
Note: Linear filtering

 The coefficient w(0,0) coincides with image value


f(x,y), indicating that the mask is centered at (x,y)
when the computation of sum of products takes
place.
 For a mask of size mxn, we assume that m-2a+1
and n=2b+1, where a and b are nonnegative
integer. Then m and n are odd.

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 7


Linear filtering

 In general, linear filtering of an image f of size MxN


with a filter mask of size mxn is given by the
expression:

a b
g ( x, y )    w(s, t ) f ( x  s, y  t )
s   at   b

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 8


Discussion

 The process of linear filtering similar to a frequency


domain concept called “convolution”

Simplify expression mn
R  w1 z1  w2 z2  ...  wmn zmn   wi zi w1 w2 w3
i 1
9 w4 w5 w6
R  w1 z1  w2 z2  ...  w9 z9   wi zi w7 w8 w9
i 1
Where the w’s are mask coefficients, the z’s are the value of the image gray
levels corresponding to those coefficients

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 9


Nonlinear spatial filtering

 Nonlinear spatial filters also operate on


neighborhoods, and the mechanics of sliding a mask
past an image are the same as was just outlined.

 The filtering operation is based conditionally on the


values of the pixels in the neighborhood under
consideration

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 10


Smoothing Spatial Filters

 Smoothing filters are used for blurring and for


noise reduction.
– Blurring is used in preprocessing steps, such as removal of
small details from an image prior to object extraction, and
bridging of small gaps in lines or curves
– Noise reduction can be accomplished by blurring

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 11


Type of smoothing filtering

 There are 2 way of smoothing spatial filters


 Smoothing Linear Filters

 Order-Statistics Filters

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 12


Smoothing Linear Filters

 Linear spatial filter is simply the average of the pixels


contained in the neighborhood of the filter mask.
 Sometimes called “averaging filters”.
 The idea is replacing the value of every pixel in an
image by the average of the gray levels in the
neighborhood defined by the filter mask.

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 13


Two 3x3 Smoothing Linear Filters

1 1 1 1 2 1
1 1
 1 1 1  2 4 2
9 16
1 1 1 1 2 1

Standard average Weighted average

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 14


5x5 Smoothing Linear Filters

1 1 1 1 1

1 1 1 1 1
1
x 1 1 1 1 1
25
1 1 1 1 1

1 1 1 1 1

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 15


Smoothing Linear Filters

 The general implementation for filtering an MxN


image with a weighted averaging filter of size mxn is
given by the expression

a b

  w(s, t ) f ( x  s, y  t )
g ( x, y )  s   at   b
a b

  w(s, t )
s   at   b

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 16


Result of Smoothing Linear Filters

Original Image

[3x3] [5x5] [7x7]

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 17


Order-Statistics Filters

 Order-statistics filters are nonlinear spatial filters


whose response is based on ordering (ranking) the
pixels contained in the image area encompassed by
the filter, and then replacing the value of the center
pixel with the value determined by the ranking
result.
 Best-known “median filter”

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 18


Process of Median filter

 Corp region of
neighborhood
10 15 20
 Sort the values of
20 100 20 the pixel in our
region
20 20 25
 In the MxN mask
the median is MxN
10, 15, 20, 20, 20, 20, 20, 25, 100 div 2 +1
5th
Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 19
Result of median filter

Noise from Glass effect Remove noise by median filte


Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 20
Sharpening Spatial Filters

 The principal objective of sharpening is to highlight


fine detail in an image or to enhance detail that has
been blurred, either in error or as an natural effect of
a particular method of image acquisition.

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 21


Introduction

 The image blurring is accomplished in the spatial


domain by pixel averaging in a neighborhood.
 Since averaging is analogous to integration.
 Sharpening could be accomplished by spatial
differentiation.

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 22


Foundation

 We are interested in the behavior of these derivatives


in areas of constant gray level(flat segments), at the
onset and end of discontinuities(step and ramp
discontinuities), and along gray-level ramps.
 These types of discontinuities can be noise points,
lines, and edges.

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 23


Definition for a first derivative

 Must be zero in flat segments


 Must be nonzero at the onset of a gray-level step or
ramp; and
 Must be nonzero along ramps.

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 24


Definition for a second derivative

 Must be zero in flat areas;


 Must be nonzero at the onset and end of a gray-level
step or ramp;
 Must be zero along ramps of constant slope

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 25


Definition of the 1st-order derivative

 A basic definition of the first-order derivative


of a one-dimensional function f(x) is

f
 f ( x  1)  f ( x)
x

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 26


Definition of the 2nd-order derivative

 We define a second-order derivative as the


difference

2 f
 f ( x  1)  f ( x  1)  2 f ( x).
x 2

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 27


Gray-level profile

000123200226332233000000776553
7
6
5
4
3
2
1
0

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 28


Derivative of image profile

0 0 0 1 2 3 2 0 0 2 2 6 3 3 2 2 3 3 0 0 0 0 0 0 7 7 6 5 5 3

first 0 0 1 1 1-1-2 0 2 0 4-3 0-1 0 1 0-3 0 0 0 0 0-7 0-1-1 0-2

second 0-1 0 0-2-1 2 2-2 4-7 3-1 1 1-1-3 3 0 0 0 0-7 7-1 0 1-2

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 29


Analyze

 The 1st-order derivative is nonzero along the entire


ramp, while the 2nd-order derivative is nonzero only
at the onset and end of the ramp.

 The response at and around the point is much


stronger for the 2nd- than for the 1st-order derivative
1st make thick edge and 2nd make thin edge

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 30


The Laplacian (2nd order derivative)

 Shown by Rosenfeld and Kak[1982] that the


simplest isotropic derivative operator is the
Laplacian is defined as

 2
f  2
f
 f  2  2
2

x y

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 31


Discrete form of derivative

f(x-1,y) f(x,y) f(x+1,y) 2 f


 f ( x  1, y)  f ( x  1, y)  2 f ( x, y)
x 2

f(x,y-1)

f(x,y) 2 f
 f ( x, y  1)  f ( x, y  1)  2 f ( x, y )
y 2

f(x,y+1)

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 32


2-Dimentional Laplacian
 The digital implementation of the 2-Dimensional
Laplacian is obtained by summing 2 components

 2
f  2
f
2 f  2  2
x x
2 f  f ( x  1, y)  f ( x  1, y)  f ( x, y  1)  f ( x, y  1)  4 f ( x, y)

1 -4 1

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 33


Laplacian

0 1 0 1 1 1

1 -4 1 1 -8 1

0 1 0 1 1 1

1 0 1

0 -4 0

1 0 1

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 34


Laplacian

0 -1 0 -1 -1 -1

-1 4 -1 -1 8 -1

0 -1 0 -1 -1 -1

-1 0 -1

0 4 0

-1 0 -1

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 35


Implementation

 f ( x, y )   2 f ( x, y ) If the center coefficient is negative


g ( x, y )  
 f ( x , y )   2
f ( x, y ) If the center coefficient is positive

Where f(x,y) is the original image


 2 f ( x,isy)Laplacian filtered image
g(x,y) is the sharpen image

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 36


Implementation

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 37


Implementation

Filtered = Conv(image,mask)

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 38


Implementation

filtered = filtered - Min(filtered)


filtered = filtered * (255.0/Max(filtered))

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 39


Implementation

sharpened = image + filtered


sharpened = sharpened - Min(sharpened )
sharpened = sharpened * (255.0/Max(sharpened ))

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 40


Algorithm

 Using Laplacian filter to original image

 And then add the image result from step 1 and the
original image

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 41


Simplification
 We will apply two step to be one mask

g ( x, y)  f ( x, y)  f ( x  1, y)  f ( x 1, y)  f ( x, y  1)  f ( x, y 1)  4 f ( x, y)

g ( x, y)  5 f ( x, y)  f ( x  1, y)  f ( x 1, y)  f ( x, y  1)  f ( x, y 1)

0 -1 0 -1 -1 -1

-1 5 -1 -1 9 -1

0 -1 0 -1 -1 -1

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 42


Unsharp masking
 A process to sharpen images consists of
subtracting a blurred version of an image from
the image itself.
 This process, called unsharp masking, is
expressed as
f s ( x, y)  f ( x, y)  f ( x, y)

Where f s ( x, y) denotes the sharpened image obtained by unsharp


masking, and f ( x, y) is a blurred version of f ( x, y)

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 43


High-boost filtering

 A high-boost filtered image, fhb is defined at any


point (x,y) as

f hb ( x, y)  Af ( x, y)  f ( x, y) where A  1

f hb ( x, y)  ( A  1) f ( x, y)  f ( x, y)  f ( x, y)

f hb ( x, y)  ( A  1) f ( x, y)  f s ( x, y)

This equation is applicable general and does not state explicity


how the sharp image is obtained

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 44


High-boost filtering and Laplacian
 If we choose to use the Laplacian, then we know
fs(x,y)
 Af ( x, y )   2 f ( x, y ) If the center coefficient is negative
f hb  
 Af ( x, y )   2
f ( x, y ) If the center coefficient is positive

0 -1 0 -1 -1 -1

-1 A+4 -1 -1 A+8 -1

0 -1 0 -1 -1 -1

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 45


High-boost filtering and Laplacian

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 46


The Gradient (1st order derivative)

 First Derivatives in image processing are


implemented using the magnitude of the
gradient.
 The gradient of function f(x,y) is

 f 
Gx   x 
f      f 
G y   
 y 

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 47


Gradient
 The magnitude of this vector is given by

mag(f )  Gx2  Gy2  Gx  Gy

Gx This mask is simple, and no isotropic. Its


-1 1
result only horizontal and vertical.

Gy 1

-1

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 48


Robert’s Method
 The simplest approximations to a first-order
derivative that satisfy the conditions stated in
that section are

z1 z2 z3 Gx = (z9-z5) and Gy = (z8-z6)

z4 z5 z6 f  ( z9  z5 ) 2  ( z8  z6 ) 2

z7 z8 z9
f  z9  z5  z8  z6

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 49


Robert’s Method

 These mask are referred to as the Roberts cross-


gradient operators.

-1 0 0 -1

0 1 1 0

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 50


Sobel’s Method

 Mask of even size are awkward to apply.


 The smallest filter mask should be 3x3.
 The difference between the third and first rows of
the 3x3 mage region approximate derivative in x-
direction, and the difference between the third
and first column approximate derivative in y-
direction.

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 51


Sobel’s Method

 Using this equation

f  ( z7  2 z8  z9 )  ( z1  2 z2  z3 )  ( z3  2 z6  z9 )  ( z1  2 z4  z7 )

-1 -2 -1 -1 0 1

0 0 0 -2 0 2

1 2 1 -1 0 1

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 52


Spatial filtering

 Neighborhood

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 53


Pixel relationships

 Filter
 Mask, kernel, window
 Size
 Symmetry

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 54


Spatial Filters

 Correlation

 Convolution

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 55


Spatial Filters

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 56


Spatial Filters

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 57


Spatial Filters

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 58


Spatial Filters

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 59


Spatial Filters

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 60


Spatial Filters

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 61


Pixel relationships

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 62


Pixel relationships

 Neighborhood

 Adjacency

 Connectivity

 Paths

 Regions and boundaries

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 63


Pixel relationships

 Neighborhood

 Adjacency

 Connectivity

 Paths

 Regions and boundaries

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 64


Pixel relationships

 Neighborhood

 Adjacency

 Connectivity

 Paths

 Regions and boundaries

Umer Javed, CS:407 Digital Image Processing Chapter:3, Slide # 65

You might also like