(IP'22) Lecture 3 - Enhancemet II - Spatial Filtering
(IP'22) Lecture 3 - Enhancemet II - Spatial Filtering
Digital Image
Processing
10/19/2022 2
Image
Enhancement II
10/19/2022 3
Fundamental Steps of DIP
Wavelet and
Multi- Compression
resolution
Output is generally images
Knowledge base
Restoration Segmentation
Representation
Enhancement and
Description
Object
Acquisition Recognition
Problem
10/19/2022 Domain 4
2. Histogram
Processing
10/19/2022 5
Image Histogram
• A way of viewing the distribution of
color/intensities in an image.
h(x) = the number of pixels in I with intensity value x
• A plot of pixels values against pixel counts.
Frequencies
Luminosity
10/19/2022 7
Image Histogram – (cont.)
Color Image
I has 3 histograms:
hR(x) = number of pixels in I(:,:,1) with intensity value x
hG(x) = number of pixels in I(:,:,2) with intensity value x
hB(x) = number of pixels in I(:,:,3) with intensity value x
10/19/2022 8
Image Histogram – (cont.)
Color Image
Luminosity
hI R hI G hI B
10/19/2022 9
Image Histogram – (cont.)
Color Image
10/19/2022 10
Image Histogram – (cont.)
Color to gray
• Lightness
Lightness = ½ × (max(R,G,B) + min(R,G,B))
• Luminosity
Luminosity = 0.21 × R + 0.72 × G + 0.07 × B
• Average
Average Brightness = (R + G + B) ÷ 3
10/19/2022 11
Image Histogram – (cont.)
Histogram Definition (normalized)
• An estimate to the probability of occurrence
of intensity level rk in an image.
nk
p(rk ) = , k = 0,1,2,..., L − 1
MN
MN : totoal number of pixels
nk : number of pixels having value rk
• Not unique.
10/19/2022 12
Image Histogram – (cont.)
Examples
10/19/2022 13
Image Histogram – (cont.)
Examples
10/19/2022 14
Image Histogram – (cont.)
Examples
10/19/2022 15
Image Histogram – (cont.)
Examples
10/19/2022 16
Image Histogram – (cont.)
Examples
10/19/2022 17
Image Histogram – (cont.)
Examples
Dark image
• Note that the high
contrast image has the
most evenly spaced Bright image
histogram.
Poor
contrast
Can we do image
that?
Good
contrast
10/19/2022
image 18
Histogram Processing
• Histogram equalization
Dark image
• Histogram matching
Bright image
Poor
contrast
image
Good
contrast
10/19/2022
image 19
Histogram Processing – (cont.)
Histogram Equalization
• Spreading out the frequencies in an image
(or equalizing the image) is a simple way to
improve dark or washed out images.
• Transfer the gray levels so that the histogram
of the resulting image is equalized to be a
constant (theoretically).
• Remap pixel values for image I so that its
histogram is as close to constant as possible.
10/19/2022 20
Histogram Processing – (cont.)
Histogram Equalization Why?
10/19/2022 21
(desired)
Known ?
Known
(given)
10/19/2022 22
Histogram Processing – (cont.)
Histogram Equalization
• For any given mapping function y = f(x)
between the input and output images the
following holds
if the gray levels are assumed to be in the ranges between 0 and 1 then p(y) = 1 and
The Cumulative
Distribution
Function of x
10/19/2022 23
Histogram Processing – (cont.)
Histogram Equalization
• PDF of a random variable X is a measure of the
likelihood for X to occur at a given point .
x2
This is the probability
Px = Prob( x1 X x2 ) = f ( x)dx that an arbitrary pixel
x has value f(x).
x1
• Integrating gives the Cumulative Distribution
Function CDF of X: x
Fx = Prob( X x) = f ( x)dx
− This is the probability that any
given pixel from f has value less
10/19/2022 than or equal to x. 24
= P(x) = CDF (x)
Notice
10/19/2022 25
10/19/2022 26
10/19/2022 27
Histogram Processing – (cont.)
Histogram Equalization - Discrete
if the gray levels are assumed to be in the ranges between 0 and L then p(y) = 1/(L-1)
and x
y = ( L − 1) p (u)du
0
10/19/2022 28
Histogram Processing – (cont.)
Histogram Equalization - Discrete
x == rk y == sk == f(x) == f(rk ) = T(rk)
( L − 1) k
sk = T ( rk ) =
N j =0
nj, k = 0, 1, 2,..., L − 1
10/19/2022 30
Histogram Processing – (cont.)
Histogram Equalization
10/19/2022 31
Histogram Processing – (cont.)
Histogram Equalization
10/19/2022 32
Histogram Processing – (cont.)
Histogram Equalization
10/19/2022 33
Histogram Processing – (cont.)
Histogram Equalization
10/19/2022 34
Histogram Processing – (cont.)
Histogram Equalization
10/19/2022 35
Histogram Processing – (cont.)
Histogram Matching (Specification)
• Mapping the histogram of an image to a
known histogram of another image.
• A method of color adjustment of two images
using the image histograms.
10/19/2022 36
Histogram Processing – (cont.)
Histogram Matching (Specification)
10/19/2022 37
Histogram Processing – (cont.)
Histogram Matching (Specification)
• Equalize the histogram of the input image.
10/19/2022 38
Histogram Processing – (cont.)
Histogram Matching (Specification) - Discrete
• Discrete histograms and are not necessarily
identical. We need to relate each gray level in
x to a gray level in z, such that for every level
i, find level j where hist1 best matches hist2.
10/19/2022 39
Histogram Processing – (cont.)
Histogram Matching (Specification) - Discrete
10/19/2022 40
Histogram Processing – (cont.)
Histogram Matching (Specification) – Discrete
• Example
10/19/2022 41
Histogram Processing – (cont.)
Histogram Matching (Specification) – Discrete
• Example Resulting lookup table
Finding best level for best fit
10/19/2022 42
Histogram Processing – (cont.)
Histogram Matching (Specification) – Discrete
• Example
10/19/2022 43
Histogram Processing – (cont.)
Histogram Matching (Specification) – Discrete
• Example
10/19/2022 44
Histogram Processing – (cont.)
Histogram Matching (Specification) – Discrete
• Example
10/19/2022 45
Histogram Processing – (cont.)
Histogram Matching (Specification) – Discrete
• Example
10/19/2022 46
Histogram Processing – (cont.)
Histogram Matching (Specification) – Discrete
• Example
10/19/2022 47
Histogram Processing – (cont.)
Local Histogram Equalization
• Example
10/19/2022 48
Histogram Processing – (cont.)
Local Histogram Equalization
• Example
10/19/2022 49
Notes on Image Enhancement
• For human perception
– The visual evaluation of image quality is a highly
subjective process.
– It is hard to standardize the definition of a good image.
• For machine perception
– The evaluation task is easier.
– A good image is one which gives the best machine
recognition results.
• A certain amount of trial and error usually is
required before a particular approach is
selected.
10/19/2022 50
3. Spatial Filtering
(cont.)
10/19/2022 51
Contents
1. Neighborhood Operations
2. What is Spatial Filtering? How?
Correlation and Convolution
3. Smoothing Filters
Linear and Nonlinear
4. Sharpening Filters
First Derivatives
Second Derivatives
Neighborhood Operations
• For any specific location (x, y), the value of
g at that location is the result of applying an
operation to the pixels in the neighborhood
with origin (x, y).
Origin Origin
x x
Operation
(x, y) (x, y)
y y
original 3x3 average
Neighborhood Operations – (cont.)
Example
Original Image Output Image
x x
123 127 128 119 115 130
y y
original 3x3 average
Neighborhood Operations – (cont.)
Example
Origin Origin
x x
Operator
(x, y) (x, y)
m = 2a + 1, n = 2b + 1
What is Spatial Filtering? – (cont.)
Convolution two remarks
a b c r s t eprocessed = v×e +
z×a + y×b + x×c +
d
g
e
h
f
i
* u
x
v
y
w
z
w×d + u×f +
t×g + s×h + r×i
Original Image Filter
Pixels
Operation
(x, y) (x, y)
e e e
y
Image f (x, y)
What is Spatial Filtering? – (cont.)
Dealing with Image Borders
• Omit missing pixels.
- Only works with some filters.
- Can add extra code and slow down processing.
• Pad the image.
- Typically with either all white or all black pixels.
• Replicate border pixels.
• Truncate the image.
• Allow pixels wrap around the image.
- Can cause some strange image artifacts.
What is Spatial Filtering? – (cont.)
Dealing with Image Borders
Filtered Image:
Zero Padding
Filtered Image:
Wrap Around Edge
Pixels
What is Spatial Filtering? – (cont.)
Basic rule
• To generate an m×n linear spatial filter, we
need to specify mn mask coefficients
(weights).
• These are selected based on what the filter is
intended to do.
1. Smoothing
Filters
10/19/2022 74
Smoothing Filters
Main applications:
• Used for blurring and noise reduction.
• Removing small details prior to object
extraction.
• Bridging small gaps in lines and curves.
• Linear and nonlinear filters.
Smoothing Filters – (cont.)
Example: OCR preprocessing.
Smoothing Filters – (cont.)
16 grey levels (4 bpp) 8 grey levels (3 bpp) 4 grey levels (2 bpp) 2 grey levels (1 bpp)
Smoothing Filters – (cont.)
Averaging Filter
• One of the simplest linear
spatial filtering operations.
• Response= average all of
the pixels in a 9
1
neighbourhood. R = z i Simple averaging
9 i =1
filter
Averaging Filter 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
81 1 1
• 9×9
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
Smoothing Filters – (cont.) 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
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
Averaging Filter
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 1
1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
289 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
• 17×17
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 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 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Smoothing Filters – (cont.)
Averaging Filter
• Highlighting gross detail: the size of the mask
determines the relative size of the objects blended.
A B
Sharpening Filters – (cont.)
1st Derivative Example in 1D:
Image Strip
8
7
6
5
4
3
2
1
0
1st Derivative
8
6
4
2
0
-2
-4
-6
-8
Sharpening Filters – (cont.)
2nd Derivative:
The formula for the 2nd derivative of a function
is as follows:
f
2
= f ( x + 1) + f ( x − 1) − 2 f ( x )
x
2
7
6
5
4
3
2
1
0
2nd Derivative
10
-5
-10
-15
8
7
6
5
Profile 4
3
2
1
0
1st Derivative
8
6
4
2
0
1st -2
-4
-6
-8
2nd Derivative
10
-5
2nd -10
-15
Sharpening Filters – (cont.)
Example in 1D:
• Note that the sign of the 2nd derivative
changes at the onset and end of ramp and
step → zero crossing → useful in locating
edges.
• The 2nd derivative is more useful for image
enhancement than the 1st derivative
–Stronger response to fine detail.
–Simpler implementation.
Sharpening Filters – (cont.)
Second Derivative (Laplacian)
The Laplacian is defined as follows:
2
f 2
f
f = 2 + 2
2
x y
where in the x direction:
f
2
= f ( x + 1, y ) + f ( x − 1, y ) − 2 f ( x, y )
2
x
and in the y direction:
f
2
= f ( x, y + 1) + f ( x, y − 1) − 2 f ( x, y)
y
2
Sharpening Filters – (cont.)
Second Derivative (Laplacian)
Hence the Laplacian is given by:
f = [ f ( x + 1, y) + f ( x −1, y)
2
+ f ( x, y + 1) + f ( x, y −1)]
− 4 f ( x, y)
+ f ( x, y + 1) + f ( x, y −1)]
− 4 f ( x, y)
0 1 0
= -
Sharpened Original Laplacian
Image Image Filtered Image
Sharpening Filters – (cont.)
Second Derivative (Laplacian)
Sharpening Filters – (cont.)
Second Derivative (Laplacian)
Sharpening Filters – (cont.)
Second Derivative (Laplacian)
Subtracting or adding a weighted Laplacian
depending on the sign of the mask coefficients.
Sharpening Filters – (cont.)
Second Derivative (Laplacian)
The entire enhancement can be combined into
a single filtering operation,
g ( x, y) = f ( x, y) − f
2
= f ( x, y) − [ f ( x + 1, y) + f ( x −1, y)
+ f ( x, y + 1) + f ( x, y −1)
− 4 f ( x, y)]
= 5 f ( x, y) − f ( x + 1, y) − f ( x −1, y)
− f ( x, y + 1) − f ( x, y −1)
Sharpening Filters – (cont.)
Second Derivative (Laplacian)
This gives us a new filter which does the whole
job for us in one step.
0 -1 0
-1 5 -1
0 -1 0
Sharpening Filters – (cont.)
Second Derivative (Laplacian)
There are lots of slightly different versions:
0 1 0 1 1 1
Simple Variant of
1 -4 1 1 -8 1
Laplacian Laplacian
0 1 0 1 1 1
-1 -1 -1
-1 9 -1
-1 -1 -1
Sharpening Filters – (cont.)
First Derivative (Gradient)
The Gradient of f(x, y) at coordinates (x, y) is
defined as the column vector:
f
Gx x
f = = f
G y
y
Sharpening Filters – (cont.)
First Derivative (Gradient)
The magnitude of this vector is given by:
M ( x, y) = mag (f )
= G +G2
x
2
y
1
2
1
f 2 f 2
2
= +
x y
For practical reasons this can be simplified as:
M ( x, y ) G x + G y
Sharpening Filters – (cont.)
First Derivative (Gradient)
Approximating the partial derivative in the x-
direction == third row – first row
Approximating the partial derivative in the y-
direction == third column – first column
f(x-1,y-1) f(x-1,y) f(x-1,y+1) z1 z2 z3
f(x,y-1) f(x,y) f(x,y+1)
= z4 z5 z6
0 0 0 -2 0 2
1 2 1 -1 0 1
Assignment
- Textbook Chapter 3: 3, 4, 5, 6, 7
- Check associated problems
Chapter 3 14, 17, 18, 20, 21, 22, 23, 25, 26, 27, 30
10/19/2022 126
References
- Gonzalez and Woods, Digital Image Processing.
- Peters, Richard Alan, II, “Spatial Filtering 1 and 2", Lectures
on Image Processing, Vanderbilt University, Nashville, TN,
April 2008, Available on the web at the Internet Archive,
http://www.archive.org/details/Lectures_on_Image_Processing.
10/19/2022 127