Image Enhancement in
the Spatial Domain
Spatial Filtering
Some figures are provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Image Operations
• Arithmetic Operations on pixels (p and q are images)
– p+q
– p-q
– p*q (also stated as pq, or p x q)
– p%q
• Logic Operations on pixels (p and q are binary images)
– p AND q
– p OR q
– Not q
– p XOR q
Image Operations
• Subtraction or Absolute Difference
– Very useful for determining the difference
between two images
– Used for “detection” Absolute Difference
Grey = non-zero, Background = 0.
Example
Digital subtraction angiogram
(DSA)
X-ray brain image X-ray brain image
without contrast agent with contrast agent injected
injected
http://en.wikipedia.org/wiki/Digital_subtraction_angiography
Image Operation
• Addition can blend two images
Logic Operators
• NOT
black = 1, white = 0.
Logic Operators
• AND
black = 1, white = 0.
Logic Operators
• OR
black = 1, white = 0.
• XOR?
Image Averaging
• If you can capture a scene or acquire an image repeatedly,
then averaging all the observed images will give you a
clear image with less noise.
• gi(x,y) = ith observed noisy image, i = 1, ..., K, and K = total
number of images.
• f(x,y) = ‘true’, original image
• η(x,y) = (eta) is zero mean, random Gaussian noise
• the final average image is given by
1 K
g ( x, y ) =
K
∑ g ( x, y )
i =1
i
Image Averaging
• It follows that
Expected value of the
average image
E { g ( x, y )} = f ( x, y )
Variance of the 1 2
σ 2
g ( x, y ) = σ η ( x, y )
average image K
• If K is sufficiently large, then the average image will be
very close to the true, original image, and the variance
(noise) will be small.
Example
K=8 K=16
K=64 K=128
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Spatial Filtering using a Mask/Filter
• Neighborhood operators
mask/filter
z1 z2 z3 w1 w2 w3
z4 z5 z6 w4 w5 w6
z7 z8 z9 w7 w8 w9
Response of a linear mask/filter, R, 9
R = ( w1z1 + w2z2 + w3z3 ... + w9z9) = ∑ wizi
i=1
f(x,y) is centered around “z5” so g(x,y) = R =filter*f(x,y), *=convolution symbol
Response of a linear mask/filter
R = w1z1 + w2z2 + w3z3 ... + w9z9
where wi are mask coefficients (weights) and
zi are pixel intensities.
Response of a linear mask/filter
𝑔𝑔 𝑥𝑥, 𝑦𝑦
= 𝑤𝑤 −1, −1 𝑓𝑓 𝑥𝑥 − 1, 𝑦𝑦 − 1 + ⋯
+ 𝑤𝑤 1,1 𝑓𝑓(𝑥𝑥 + 1, 𝑦𝑦 + 1)
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Response of a linear mask/filter
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Smoothing Filters
• Smoothing Filters
– blurring
• “pre-processing”
– removal of small details before object extraction
– noise reduction
• removal of noise in an image
• Often called “Low-Pass” Filters
– Filter lets low-frequencies pass
– Stops high-frequencies
Low-pass spatial filtering
All values are positive
0
0
Filter center
Frequency Domain Spatial Domain
Low-pass spatial filtering
• Only requirement for a low-pass
filter is that wi be positive
w1 w2 w3
• Note that the result can be larger
w4 w5 w6 than the valid output range(L-1)
w7 w8 w9
• Can pre-scale the filter
scale_factor = (Σ wi )-1
Low-pass spatial filter
1 1 1
1/9 * 1 1 1
1 1 1
Average Filter
(Box Filter)
Extends to larger filters
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1
1 1 1 1 1 1 1 1 1
1/9 * 1 1 1 1/81 *
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
Example
original n=5 (nxn mask)
n=15 (nxn mask) n=25 (nxn mask)
Example
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Example
Smooth image
Original image 15x15 averaging filter thresholded image
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Example
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Other arrangements
1 1 1 1 1
1 1 1
1 2 3 2 1
1 3 4 3 1
1 2 1
1 2 3 2 1
1 1 1
1 1 1 1 1
Remember that the resulting gray-levels
may be out of the range of the original image.
Gaussian Filter
𝑠𝑠 2 +𝑡𝑡 2
−
𝑤𝑤 𝑠𝑠, 𝑡𝑡 = 𝐾𝐾𝑒𝑒 2𝜎𝜎2
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Gaussian Filter
𝑠𝑠 2 +𝑡𝑡 2
−
𝑤𝑤 𝑠𝑠, 𝑡𝑡 = 𝐾𝐾𝑒𝑒 2𝜎𝜎2
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Gaussian Filter
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Linear vs. Non-linear
• Linear Filters
– Linear operation (output can be expressed as the
convolution of the input image with filter)
– Have corresponding frequency domain filter
• Non-linear Filters
– Examine neighbors using various orderings
• Often use Order Statistics
Median Filter
• Very popular non-linear filter
• Find the median of the window
• Preserves edges
• Removes impulse noise, avoids excessive smoothing
2 3 8
neighbor sort = {2,2,3,3,4,4,8,9,10}
3 4 10
4 2 9
f(x,y) = median
pixel values about (x,y)
window 3x3
Examples
Original Noisy Image
Low-Pass Filter Median Filter
Examples
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Median Filter
• Often referred to as “de-speckle operation”
• It converges
– That is, if you perform it over and over for many
times, eventually the image will not change.
Iterative Smoothing
• Local Averaging
– Converges to an image with constant intensity
• Median Filter
– Converges to an image invariant to the filter
Sharpening
• Objectives of sharpening
– Highlight fine details in an image
– Enhance details that have been blurred
– We can think of this as high-pass filtering
• Letting high frequencies pass
• Removing low frequencies
First Order Derivatives
• Gradient
• Function of 2 variables x, y
∂f y
∂x
∇f =
∂f
∂y
x
First Order Derivatives
• For each (x,y) you are storing two values:
∂f
∂x
∇f =
∂f
∂y
• Often have two images to represent this
• X-Gradient and Y-Gradient
– Can be computed independently
First Order Derivatives
• X-Gradient and Y-Gradient y
x
Original |∂f/∂x|
| ∂f/∂y| mag(∇f)
Gradient
• Gradient Magnitude
2 1/ 2
∂f ∂f
2
mag(=
∇f) +
∂x ∂y
Basic Derivative
• Consider the pixels z1 z2 z3
z4 z5 z6
z7 z8 z9
• mag(∇f) at z5 can be computed
mag(∇f ) ≈ [( z5 − z6 ) 2 + ( z5 − z8 ) 2 ]1/ 2
Basic Derivative
• Consider the pixels z1 z2 z3
z4 z5 z6
z7 z8 z9
• mag(∇f) at z5 can be computed quicker
mag(∇f ) ≈| z5 − z6 | + | z5 − z8 |
Basic Derivative
z1 z2 z3
z4 z5 z6
z7 z8 z9
• mag(∇f) sometimes is computed using the
“cross” difference
mag(∇f ) ≈| z6 − z8 | + | z5 − z9 |
First Order Derivatives
mag(∇f ) ≈| ( z7 + z8 + z9 ) − ( z1 + z2 + z3 ) | +
| ( z3 + z6 + z9 ) − ( z1 + z4 + z7 ) |
• Difference between first and third rows (∂f/∂x)
• Difference between first and third columns (∂f/∂y)
First Order Derivatives
z1 z2 z3
• Prewitt operator z4 z5 z6
z7 z8 z9
-1 0 1 -1 -1 -1
-1 0 1 0 0 0
-1 0 1 1 1 1
mag(∇f ) ≈| ( z7 + z8 + z9 ) − ( z1 + z2 + z3 ) | +
| ( z3 + z6 + z9 ) − ( z1 + z4 + z7 ) |
First Order Derivatives
• Sobel operator
-1 0 1 -1 -2 -1
-2 0 2 0 0 0
-1 0 1 1 2 1
• Weights closer neighbor a little more
Example
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Unsharp Masking and Highboost Filtering
̅ 𝑦𝑦)
1. Blur the original image 𝑓𝑓(𝑥𝑥, 𝑦𝑦) to generate 𝑓𝑓(𝑥𝑥,
2. Subtract the blurred image 𝑓𝑓 ̅ 𝑥𝑥, 𝑦𝑦 from the original
image 𝑓𝑓(𝑥𝑥, 𝑦𝑦) (unsharp masking)
3. Add the mask to the original image (highboost
filtering) to generate 𝑔𝑔(𝑥𝑥, 𝑦𝑦)
̅ 𝑦𝑦))
𝑔𝑔 𝑥𝑥, 𝑦𝑦 = 𝑓𝑓 𝑥𝑥, 𝑦𝑦 + 𝑘𝑘(𝑓𝑓 𝑥𝑥, 𝑦𝑦 − 𝑓𝑓(𝑥𝑥,
Unsharp Masking and Highboost Filtering
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Unsharp Masking and Highboost Filtering
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Sharpening
Values can be either
positive or negative
1
0
0
Filter center
Frequency Domain Spatial Domain
Sharpening spatial filter
• The results may be negative.
• You’ll need to scale and/or clip so that the gray levels
of the result span [0, L-1].
Sharpening spatial filter
• Positive coefficients near its center
• Negative coefficients near the outer
periphery
-1 -1 -1
• Note, the sum of the coefficients is
zero
1/9 * -1 8 -1
-1 -1 -1 • Thus, when the mask is applied over
an area of constant intensity, the
result is zero
• Output = 0 if the intensity values are
constant.
Example
Original High-Pass
Second Order Derivatives
• The Laplacian operator
𝜕𝜕 2 𝑓𝑓 𝜕𝜕 2 𝑓𝑓
∇2 𝑓𝑓 = 2 + 2
𝜕𝜕𝑥𝑥 𝜕𝜕𝑦𝑦
• Central difference approximation along x-
direction and y-direction
∇2 𝑓𝑓
= 𝑓𝑓 𝑥𝑥 + 1, 𝑦𝑦 + 𝑓𝑓 𝑥𝑥 − 1, 𝑦𝑦
+ 𝑓𝑓 𝑥𝑥, 𝑦𝑦 + 1 + 𝑓𝑓 𝑥𝑥, 𝑦𝑦 − 1
− 4𝑓𝑓(𝑥𝑥, 𝑦𝑦)
Second Order Derivatives
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Second Order Derivatives
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com
Laplacian for sharpening
𝑓𝑓(𝑥𝑥, 𝑦𝑦) ∇2 𝑓𝑓(𝑥𝑥, 𝑦𝑦)
𝑔𝑔 𝑥𝑥, 𝑦𝑦
= 𝑓𝑓 𝑥𝑥, 𝑦𝑦 + 𝑐𝑐[∇2 𝑓𝑓(𝑥𝑥, 𝑦𝑦)]
𝑔𝑔(𝑥𝑥, 𝑦𝑦) 𝑔𝑔(𝑥𝑥, 𝑦𝑦)
The figure is provided by Pearson Education, Digital Image Processing, Gonzalez & Woods, www.ImageProcessingPlace.com