Digital Image Processing Chapter # 3
Digital Image Processing Chapter # 3
Chapter # 3
2
Image Enhancement
3
Basic Concepts
4
Basic Concepts
5
Basic Concepts
g(x,y) = T [f(x,y)]
Pixel/point operation:
Neighborhood of size 1x1: g depends only on f at (x,y)
T: a gray-level/intensity transformation/mapping function
Let r = f(x,y) s = g(x,y)
r and s represent gray levels of f and g at (x,y)
Then s = T(r)
Local operations:
g depends on the predefined number of neighbors of f at (x,y)
Implemented by using mask processing or filtering
Masks (filters, windows, kernels, templates) :
a small (e.g. 3×3) 2-D array, in which the values of the
coefficients determine the nature of the process
6
3 basic gray-level transformation functions
• Linear function
– Negative and identity
transformations
• Logarithm function
– Log and inverse-log
transformation
• Power-law function
– nth power and nth root
transformations
7
Identity Function
• Is included in the
graph only for
completeness.
8
Image Negatives
Reverses the gray level order
For L gray levels the transformation function is
s =T(r) = (L - 1) - r
9
Log Transformations
Function of s = cLog(1+r)
10
Log Transformations
12
Inverse Log Transformations
13
Power-Law Transformation
14
Power-Law Transformation
For γ < 1: Expands values of dark pixels,
compress values of brighter pixels
For γ > 1: Compresses values of dark pixels,
expand values of brighter pixels
If γ=1 & c=1: Identity transformation (s = r)
15
Power-Law Transformation
16
Gamma correction
=1/2.5 = 0.4
17
Power-Law Transformation: Example
18
Power-Law Transformation: Example
19
Piecewise-Linear Transformation
Contrast Stretching
Goal:
– Increase the dynamic range of the gray levels for low
contrast images
20
Contrast Stretching Example
21
Piecewise-Linear Transformation: Gray-level slicing
22
Piecewise-Linear Transformation: Bit Plane slicing
23
8 bit planes
Bit-plane 7 Bit-plane 6
Bit- Bit- Bit-
plane 5 plane 4 plane 3
Bit- Bit- Bit-
plane 2 plane 1 plane 0
24
Histograms
25
Example Histogram
06/13/21 26
Example Histogram
27
Histogram Examples
28
Contrast Stretching through Histogram
If rmax and rmin are the maximum and minimum gray level
of the input image and L is the total gray levels of output
image The transformation function for contrast stretching
will be
29
Histogram Equalization
30
Histogram Equalization
31
Histogram Equalization
32
Histogram Equalization
•Spreading out the frequencies in an image (or
equalising the image) is a simple way to improve
dark or washed out images
•The formula for histogram
equalisation is given where sk T (rk )
– rk: input intensity k
34
Histogram Equalization: Example
35
Notice that the minimum value (52) is now 0 and the maximum value (154) is now 255.
Histogram Equalization: Example
36
Histogram Equalization: Example
No. of pixels
6
2 3 3 2 5
4 2 4 3 4
3
3 2 3 5
2
2 4 2 4
1
Gray level
4x4 image
0 1 2 3 4 5 6 7 8 9
Gray scale = [0,9]
histogram
37
Histogram Equalization: Example
Gray
0 1 2 3 4 5 6 7 8 9
Level(j)
No. of
0 0 6 5 4 1 0 0 0 0
pixels
k
n
j 0
j 0 0 6 11 15 16 16 16 16 16
k nj 6 11 15 16 16 16 16 16
s 0 0 / / / / / / / /
j 0 n
16 16 16 16 16 16 16 16
3.3 6.1 8.4
sx9 0 0 9 9 9 9 9
3 6 8
38
Histogram Equalization: Example
No. of pixels
6
3 6 6 3 5
8 3 8 6 4
3
6 3 6 9
2
3 8 3 8
1
Output image
0 1 2 3 4 5 6 7 8 9
Gray scale = [0,9] Gray level
Histogram equalization39
Mathematical/Logical Operations on Images
Addition
–Averaging images for noise removal
Subtraction
–Removal of background from images
–Image enhancement
–Image matching
–Moving/displaced object tracking
Multiplication
–Superimposing of texture on an image
–Convolution and correlation of images
And and or operations
–To remove the unnecessary area of an image through
mask operations
06/13/21 40
Image Averaging for Noise Reduction
06/13/21 41
Image Averaging for Noise Reduction
06/13/21 42
Image Averaging for Noise Reduction
06/13/21 43
Image Subtraction
• Takes two images as input and produces a third
image whose pixel values are those of the first
image minus the corresponding pixel values from
the second image
• Variants
– It is also often possible to just use a single image as
input and subtract a constant value from all the pixels
– Just output the absolute difference between pixel
values, rather than the straightforward signed output.
06/13/21 44
Image Subtraction
• The subtraction of two images is performed in a
single pass
06/13/21 45
Image Subtraction
• If the operator calculates absolute differences, then it is
impossible for the output pixel values to be outside the
range
• In rest of the two cases the pixel value may become
negative
• This is one good reason for using absolute differences.
06/13/21 46
Image Subtraction
• How to solve problem of negative pixels?
• Let we have an 8 bit Grayscale image (Value Range= 0 t0 255)
• The result of image subtraction may come in the range of -255 to
+255
• One scheme can be to add 255 to every pixel and then divide by
2
• Method is easy and fast
• Limitations
– Truncation errors can cause loss of accuracy
– Full range of display may not be utilized
06/13/21 47
Image Subtraction
06/13/21 48
Examples of Image Subtraction
06/13/21 49
Examples of Image Subtraction
06/13/21 50
Example: Background Removal Using Image Subtraction
06/13/21 51
Example: Background Removal Using Image Subtraction
06/13/21 52
Examples of Image Multiplication
06/13/21 53
Logic Operations
06/13/21 54
Example of Logical Operations using Masks
06/13/21 55
Neighbourhood Operations
• Neighbourhood operations simply operate on a
larger neighbourhood of pixels than point
operations
Origin x
• Neighbourhoods are
mostly a rectangle
around a central pixel
• Any size rectangle
(x, y)
and any shape filter Neighbourhood
are possible
06/13/21 57
Simple Neighbourhood Operations
06/13/21 58
Simple Neighbourhood Operations Example
y y
06/13/21 59
The Spatial Filtering Process
Origin x
a b c r s t
d
g
e
h
f
i
* u
x
v
y
w
z
Original Image Filter
Simple 3*3 Pixels
e 3*3 Filter
Neighbourhood
eprocessed = v*e +
r*a + s*b + t*c +
u*d + w*f +
y Image f (x, y) x*g + y*h + z*i
06/13/21 61
Basics of Spatial Filtering
06/13/21 63
Mask Operation Near the Image Border
06/13/21 64
Spatial Filtering for Smoothing
For blurring/noise reduction;
Blurring is usually used in preprocessing steps,
e.g., to remove small details from an image prior to object
extraction, or to bridge small gaps in lines or curves
Equivalent to Low-pass spatial filtering in frequency domain
because smaller (high frequency) details are removed based on
neighborhood averaging (averaging filters)
Implementation: The simplest form of the spatial
filter for averaging is a square
mask (assume m×m mask) with
the same coefficients 1/m2 to
preserve the gray levels
(averaging).
Applications: Reduce noise; smooth false contours
Side effect: Edge blurring
06/13/21 65
Smoothing Filters
06/13/21 66
Smoothing Spatial Filters
• One of the simplest spatial filtering operations we
can perform is a smoothing operation
– Simply average all of the pixels in a neighbourhood
around a central value
– Especially useful
in removing noise
from images
– Also useful for 1
/9 1
/9 1
/9
highlighting gross
detail
1
/9 1
/9 1
/9
1
/9 1
/9 1
/9
67
Simple Averaging Filter
Smoothing Spatial Filtering
Origin x
104 100 108 1
/9 1
/9 1
/9
* /9 /9 /9
1 1 1
99 106 98
95 90 85 1
/9 1
/9 1
/9
1
/9 100
104 1
/9 108
1
/9
Original Image Filter
Simple 3*3 /9 106
1
99 1
/9 198
/9
3*3 Smoothing Pixels
Neighbourhood /9 190
1
95 /9 185
/9 Filter
e = 1/9*106 +
1
/9*104 + 1/9*100 + 1/9*108 +
1
/9*99 + 1/9*98 +
y Image f (x, y) 1
/9*95 + 1/9*90 + 1/9*85
= 98.3333
• The above is repeated for every pixel in the original image to generate the
smoothed image
06/13/21 68
Spatial Filtering for Smoothing (Example)
06/13/21 69
Spatial Filtering for Smoothing (Example)
06/13/21 70
Weighted Smoothing Filters
• More effective smoothing filters can be
generated by allowing different pixels in the
neighbourhood different weights in the
averaging function
– Pixels closer to the 1
/16 2
/16 1
/16
central pixel are more
important 2
/16 4
/16 2
/16
– Often referred to as a
weighted averaging 1
/16 2
/16 1
/16
Weighted Averaging Filter
06/13/21 71
Order-Statistics Filtering
Nonlinear spatial filters
Output is based on order of gray levels in the masked area
(sub-image)
Examples: Median filtering, Max & Min filtering
Median filtering
Assigns the mid value of all the gray levels in the mask to the
center of mask;
Particularly effective when
– the noise pattern consists of strong, spiky components (impulse
noise, salt-and-pepper)
– edges are to be preserved
– Force points with distinct gray levels to be more like their neighbors
06/13/21 72
Median Filtering
06/13/21 73
Median Filtering (Example)
06/13/21 74
Strange Things Happen At The Edges!
• At the edges of an image we are missing pixels
to form a neighbourhood
Origin x
e e
e e e Image f (x, y)
06/13/21 y 75
Strange Things Happen At The Edges!
• There are a few approaches to dealing with
missing edge pixels:
– Pad the image
• Typically with either all white or all black pixels
06/13/21 76
Simple Neighbourhood Operations
Example
x
123 127 128 119 115 130
y
06/13/21 77
Correlation & Convolution
• The filtering we have been talking about so far
is referred to as correlation with the filter itself
referred to as the correlation kernel
• Convolution is a similar operation, with just
one subtle difference
a b c r s t eprocessed = v*e +
d
f
e
g
e
h
* u
x
v
y
w
z
z*a + y*b + x*c +
w*d + u*e +
t*f + s*g + r*h
Original Image Filter
Pixels
06/13/21 78
• For symmetric filters it makes no difference
Spatial Filtering for Image Sharpening
weapons)......
Foundation (Blurring vs Sharpening):
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
06/13/21 79
Spatial Filtering for Image Sharpening
06/13/21 80
Derivatives
First Order Derivative
A basic definition of the first-order derivative of a one-
dimensional function f(x) is the difference
f
f ( x 1) f ( x )
x
2 f
f ( x 1) f ( x 1) 2 f ( x )
x 2
06/13/21 81
First and Second Order Derivatives
06/13/21 82
Example for Discrete Derivatives
06/13/21 83
Comparison between f" and f´
06/13/21 84
Laplacian for Image Enhancement
06/13/21 85
Laplacian for Image Enhancement
06/13/21 86
Laplacian for Image Enhancement
06/13/21 87
Laplacian for Image Enhancement (Example)
06/13/21 88
Laplacian for Image Enhancement (Example)
06/13/21 89
Image Sharpening Based on Unsharp Masking
06/13/21 90
High Boost Filtering
Principal application:
Boost filtering is used when input image is darker than desired,
high-boost filter makes the image lighter and more natural
06/13/21 91
High Boost Filtering Masks
06/13/21 92
1st Derivative Filtering
• Implementing 1st derivative filters is difficult in practice
f
Gx x
f f
G y
y
1st Derivative Filtering (cont…)
• The magnitude of this vector is given by:
f mag (f )
G G2
x
2
y 1
2
1
f f
2 2 2
x y
• For practical reasons this can be simplified as:
f G x G y
1st Derivative Filtering (cont…)
• Now we want to define digital approximations and their
Filter Masks
• For simplicity we use a 3x3 region
• For example z5 denotes f(x,y), z1 denotes f(x-1,y-1)
• A simple approximation for First Derivative is
z1 z2 z3
z4 z5 z6
z7 z8 z9
1st Derivative Filtering (cont…)
A simple approximation for First Derivative is
z1 z2 z3
If we use
z4 z5 z6
z7 z8 z9
1st Derivative Filtering (cont…)
z4 z5 z6
z7 z8 z9
The Masks corresponding to these equations are:
06/13/21 98
Gradient Operators
Normally the smallest mask used is of size 3 x 3
Based on the concept of approximating the gradient several
spatial masks have been proposed:
06/13/21 99
Gradient Operators
06/13/21 100
Gradient Processing (Example)
06/13/21 101
NOTE
The summation of coefficients in all masks
equals 0, indicating that they would give a
response of 0 in an area of constant gray level.
06/13/21 102
Mask used to estimate the Gradient
Combining Spatial Enhancement
Methods (cont…)
(a)
Laplacian filter of
bone scan (a)
(b)
Sharpened version of
bone scan achieved (c)
by subtracting (a)
and (b) Sobel filter of bone
scan (a) (d)
Combining Spatial Enhancement Methods
(cont…)
Result of applying a (h)
power-law trans. to
Sharpened image (g)
which is sum of (a)
and (f) (g)
The product of (c)
and (e) which will be (f)
used as a mask
(e)