Digital Image Processing
Chapter 3:
Image Enhancement in the
Spatial Domain
Types of Image Enhancement in the Spatial Domain
• Single pixel methods
- Gray level transformations
Example
- Historgram equalization
- Contrast stretching
- Arithmetic/logic
operations Examples
- Image subtraction
- Image averaging
• Multiple pixel methods
Examples
Spatial filtering
- Smoothing filters
- Sharpening filters
What is spatial domain processing?
Spatial domain techniques operate directly on the pixels of an image
Application: Image Enhancement
• Contrast adjusting
• Gamma correction
• Histogram processing & matching
• Sharpening
What is spatial domain processing?
Point processing vs. Neighborhood processing techniques
▪ Point processing transforms a pixel’s value as function of its value alone. It does not
depend on the values of the pixel’s neighbors.
▪ Neighborhood processing transforms a pixel’s value based on intensity of the pixels
in the neighborhood.
Point processing
Gray Level Transformation
[ f (x, y)]
Transformation function T(.) g(x, y) = T
s = T (r) s and r to denote, respectively, the intensity
of g and f at any point (x, y).
Contrast
enhancement
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Linear Transformation
Original
digital
s = a*r+b mammogram
Image Negative
Original
digital
mammogram
Output intensity
s = L −1− r
Negative
digital
mammogram
0 L-1
Input intensity
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Thresholding
Original
digital
mammogram
Log Transformations
Application
s = clog(r +1)
Fourier
spectrum
Log Tr. of
Fourier
spectrum
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Power-Law Transformations (Gamma Correction)
s = cr
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Power-Law Transformations : Gamma Correction Application
Power-Law Transformations : Gamma Correction Application
Power-Law Transformations : Gamma Correction Application
Question
Implement these transforms on
MATLAB/OpenCV and show the results
Contrast Stretching
Before contrast
Contrast enhancement
After
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Gray Level Slicing
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Bit-plane Slicing
Bit 7 Bit 6
Bit 5
Bit 3
Bit 2 Bit 1
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Histogram
Demo
MATLAB: imhist
OpenCV: cv2.calcHist
Histogram Shape
Monotonically Increasing Function
Function T
s = T(r)
Histogram processing
function
1. Monotonically increasing
function
2. 0 T (r) 1 for 0 r 1
Probability Density Function
Histogram Probability Density Functions (PDF)
Random variables ps(s), pr(r ) denote the PDFs of intensity values r and s in two different images
s =T(r)
dr
ps (s) = pr (r)
ds
Probability Density Function
Any function ƒ that describes the probability density in
terms of the input variable x is a probability density
function if and only if it is non-negative and the area
under the graph is 1:
a probability density function (pdf)
of a Normal N(0,1,σ2) Population
Histogram Equalization
a uniform
probability
density function.
Probability density function of uniform distribution
a density function that is constant, making it the simplest kind of
density function.
The equation for the standard uniform distribution is
Histogram Equalization
Demo
Continuous PDF MATLAB : histeq
OpenCV: cv2.equalizeHist
Histogram Digital Image
k
sk = T (rk ) = pr (rj )
j=0
This is called a histogram
k
nj equalization or histogram
= linearization transformation.
j=0 N
nj = the number of pixels with intensity = j
N = the number of total pixels
Histogram Equalization Example
Intensity # pixels Accumulative Sum of Pr
0 20 20/100 = 0.2
1 5 (20+5)/100 = 0.25
2 25 (20+5+25)/100 = 0.5
3 10 (20+5+25+10)/100 = 0.6
4 15 (20+5+25+10+15)/100 = 0.75
5 5 (20+5+25+10+15+5)/100 = 0.8
6 10 (20+5+25+10+15+5+10)/100 = 0.9
7 10 (20+5+25+10+15+5+10+10)/100 = 1.0
Total 100 1.0
Histogram Equalization Example (cont.)
Intensity No. of Pixels Acc Sum Output value Quantized
(r) (nj) of Pr Output (s)
0 20 0.2 0.2x7 = 1.4 1
1 5 0.25 0.25*7 = 1.75 2
2 25 0.5 0.5*7 = 3.5 3
3 10 0.6 0.6*7 = 4.2 4
4 15 0.75 0.75*7 = 5.25 5
5 5 0.8 0.8*7= 5.6 6
6 10 0.9 0.9*7 = 6.3 6
7 10 1.0 1.0x7 = 7 7
Total 100
Histogram Equalization
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Histogram Equalization (cont.)
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Histogram Equalization (cont.)
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Histogram Equalization (cont.)
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Histogram Equalization (cont.)
Histogram Eq.
Histogram equalization Low
Contrast
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Histogram Equalization (cont.)
Limitations
Histogram Equalization (cont.)
Limitations
Project
Topic about Histogram Equalization Techniques
Brightness Preserving by Histogram Equalization
Brightness preserving Bi-Histogram Equalization
Brightness Preserving Bi-Histogram Equalization
Contrast Limited Adaptive Histogram Equalization
Brightness Preserving Dynamic Histogram Equalization
Brightness Preserving Dynamic Fuzzy Histogram Equalization
…..
Histogram Matching : definition
There are applications in which attempting to base
enhancement on a uniform histogram is not the best approach.
In particular, it is useful sometimes to be able to specify the
shape of the histogram that we wish the processed image to
have.
The method used to generate a processed image that has a
specified histogram is called histogram matching or histogram
specification.
Histogram Matching :Algorithm
Histogram
Histogram Matching : Algorithm (cont.)
s = T(r) v = G(z)
3 z = G-1(v)
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Histogram Matching Example
Histogram Histogram
input image output image
Intensity # pixels Intensity # pixels
(s) (z)
0 20 0 5
1 5 1 10
2 25 2 15 User
3 10 3 20
4 15 4 20
5 5 5 15
6 10 6 10
7 10 7 5
Total 100 Total 100
Histogram Matching Example (cont.)
1. Histogram Equalization
r (nj) Pr s z (nj) Pz v
0 20 0.2 1 0 5 0.05 0
1 5 0.25 2 1 10 0.15 1
2 25 0.5 3 2 15 0.3 2
3 10 0.6 4 3 20 0.5 4
4 15 0.75 5 4 20 0.7 5
5 5 0.8 6 5 15 0.85 6
6 10 0.9 6 6 10 0.95 7
7 10 1.0 7 7 5 1.0 7
sk = T(rk) vk = G(zk)
Histogram Matching Example (cont.)
2. Map
Actual Output
r → s v → z Histogram
r s s → v v z r z z # Pixels
0 1 0 0 0 1 0 0
1 2 1 1 1 2 1 20
2 3 2 2 2 2 2 30
3 4 4 3 3 3 3 10
4 5 5 4 4 4 4 15
5 6 6 5 5 5 5 15
6 6 7 6 6 5 6 10
7 7 7 7 7 6 7 0
sk = T(rk) zk = G-1(vk)
Histogram Matching Example (cont.)
Desired histogram
Transfer function
Actual histogram
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Histogram Matching Example (cont.)
After After
Original
histogram equalization histogram matching
image
Local Enhancement : Local Histogram Equalization
Concept: Perform histogram equalization in a small neighborhood
After Local Hist Eq.
Orignal image After Hist Eq. In 7x7 neighborhood
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Local Enhancement – low contrast
Local enhancement: compare local average & global mean, to Global standard deviation
SEM (scanning electron microscope) image of a tungsten filament
E f ( x, y) when m s xy k 0 M G and k1 DG s xy k 2 DG
g ( x, y) =
f ( x, y) otherwise
Original image Local Variance image Multiplication factor
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Local Enhancement
Output image
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Local Enhancement – low contrast
Logic Operations
Application:
AND
OR
Original Image mask ROI:
image Region of Interest
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Arithmetic Operation: Subtraction
Application: Error measurement
Error
image
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Arithmetic Operation: Subtraction (cont.)
Application: Mask mode radiography in angiography work
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Arithmetic Operation: Image Averaging
Application : Noise reduction
Arithmetic Operation: Image Averaging (cont.)
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Basics of Spatial Filtering
Sometimes we need to manipulate values obtained from
neighboring pixels
Example: How can we compute an average value of pixels
in a 3x3 region center at a pixel z?
Basics of Spatial Filtering (cont.)
Basics of Spatial Filtering (cont.)
Basics of Spatial Filtering (cont.)
• Correlation consists of moving the center of a kernel over an image, and computing the
sum of products at each location.
• The mechanics of spatial convolution are the same, except that the correlation kernel is
rotated by 180°.
Basics of Spatial Filtering (cont.)
Basics of Spatial Filtering (cont.)
Smoothing the intensities of an image
Basics of Spatial Filtering (cont.)
Step 2. Multiply every pixel by 1/9 and then sum up the values
…
3 4 4
… 9 7 6 …
1 1 1
3 6 1 y= 3+ 4 + 4
9 9 9
…
1 1 1
+ 9 + 7 + 6
9 9 9
X
1 1 1
1 1 1 Mask or + 3 + 6 + 1
1 9 9 9
1 1 1 Window or
9 Template or convolution filter, convolution mask, or
1 1 1
Kernel convolution kernel
Basics of Spatial Filtering (cont.)
Question: How to compute the 3x3 average values at every pixels?
Basics of Spatial Filtering (cont.)
Basics of Spatial Filtering (cont.)
The 3x3 averaging method is one example of the mask
operation or Spatial filtering.
The mask operation has the corresponding mask (sometimes
called window or template or kernel).
The mask contains coefficients to be multiplied with pixel
values.
Example : moving averaging
w(1,1) w(2,1) w(3,1)
1 1 1
w(1,2) w(2,2) w(3,2) 1
1 1 1
9
w(3,1) w(3,2) w(3,3) 1 1 1
Mask coefficients The mask of the 3x3 moving average
filter has all coefficients = 1/9
The term linear spatial filtering, we mean convolving a kernel with an image.
Basics of Spatial Filtering (cont.)
The mask operation at each point is performed by:
1. Move the reference point (center) of mask to the
location to be computed
2. Compute sum of products between mask coefficients
and pixels in subimage under the mask.
Mask frame
…
p(1,1) p(2,1) p(3,1) w(1,1) w(2,1) w(3,1)
… p(2,1) p(2,2) p(3,2) … w(1,2) w(2,2) w(3,2)
p(1,3) p(2,3) p(3,3) w(3,1) w(3,2) w(3,3)
…
Subimage Mask coefficients
N M
The reference point y = w(i, j) p(i, j)
of the mask i=1 j=1
Basics of Spatial Filtering (cont.)
The spatial filtering on the whole image is given by:
1. Move the mask over the image at each location.
2. Compute sum of products between the mask coefficeints
and pixels inside subimage under the mask.
3. Store the results at the corresponding pixels of the
output image.
4. Move the mask to the next location and go to step 2
until all pixel locations have been used.
Examples of Spatial Filtering Masks
Examples of the masks
Sobel operators 3x3 moving average filter
-1 0 1 -1 -2 -1 1 1 1
1
-2 0 2 0 0 0 1 1 1
9
-1 0 1 1 2 1 1 1 1
P P
to compute to compute
x y
3x3 sharpening filter
-1 -1 -1
1
-1 8 -1
9
-1 -1 -1
Examples of Spatial Filtering Masks
Smoothing (LOWPASS) Linear Filter : Moving Average
• Smoothing (also called averaging) spatial filters are used to
reduce sharp transitions in intensity
• Application of smoothing is noise reduction.
• Convolving a smoothing kernel with an image blurs the image,
with the degree of blurring being determined by the size of the
kernel and the values of its coefficients
Smoothing Linear Filter : Box Kernel
Application : noise reduction
and image smoothing
Disadvantage: lose sharp details
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Lowpass filtering (smoothing) with a box kernel
Smoothing Linear Filter (cont.)
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Smoothing Linear Filter: Gaussian Kernel
Smoothing Linear Filter: Gaussian Kernel
Smoothing Linear Filter: Gaussian Kernel
the shape of a ramp smoothing of edges
Order-Statistic Filters
Original image
subimage
Statistic parameters
Mean, Median, Mode,
Min, Max, Etc.
Moving
window
Output image
Order-Statistic Filters: Median Filter
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Sharpening (HIGHPASS) Spatial Filters
There are intensity discontinuities near object edges in an image
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
First and Second Derivative (Laplacian)
The first order derivative
Laplacian : an isotropic derivative operator
First and Second Derivative (Laplacian)
Laplacian Sharpening : How it works
Laplacian Sharpening : How it works (cont.)
1.5
p(x) 0.5
-0.5
0 50 100 150 200
1.5
1
d2 p
p( x) −10 0.5
dx2
0
-0.5
0 50 100 150 200
Laplacian sharpening results in larger intensity discontinuity
near the edge.
Laplacian Sharpening : How it works (cont.)
Before sharpening
p(x)
After sharpening
d2 p
p(x) −10 2
dx
Laplacian Masks
Laplacian Sharpening Example
p 2 P
2 P P − 2 P
Gonzalez and Richard E. Wood, Digital
Image Processing, 2nd Edition.
Laplacian Sharpening (cont.)
Mask for
P − 2 P
0 -1 0 -1 -1 -1
-1 5 -1 or -1 9 -1
Mask for 0 -1 0 -1 -1 -1
2 P
1 1 1
1 -8 1
1 1 1
or
0 1 0
1 -4 1
0 1 0
(Images from Rafael C. Gonzalez and
Richard E. Wood, Digital Image Processing,
2nd Edition.
Unsharp Masking and High-Boost Filtering
Subtract a blurred version from the original : unsharp masking
A further generalization: unsharp high-boost filtering
Unsharp Masking
Unsharp Masking and High-Boost Filtering
-1 -1 -1 0 -1 0
-1 A+8 -1 -1 A+4 -1
-1 -1 -1 0 -1 0
Equation:
Af ( x, y) − 2 f ( x, y) The center of the mask is negative
f hb ( x, y) =
Af ( x, y )+ 2
f ( x, y) The center of the mask is positive
Unsharp Masking and High-Boost Filtering (cont.)
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Unsharp Masking and High-Boost Filtering (cont.)
First Order Derivative – the Gradient
Graient of f at (x,y)
Magnitude of the gradient
Approximation
First Order Derivative – the Gradient
Intensity profile 1
f(x) 0.5 Edges
0
20 40 60 80 100 120 140 160 180 200
1st derivative 0.2
df 0
dx
-0.2
0 50 100 150 200
2nd derivative 0.2
df
0.1
dx
0
0 50 100 150 200
Use of Gradient for Edge Enhancement
First Order Partial Derivative:
Sobel operators
-1 0 1 -1 -2 -1
-2 0 2 to compute 0 0 0 to compute
-1 0 1 1 2 1
P P P
x y
First Order Partial Derivative: Image Gradient
Gradient magnitude
A gradient image emphasizes edges
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
First Order Partial Derivative: Image Gradient
P
P
y
P
x
P
Image Enhancement in the Spatial Domain : Mix things up !
2 P
P
A B D smooth
+ -
noisier results than the gradient.
Sharpening C (Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition. E
Image Enhancement in the Spatial Domain : Mix things up !
The product final
will preserve sharpened
details in image,
the strong
areas while
reducing noise
in the
relatively flat
areas.
Power
G
C E Law Tr.
F A
Multiplication
H