Spatial Domain Filtering - Gaziantep Üniversitesiskoc/spatial-filter.pdf · 2010-03-18 ·...
Transcript of Spatial Domain Filtering - Gaziantep Üniversitesiskoc/spatial-filter.pdf · 2010-03-18 ·...
1
Spatial Domain Filtering
Some portions of conte
2
Lecture Outline
● Basics of Spatial Filtering
● Smoothing Spatial Filters
● Averaging filters, Order-Statistics filters
● Sharpening Spatial Filters
● Laplacian filters, High-boost filters
● Gradient Masks
● Combining Spatial Enhancement Methods
4
Spatial Domain
● Spatial Domain Operations – procedures that
operate directly on pixelsg(x,y) = T[F(x,y)]
where● f(x,y) is the input image● g(x,y) is the processed image● T is an operator on f defined over some neighborhood of
(x,y)
5
Mask / Filter
● Neighborhood of a point (x,y) can be defined by using
a square/rectangular (commonly used) or circular
subimage area centered at (x,y) – mask or filter
● The center of the subimage is moved from pixel to
pixel starting at the top left corner of the image
6
Spatial Filtering
● Use filter (can also be called as
mask / kernel / template / window)
● The values of a filter subimage are
referred to as coefficients, rather
than pixel intensities.
● Our focus will be on masks of odd
sizes, e.g. 3X3, 5x5.
● Can a filter be of even size?
7
Spatial Filtering Process
● Simply move 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
∑=
=
+++=mn
iiii
mnmn
zw
zwzwzwR ...2211
(Linear Filtering)
8
Spatial Filtering Approaches
● Linear Filtering
● Non-linear Filtering
● Order-Statistics filtering
● Morphological filtering
● Adaptive Filtering
9
How Does Linear Filtering Work?
10
Linear Filtering
● Linear Filtering of an image f of size MxN by filter by mask of
size mxn is given by the expression
● To generate a complete filtered image this equation must be
applied for x = 0, 1, 2, ..., M-1 and y = 0, 1, 2, ..., N-1
● Convolution process
where a = (m-1)/2 and b = (n-1)/2
g x , y=∑s=�a
a
∑t=�b
b
w s ,t f xs , yt
11
What Happens at the Borders?
● The mask falls outside the edge!
● Solutions?
● Ignore the edges
● The resultant image is smaller than the original
● Pad with zeros
● Introducing unwanted artifacts
● What else?
12
Smoothing Spatial Filters
● Use: For blurring images & noise reduction
● Blurring is used in preprocessing steps, such as
● Removal of small details from an image prior to object
extraction
● Bridging of small gaps in lines or curves
● Noise Reduction can be accomplished by blurring
with linear or non-linear filters
13
Smoothing Linear Filters
● Output is simply the average of the pixels contained
in the neighborhood of the filter mask
● Also referred to as averaging filters or lowpass filters
(explained in future lecture)
14
Averaging Filter: An Intuitive Approach
● Replace each pixel by the average of pixels in a
square window surrounding this pixel
● 3x3 averaging filter
● What happens if we use an averaging filter with a
larger window?
15
Example: Applying 3x3 Averaging Filter
Assume edges ignored, thus resultant image is 3x3
16
More Examples
● Original image, size 500x500
pixels
● Results of smoothing with
averaging filter masks of
size n=3, 5, 9, 15, 35
respectively
(order: left to right, top to bottom)
17
Example: Space Imagery
18
Smoothing Linear Filters
● Able to reduce sharp transitions in gray levels
● Random noise in the image
● Edges of objects in the image
● Smoothing can reduce noises (desirable) and blur
edges (undesirable)
19
3x3 Smoothing Linear Filters
box filter weighted average
20
Weighted Averaging Filter
● Instead of averaging all the pixel values in the window, give
the closer-by pixels higher weighting, and far-away pixels
lower weighting
● Reduce value of coefficients as a function of increasing
distance from the origin
● An attempt to reduce blurring in the smoothing process
21
General form: Smoothing Mask
● Filter of size m x n (m and n odd)
∑∑
∑∑
−= −=
−= −=
++= a
as
b
bt
a
as
b
bt
tsw
tysxftswyxg
),(
),(),(),(
Summation of all
coefficients of the mask
22
Example: Lena 'Smoothen'
23
Other Common Smoothing Filters
● Criteria for designing a smoothing filter
● w(s, t) ≥ 0 (function as averaging)
● (mean value is preserved)
● Another popular smoothing filter: Gaussian filter
∑s=�a
a
∑t=�b
b
w s ,t =1
24
Order-Statistics Filters
● Non-linear filters
● Response is based on ordering (ranking) the pixels contained
in the image area encompassed by the filter
● Example:
● Median filter:
● Max filter:
● Min filter:
(Note: n x n is the size of the mask)
R=median zk , k=1,2,. .. , n xn
R=max zk , k=1,2,. .. , n xn
R=min zk , k=1,2,. .. , n x n
25
Median Filters
● Replaces the value of a pixel by the median of the gray levels
in the specified neighborhood of that pixel (Original value of
the pixel is included in the computation of the median)
● Popularly used for certain types of random noise (impulse
noise, salt and pepper noise)
– Excellent noise-reduction capabilities
– Less blurring effect that linear smoothing filters of similar size
● Intuitively, how do they work?
26
Example: Fighting Salt and Pepper
Noise
27
Sharpening Spatial Filters
● Sharpening: to enhance line structures or other
details in an image
● Sharpening Spatial Filters:
– To highlight fine detail or edges in an image
– To enhance and focus on details that has been blurred,
either in error or as a natural effect of a particular method
of image acquisition
Image Sharpening
• Blur the original image
• Add the Laplacian
g(x, y) = f(x, y) ± c ⋅ ∇2f(x, y)
6
28
Blurring vs. Sharpening
● Blurring can be done in spatial domain by pixel
averaging in the neighborhood of a pixel
● Since averaging is analogous to integration...
● Can we conclude that sharpening is accomplished by
spatial differentiation?
29
Derivative Operator
● The strength of the response of a derivative operator
is proportional to the degree of discontinuity of the
image at the point at which the operator is applied
● Image differentiation
– Enhances edges and other discontinuities (such as noise)
– De-emphasizes area with slowly varying gray-level values
30
First-order Derivative
● A basic definition of the first-order derivative of a
one-dimensional function f(x) is the difference
)()1( xfxfx
f −+=∂∂
31
Second-order Derivative
● Similarly, the second-order derivative of a one-
dimensional function f(x) is the difference
)(2)1()1(2
2
xfxfxfx
f −−++=∂∂
32
First & Second Derivatives of f(x,y)
● Consider an image function of two variables f(x,y), we
are dealing with partial derivatives along the two
spatial axes.
y
yxf
x
yxf
yx
yxf
∂∂+
∂∂=
∂∂∂=∇ ),(),(),(
f
2
2
2
22 ),(),(
y
yxf
x
yxff
∂∂+
∂∂=∇Laplacian operator
Gradient operator
33
Discrete form of Laplacian
From
yield,
),(2),1(),1(2
2
yxfyxfyxfx
f −−++=∂∂
),(2)1,()1,(2
2
yxfyxfyxfy
f −−++=∂∂
)],(4)1,()1,(
),1(),1([2
yxfyxfyxf
yxfyxff
−−+++−++=∇
34
Laplacian Mask
● The Laplacian mask contains the coefficients of the Laplacian
operator (second-order derivatives)
35
Laplacian Mask witih Diagonal Terms
● This Laplacian mask is implemented differently by
incorporating the diagonal directions. The center value is
now -8 due to the total subtracted from the difference terms.
36
Other Implementations of Laplacian
Masks
● These masks (difference in sign) give the same result as the
previous two masks.
● The difference in sign must be kept in mind when combining
(add / subtract) a Laplacian-filtered image with another
image
37
Effect of Laplacian Operator
● Tends to produce images that have
– Grayish edge lines and other discontinuities (in brighter intensities),
all superimposed on a dark, featureless background
● To correct the effect of featureless background
– Add the original and Laplacian-filtered image together
– Be careful with the Laplacian filter used
∇+∇−
=),(),(
),(),(),(
2
2
yxfyxf
yxfyxfyxg
if the center coefficient of the
Laplacian mask is negative
if the center coefficient of the
Laplacian mask is positive
38
Example: Moon Images
● Image of the moon
● Laplacian-filtered image with
● Laplacian image scaled for
display purposes
● Image enhanced (sharpened) by
adding Laplacian-filtered image
to the original image
(Note: left to right, top to bottom)
111
1-81
111
39
Simplifications
● To simplify the computation, we can create a mask which do
both operations, Laplacian filter and Addition of original
image
● Composite Laplacian Mask
)]1,()1,(
),1(),1([),(5
),(4)]1,()1,(
),1(),1([),(),(
−+++−++−=
+−+++−++−=
yxfyxf
yxfyxfyxf
yxfyxfyxf
yxfyxfyxfyxg
0-10
-15-1
0-10
40
Composite Laplacian Mask
41
Composite Laplacian Mask
● Which composite mask (normal Laplacian / Laplacian with
diagonal terms) can produce better image sharpening?
0-10
-19-1
0-10
0-10
-15-1
0-10
42
Composite Laplacian Mask
0-10
-19-1
0-10
0-10
-15-1
0-10
∇+∇−
=),(),(
),(),(),(
2
2
yxfyxf
yxfyxfyxg
000
010
000
0-10
-14-1
0-10
= +
000
010
000
0-10
-18-1
0-10
+=
43
Unsharp Masking
● A process used many years in the publishing industry
to sharpen images
● Sharpened image: Subtracting a blurred version of an
image from the image itself
),(),(),( yxfyxfyxf s −=sharpened image = original image – blurred image
44
Unsharp Masking
● Can you make sense of unsharp masking?
45
High-Boost Filtering
● High-boost Filtering: A further generalization of
unsharp masking
),(),(),( yxfyxAfyxfhb −=
),(),()1(
),(),(),()1(),(
yxfyxfA
yxfyxfyxfAyxf
s
hb
+−=−+−=
1≥Awhere
46
High-Boost Filtering
● This general equation does not state explicitly how the sharp
image is obtained...
● If we choose to use Laplacian operator to sharpen the image,
we get
),(),()1(),( yxfyxfAyxf shb +−=
∇+∇−
=),(),(
),(),(),(
2
2
yxfyxAf
yxfyxAfyxfhb
if the center coefficient
of the Laplacian mask is
negative
if the center coefficient
of the Laplacian mask is
positive
47
High-Boost Filtering
● If A = 1, it becomes a “standard” Laplacian sharpening
1≥A
48
Example: High-Boost Filtering
49
Example: Lena 'Sharpened'
50
First Derivatives: Gradient Operator
● First derivatives are implemented using the
magnitude of the gradient
∂∂∂∂
=
=∇
y
fx
f
G
G
y
xf
21
22
2122 ][)f(
∂∂+
∂∂=
+=∇=∇
y
f
x
f
GGmagf yx
The magnitude becomes nonlinear
yx GGf +≈∇Approximation:
51
Gradient Mask
● On the basis of a first-order derivative of a 2-D
function f(x,y), the simplest approximation of the
gradient mask: 2x2
)( and )( 5658 zzGzzG yx −=−=
212
562
582
122 ])()[(][ zzzzGGf yx −+−=+=∇
5658 zzzzf −+−≈∇z9z8z7
z6z5z4
z3z2z1
52
Gradient Mask
● Roberts 2x2 cross-gradient operators [1965]
)( and )( 6859 zzGzzG yx −=−=
212
682
592
122 ])()[(][ zzzzGGf yx −+−=+=∇
6859 zzzzf −+−≈∇ z9z8z7
z6z5z4
z3z2z1
53
Gradient Mask
● Sobel operators, 3x3
z9z8z7
z6z5z4
z3z2z1
)2()2(
)2()2(
741963
321987
zzzzzzG
zzzzzzG
y
x
++−++=++−++=
yx GGf +≈∇
The weight value 2 is to achieve
smoothing by giving more
important to the center point
54
Example: Gradient Filtering
More on gradient masks will be covered under
Edge Detection (Image Segmentation)
55
Gradient Mask
● The summation of coefficients in gradient masks
equals 0, indicating that they would give a response
of 0 in an area of constant gray level
56
Figure out!
● The summation of coefficients in a mask equals 0 for both
gradient operators (1st derivative) and Laplacian operators
(2nd derivative)
● The summation of coefficients in a smoothing mask equals 1
● Why???
57
Separable Filters
● In 2D filtering, some filters can be implemented by successive
application of two simpler filters
– One horizontal followed by another vertical
● This can significantly lower the computational complexity
● By how much? For a mask of size n x n,
– Originally, n2 multiplications and n2 – 1 additions
– After separation, 2n multiplications and 2n – 2 additions
58
Values Outside the Range?
● Linear filtering might bring the intensity outside the display
range
● Solutions?
– Clip values
– Scaling transformation
Transform values in [gL, gH] to [0, 255]
>≤≤
<=
255 xif ,255
255x0 if x,
0 x if 0,
y
LH
L
gg
gxy
−−= 255
59
Example: Combining Spatial
Enhancement Methods● Target: To sharpen the original
image and bring out more
skeletal detail
● Problems:
– Narrow dynamic range of
gray level
– High noise content makes
image difficult to enhance
60
Example: Combining Spatial
Enhancement Methods
I. Laplacian to highlight fine
details
II. Gradient to enhance prominent
edges
III.Gray level transformation to
increase the dynamic range of
gray levels
Solution:
61
Example: Combining Spatial
Enhancement Methods
62
Example: Combining Spatial
Enhancement Methods