Dip2 2
Dip2 2
Image Enhancement in
Spatial Domain
Image Enhancement in
Spatial Domain
Point – Log transformation, Power law transformation,
Piecewise linear transformation, Image histogram,
histogram equalization,
Mask processing of images filtering operations- Image
smoothing, image sharpening
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, operator, filter
The value in a filter subimage are
referred as coefficients, rather than
pixels.
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
Spatial filters
Low pass filters
Low frequency details correspond to slowly varying
component of an image ,LPF allows only slowly varying
image details & attenuate heavily the details corresponding
to edges & sharp transitions & results in a Blurred image
High-pass filters
Edges & sharp details are highlighted,& attenuates
low frequency components
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
Linear spatial filtering
Pixels of image The result is the sum of
products of the mask
coefficients with the
f(x-1,y-1) f(x-1,y) f(x-1,y+1)
corresponding pixels
directly under the mask
f(x,y-1) f(x,y) f(x,y+1) Mask coefficients
w(-1,-1) w(-1,0) w(-1,1)
Where the w’s are mask coefficients, the z’s are the value of
the image gray levels corresponding to those coefficients
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
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
Type of smoothing filtering
1 1 1 1 2 1
1 1
1 1 1 2 4 2
9 16
1 1 1 1 2 1
1 1 1 1 1
1 1 1 1 1
1
1 1 1 1 1
?
25
1 1 1 1 1
1 1 1 1 1
Smoothing Linear Filters
a b
w(s, t ) f ( x s, y t )
g ( x, y ) s at b
a b
w(s, t )
s at b
Result of Smoothing Linear Filters
Original Image
Noisy image
Average filter 3x3Mask
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
Median Smoothing
Median is one value in an ordered set:
n 1
n odd
2
n n 1
average , n even
2 2
1 2 3 4 5 6 7 median = 4
2 3 4 5 6 7 median = 4.5
Order-Statistics Filters
Based on ordering (ranking) pixels
encompassed by the filter then replacing the
center pixel with the ranking result
Median filter
Provide excellent noise reduction with less blurring
Median value half of the values less than or
equal to median and half are greater than or
equal to median
Sort pixels in the neighborhood
Determine the median
Assign median to the corresponding pixel
Median Filtering
• It replaces the value of the center pixel with the median of the
intensity values in the neighborhood of that pixel.
2
• Median filtering is a nonlinear operation often used in image
processing to reduce "salt and pepper" noise. A median filter is more
effective than convolution when the goal is to simultaneously reduce
noise and preserve edges.
3
• 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.
4 • For every pixel, a 3x3 neighborhood with the pixel as center is
considered. In median filtering, the value of the pixel is replaced by
the median of the pixel values in the 3x3 neighborhood.
5
Master Layout
1 Original Image Image with ‘Salt & pepper’
noise Image after filtering
5
Salt and pepper Noise
Salt (255) Pepper(0)
Noisy Image
(salt & Pepper
Noise)
Step 1: Noise density 0.01
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then • The image with ‘salt & pepper’ noise with
when the slider points at 0.01, the noise density 0.01
second fig. should be shown
• The resulting image after median filtering is
• The text in DT should appear in parallel done
to the figures
5
Step 2: Noise density 0.02
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.02
the slider points at 0.02, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 3: Noise density 0.05
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.05
the slider points at 0.05, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 4: Noise density 0.07
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.07
the slider points at 0.07, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 5: Noise density 0.09
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.09
the slider points at 0.09, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 6: Noise density 0.1
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.1
the slider points at 0.1, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 7: Noise density 0.15
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.15
the slider points at 0.15, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 8: Noise density 0.2
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.2
the slider points at 0.2, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 9: Noise density 0.22
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.22
the slider points at 0.22, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 10: Noise density 0.3
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.3
the slider points at 0.3, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 11: Noise density 0.4
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.4
the slider points at 0.4, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 12: Noise density 0.5
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.5
the slider points at 0.5, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 13: Noise density 0.6
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.6
the slider points at 0.6, the second fig.
should be shown • The resulting image after median filtering is done
5
Step 14: Noise density 0.7
1
3
4 Instruction for the animator Text to be displayed in the working area (DT)
• The first fig. should appear and then when • The image with ‘salt & pepper’ noise with noise density 0.7
the slider points at 0.7, the second fig.
should be shown • The resulting image after median filtering is done
5
1 Questionnaire
Answers: a) 7 b) 1 c) 3 d) 9
4
5
Median Filter
1 2 2 3 5 7 1 2 2 3 5 7 1 2 2 3 5 7
6 7 8 0 6 5 6 7 8 0 6 5 6 7 8 0 6 5
27 3 7 8 2 1 27 3 7 8 2 1 27 3 7 8 2 1
1 2 2 3 5 7 1 2 2 3 5 7 1 2 2 3 5 7
6 7 8 0 6 5 6 7 8 0 6 5 6 7 8 0 6 5
3 0 7 8 255 3 3 0 7 8 255 3 3 0 7 8 255 3
4 5 255 23 56 0 4 5 255 23 56 0 4 5 255 23 56 0
27 3 7 8 2 1 27 3 7 8 2 1 27 3 7 8 2 1
1 2 2 3 5 7 1 2 2 3 5 7 Output Image
6 7 8 0 6 5 1 2 2 3 5 7
6 7 8 0 6 5
6
3 0 7 8 255 3 3 0 7 8 255 3 3 3 6 5 5
4 5 255 23 56 0 4 5 255 23 56 0 3 6 8 3
8 6
27 3 7 8 2 1 4 0
27 3 7 8 2 1 7 7 8 8
27 3 7 8 2 1
Median / Max / Min Filters
1 2 2 3 5 7
Max Filter
1 2 2 3 5 7
6 7 8 0 6 5
6 8 8 25 25 5
3 0 7 8 25 3
3 25 27 56 56 3
4 5 25 27 56 0
4 27 27 58 56 0
27 3 7 8 2 1
27 3 7 8 2 1
Min Filter
Median filter 1 2 2 3 5 7
1 2 2 3 5 7 6 0 0 0 0 5
3
6 3 3 6 5 5 0 0 0 0 3
4
3 6 8 8 6 3 0 0 2 0 0
4 27 3 7 8 2 1
7 7 8 8 0
27 3 7 8 2 1
Questionnaire
1
3
What are the values of yellow boxes after median filtering is done?
a)
4
5
Questionnaire
1 2. 2. b)
3 c) d)
4
5
Links for further reading
Reference websites:
http://en.wikipedia.org/wiki/Median_filter
http://medim.sth.kth.se/6l2872/F/F7-1.pdf
Books:
Digital Image Processing – Rafael C. Gonzalez,
Richard E. Woods, Third edition, Prentice Hall
Median Filter
Median Filter
Median Filter
Spatial filtering for image sharpening
Background: to highlight fine detail in an image or to
enhance blurred detail
Applications: electronic printing, medical imaging,
industrial inspection, autonomous target
detection (smart weapons)......
Foundation:
• Blurring/smoothing is performed by spatial
averaging (equivalent to integration)
• Sharpening is performed by noting only the gray
level changes in the image that is the differentiation
Thin lines
Isolated point
f
f ( x 1) f ( x)
x
Definition of the 2nd-order derivative
2 f
f ( x 1) f ( x 1) 2 f ( x).
x 2
Gray-level 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
7
6
5
4
3
2
1
0
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
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
f
f ( x 1) f ( x)
x
2 f
f ( x 1) f ( x 1) 2 f ( x).
x 2
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.
1st make thick edge and 2nd make thin edge
The response at and around the point is
much stronger for the 2nd- than for the
1st-order derivative
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
Discrete form of derivative
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)
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
1 -4 1
1
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
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
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
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
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 )
0 -1 0 -1 -1 -1
-1 A+4 -1 -1 A+8 -1
0 -1 0 -1 -1 -1
Directional Smoothing
Conservative smoothing
Image Quality Metrics
Image Quality Metrics
Example
Thank You
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
Gradient
The magnitude of this vector is given by
Gy 1
-1
Robert’s Method
The simplest approximations to a first-order
derivative that satisfy the conditions stated in
that section are
z4 z5 z6 f ( z 9 z 5 ) 2 ( z8 z 6 ) 2
z7 z8 z9 f z 9 z 5 z8 z 6
Robert’s Method
These mask are referred to as the
Roberts cross-gradient operators.
-1 0 0 -1
0 1 1 0
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.
Sobel’s Method
Using this equation
f ( z7 2 z8 z9 ) ( z1 2 z 2 z3 ) ( z3 2 z6 z9 ) ( z1 2 z 4 z7 )
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
1 2 1 -1 0 1