Digital Image Processing: Image Enhancement in The Spatial Domain
Digital Image Processing: Image Enhancement in The Spatial Domain
s(x,y) = T{ r(x,y)}
Transformed Original
Gray Level Gray Level
Transformation
Function
3 Basic Gray-Level Transformation Functions :
0 15
Log Transformations
s = c log (1+r)
Where c is a constant and r ≥ 0
• Log curve maps a narrow range of low (intensity ) gray-
level values in the input image into a wider range of
output levels.
Correct threshold
64
96
128
20
Contrast Stretching :
Dark image
Components of
histogram
areconcentrated on the
low side of the gray
scale.
Bright image
Components of
histogram are
concentrated on the high
side of the gray
Low-contrast image
histogram is narrow and
centered toward the middle
of the gray scale
High-contrast image
histogram covers broad
range of the gray scale and
the distribution of pixels
uniform like, with very few
vertical lines being much
higher than the others
Dark Image and Bright Image
Low contrast Image and High contrast Image
Histogram Transformation:
s = T(r)
Where 0 ≤ r ≤ 1
T(r) satisfies:
Input image
Output image
32
Histogram equalization
original contrast streching histogram equalization
36
Digital Linear Filters
a b
g ( x, y ) w(s, t ) f ( x s, y t )
s a t b
a = (m - 1) / 2 b = (n - 1) / 2
Image Filtering
Spatial filtering
39
Linear Filtering:
• Linear Filtering of an image f of size MxN
filter mask of size mxn is given by the
expression
41
Smoothing Spatial Filters:
• Replacing the value of every pixel in an image by
the average of the gray levels in the neighborhood
will reduce the “sharp” transitions in gray levels.
• Sharp transitions
• Random noise in the image
• Edges of objects in the image
• Thus, smoothing can reduce noises (desirable)
and blur edges (undesirable)
42
Spatial Filtering:
•Use filter (can also be called as mask/ kernel/ template or
window)
• The values in a filter subimage are referred to as
coefficients, rather than pixel.
• Our focus will be on masks of odd sizes, e.g. 3x3, 5x5,…
• 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.
43
Basics of Spatial Filtering
Response, R, of an m n mask at any point (x, y)
mn
R wi zi
i 1
w1 w 2 w3
z1 z2 z3
w4 w5 w6
z4 z5 z6
w7 w8 w9
z7 z8 z9
Replace
with
R = w1z1 + w2z2 + ….. +w9z9
Smoothing Spatial Filtering - Linear
Averaging (low-pass) Filters
Smoothing filters are used
- Noise reduction
- Smoothing of false contours
- Reduction of irrelevant detail
Box Weighted
filter average
Smoothing Spatial Filtering _ Linear
Averaging (low-pass) Filters
n = filter size
n=3
n=5 n=9
n = 35
n = 15
Smoothing Spatial Filtering
Averaging & Threshold
filter size Thrsh = 25% of
n = 15 highest intensity
Nonlinear of Spatial Filtering
Example
Computation for the median is a nonlinear operation.
Smoothing Spatial Filtering
Order Statistic 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.
3 3 Median filter [10 125 125 135 141 141 144 230 240] = 141
3 3 Max filter [10 125 125 135 141 141 144 230 240] = 240
3 3 Min filter [10 125 125 135 141 141 144 230 240] = 10
52
Smoothing Linear Filters:
53
Linear Low-pass filter:
See the result after smoothing and thresholding, the remains are the largest
and brightest objects in the image.
54
Original image "airplane" (512x512x8)
55
Order-Statistics Filters (Nonlinear Filters):
56
Median Filters:
• Replaces the value of a pixel by the median of the gray levels in the
neighborhood of that pixel
• Forces the points with distinct gray levels to be more like their
neighbors.
58
Example : Median Filters
59
Nonlinear of Spatial Filtering
Example
Computation for the median is a nonlinear operation.
Smoothing Spatial Filtering
Order Statistic 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.
3 3 Median filter [10 125 125 135 141 141 144 230 240] = 141
3 3 Max filter [10 125 125 135 141 141 144 230 240] = 240
3 3 Min filter [10 125 125 135 141 141 144 230 240] = 10
63
Median Filter:
64
(a) Salt & pepper noise 5(%)
(b) Salt & pepper noise 20(%)
(c) 3x3 averaging
(d) 3x3 averaging
65
Median Filter:
3
5
66
Median Filter:
7
9
67
Sharpening Spatial Filters
68
Linear High-Pass Filter: LHPF
72
Using Second Derivatives For Image
Enhancement
•The 2nd derivative is more useful for image
enhancement than the 1st derivative
– Stronger response to fine detail
– Simpler implementation
– We will come back to the 1st order derivative later on
•The first sharpening filter we will look at is the
Laplacian
– Isotropic
– One of the simplest sharpening filters
– We will look at a digital implementation
The Laplacian
•The Laplacian is defined as follows:
f f
2 2
f 2 2
2
x y
•where the partial 1st order derivative in the x
direction is defined as follows:
f2
f ( x 1, y ) f ( x 1, y ) 2 f ( x, y )
x
2
•and in the y direction as follows:
2 f
f ( x, y 1) f ( x, y 1) 2 f ( x, y )
y
2
The Laplacian (cont…)
•So, the Laplacian can be given as follows:
f [ f ( x 1, y ) f ( x 1, y )
2
f ( x, y 1) f ( x, y 1)]
4 f ( x, y )
•We can easily build a filter based on this
0 1 0
1 -4 1
0 1 0
The Laplacian (cont…)
•Applying the Laplacian to an image we get a
new image that highlights edges and other
discontinuities
- =
Original Laplacian Sharpened
Image Filtered Image Image
(a) original
(c) df/dx
1st derivative
78
Sharpening Spatial Filters
Comparing the response between first- and second-ordered
derivatives:
1) First-order derivative produce thicker edge
2) Second-order derivative 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 {2 4 15}.
4) Second-order derivatives produce a double response at
step changes in gray level.
In general the second derivative is better than the first
derivative for image enhancement. The principle use of
first derivative is for edge extraction.
1 Derivative Filtering
st
f
Gx x
f f
G y
y
1 Derivative Filtering (cont…)
st
1
f 2 f 2 2
x y
•For practical reasons this can be simplified as:
f G x G y
Use of First Derivative for Edge Extraction
Gradient
First derivatives in image processing are implemented
using the magnitude of the gradient.
t
f f
f z1 z2 z3
x y
0.5
z4 z5 z6
f f
2 2
f mag (f ) Gx G y z7 z8 z9
x y
Roberts operator
Gx = (z9-z5) and Gy = (z8 - z6)
Sobel operator
Gx = (z3+2z6 +z9) - (z1+2z4+z7) and
Gy = (z7+2z8+z9) - (z1+2z2+z3)
Use of First Derivative for Edge Extraction
Gradient
Robert operator
Sobel operators
Sobel Operators
•Based on the previous equations we can derive
the Sobel Operators
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
1 2 1 -1 0 1
An image of a contact
lens which is
enhanced in order to
make defects (at four
and five o’clock in the
image) more obvious
…. 0 0 0 0 0 0
…. 0 200 400 400 400 400 ……
…. 0 400 600 400 400 400 …...
…. 0 400 400 0 0 0
…. 0 400 400 0 0 0
…. 0 400 400 0 0 0
…0 0 0 0 0
-1 -1 …0 100 200 200 200
1 1 …0 200 0 0 0
…0 200 0 0 0
Use of First Derivative for Edge Extraction
Gradient
2nd Derivative _ Laplacian
2 2
2 f f
f 2 2
x y
2 f
2
f ( x 1, y ) f ( x 1, y ) 2 f ( x, y )
x
2 f
2
f ( x, y 1) f ( x, y 1) 2 f ( x, y )
y
2 f [ f ( x 1, y ) f ( x 1, y ) f ( x, y 1) f ( x, y 1) 4 f ( y, y )]
Use of 2nd Derivative for Enhancement
Laplacian
2 2
f f
2 f 2 2
x y
Use of 2nd Derivative for Enhancement
Laplacian
1 1 1
1 -8 1
1 1 1
f ( x, y ) 2 f ( x, y )
g ( x, y ) 2
f ( x , y ) f ( x, y )
Use of 2nd Derivative for Enhancement
Laplacian
2 f [ f ( x 1, y ) f ( x 1, y ) f ( x, y 1) f ( x, y 1) 4 f ( x, y )]
0 1 0
g(x,y) = f(x,y) - 1 -4 1 =
0 1 0
2
f ( x, y )
Un-sharp Masking and High-boost Filtering
(b)
What is Image Restoration?
Image restoration attempts to restore images
that have been degraded
– Identify the degradation process and attempt to
reverse it
– Similar to image enhancement, but more
objective
Enhancement vs. Restoration
Enhancement Restoration
Subjective Objective
n(x,y)
g ( x, y ) h ( x , y ) * f ( x, y ) ( x, y )
In frequency domain representation:
G (u, v) H (u, v) F (u, v) N (u, v)
Where: f(x,y) is the input image, g(x,y) is the degraded
image, h(x,y) is the degradation function, and ( x, y ) is
the additive noise.
Restoration Model
Unconstrained Constrained
• Inverse Filter • Wiener Filter
• Pseudo-inverse Filter
Restoration Model
The objective of restoration is to obtain an estimate
f ( x, y ) of the original image f(x,y).
DEGRADATION RESTORATION
f ( x, y )
Degradation g ( x, y )
Degradation
Function
Function +
Restoration
Restoration
Filter(s)
fˆ ( x, y )
HH Filter(s)
( x, y )
Image Restoration
• Given g(x,y), some knowledge about the
degradation function H, and some
information about the additive noise ( x, y )
• The objective of the restoration is to obtain
an estimate fˆ ( x, y ) of the original image.
f(x,y) =input image, g(x,y)= degraded
image, h(x,y)=degradation function, and
( x, y ) =additive noise.
Noise and Images
The sources of noise in digital
images arise during image
acquisition (digitization) and
transmission
– Imaging sensors can be affected
by ambient conditions
– Interference can be added
to an image during transmission
Noise Models
Impulse noise is also called salt-and-pepper noise.
For pa=pb=0.05
128 128 128 128 128 128 128 128 128 128 128 128 255 0 128 128 128 128 128 128
128 128 128 128 128 128 128 128 128 128 128 128 128 128 0 128 128 128 128 0
128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128 128 128 128 128 0 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128 128 128 128 128 128 255 128 128 128 128 128 128
128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128 128 128 0 128 128 128 128 255 128 128 128 128
128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 255
128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 255 128 128
z
Rayleigh noise
• The PDF of a Rayleigh noise is given by
p(z)
b2 ( z a )e ( z a )
2
/b
for z a
p( z )
0 for z a
a z
a b
2
Erlang (Gamma) noise
• The PDF of a Erlang noise is given by
p(z) a (b 1) b 1 (b 1)
K e
K (b 1)!
a b z b 1 az
e for z 0
p ( z ) (b 1)!
0 for z 0
b 1 z
a
Exponential noise
• The PDF of a Exponential noise is given by
p(z)
a
ae az
for z 0
p( z )
0 for z 0
z
Note: It is a special case of Erlang PDF, with b=1.
Uniform noise
• The PDF of a Uniform noise is given by
p(z)
1
if a z b
p( z ) b a 1
0 otherwise ba
a b z
Impulse (salt-and-pepper) noise
• The PDF of a (bipolar) impulse noise is given by
p(z)
Pa for z a
p ( z ) Pb for z b Pb
0 otherwise
Pa
a b z
Exponential noise Uniform noise Salt and pepper noise
Restoration of Noise Only Degradation
g ( x, y ) f ( x , y ) ( x , y )
• The noise is unknown, so subtracting them from
g(x,y) is not a realistic option.
• In fact, enhancement and restoration become
almost indistinguishable disciplines in this
particular case.
Mean Filters
• This is the simply methods to reduce noise
in spatial domain.
– Arithmetic mean filter
– Geometric mean filter
– Harmonic mean filter
– Contraharmonic mean filter
• Let Sxy represent the set of coordinates in a
rectangular subimage window of size mxn,
centered at point (x,y).
Arithmetic mean filter
• Compute the average value of the corrupted
image g(x,y) in the aread defined by Sx,y.
• The value of the restored image fˆ at any
point (x,y)
ˆf ( x, y ) 1
g ( s, t )
mn ( s ,t )S x , y
mn
fˆ ( x, y ) g ( s, t )
( s ,t )S xy
Harmonic mean filter
• The harmonic mean filter operation is
given by the expression
mn
fˆ ( x, y )
1
( s ,t )S xy g ( s, t )
Contraharmonic mean filter
• The contraharmonic mean filter operation is
given by the expression
g ( s ,
( s ,t )S xy
t ) Q 1
fˆ ( x, y )
g ( s ,
( s ,t )S xy
t ) Q
fˆ ( x, y ) median g ( s, t )
( s ,t )S xy
Max and Min filter
• Using the 100th percentile results in the so-called
max filter, given by
fˆ ( x, y ) max g ( s, t )
( s ,t )S xy
This filter is useful for finding the brightest points in an image.
Since pepper noise has very low values, it is reduced by this
filter as a result of the max selection processing the subimage
area Sxy.
• The 0th percentile filter is min filter:
fˆ ( x, y ) min g ( s, t )
( s ,t )S xy
This filter is useful for finding the darkest points in an image.
Also, it reduces salt noise as a result of the min operation.
Midpoint filter
• The midpoint filter simply computes the
midpoint between the maximum and minimum
values in the area encompassed by the filter:
1
f ( x, y ) max g ( s, t ) min g ( s, t )
2 ( s ,t )S xy ( s ,t )S xy
Note: This filter works best for randomly distributed
noise, like Gaussian or uniform noise.