Lect02 Better 1
Lect02 Better 1
Histogram Equalization
Histogram Matching
03/25/25 1
Histogram Processing
Graphical Representation of Data
Associated with Digital Image
Frequency of occurrence of various gray levels
Histogram h( rk ) nk
rk is the k th intensity value
nk is the number of pixels in the image with intensity rk
K = 0, 1, ………..L-1
nk
Normalized histogram p( rk )
MN
nk : the number of pixels in the image of
size M N with intensity rk
Discrete values:
k
sk T (rk ) ( L 1) pr (rj )
j 0
k nj L 1 k
( L 1) nj k=0,1,..., L-1
j 0 MN MN j 0
03/25/25 3
Histogram Equalization
The intensity levels in an image may be viewed as
random variables in the interval [0, L-1].
Let pr (r ) and ps ( s ) denote the probability density
function (PDF) of random variables r and s.
03/25/25 4
Histogram equalization Histogram
Linearisation requires
construction of a transformation
function sk
5
Example: Histogram Equalization
Suppose that a 3-bit image (L=8) of size 64 × 64 pixels (MN
= 4096) has the intensity distribution shown in following
table.
03/25/25 6
Example: Histogram Equalization
0
s0 T (r0 ) 7 pr (rj ) 7 0.19 1.33 1
j 0
1
s1 T (r1 ) 7 pr (rj ) 7 (0.19 0.25) 3.08 3
j 0
s2 4.55 5 s3 5.67 6
s4 6.23 6 s5 6.65 7
s6 6.86 7 s7 7.00 7
03/25/25 7
Example: Histogram Equalization
03/25/25 8
03/25/25 9
03/25/25 10
Digital Image With Various Gray Levels
Occurrence
6 6 7 7 6 0-0
5 2 2 3 4 1-0
2-3
3 3 4 4 5 3-4
5 7 3 6 2 4-4
7 6 5 5 4 5-5
6-5
7-4
5
4 4 5 4
3
0 1 2
3 4 5 6 7
12
Histogram Components
Concentrated on the low side
of the gray scale
ps ( s )ds pr (r )dr
03/25/25 15
Histogram Equalization
r
s T (r ) ( L 1) pr ( w)dw
0
ds dT (r ) d r
dr
dr
( L 1)
dr
0
pr ( w) dw
( L 1) pr (r )
pr (r )dr pr (r ) pr (r ) 1
ps ( s )
ds ds ( L 1) pr (r ) L 1
dr
03/25/25 16
Example
Suppose that the (continuous) intensity values
in an image have the PDF
2r
2
, for 0 r L-1
pr (r ) ( L 1)
0, otherwise
03/25/25 17
Example
r
s T (r ) ( L 1) pr ( w)dw
0
r 2w
( L 1) 2
dw
0 ( L 1)
2
r
L 1
03/25/25 18
Histogram Equalization
Continuous case:
r
s T (r ) ( L 1) pr ( w)dw
0
Discrete values:
k
sk T (rk ) ( L 1) pr (rj )
j 0
k nj L 1 k
( L 1) nj k=0,1,..., L-1
j 0 MN MN j 0
03/25/25 19
Question
Is histogram equalization always good?
No
03/25/25 20
Histogram Matching
Histogram matching (histogram specification)
—A processed image has a specified histogram
Let pr ( r ) and pz ( z ) denote the continous probability
density functions of the variables r and z. pz ( z ) is the
specified probability density function.
Let s be the random variable with the probability
r
s T ( r ) ( L 1) pr ( w) dw
0
03/25/25 21
Histogram Matching
r
s T (r ) ( L 1) pr ( w)dw
0
z
G ( z ) ( L 1) pz (t )dt s
0
1
z G ( s ) G 1
T (r )
03/25/25 22
Histogram Matching: Procedure
Obtain pr(r) from the input image and then obtain the
values of s r
s ( L 1) pr ( w)dw
0
Mapping from s to z
z G 1 ( s )
03/25/25 23
Histogram Matching: Example
Assuming continuous intensity values, suppose that an
image has the intensity PDF
2r
2
, for 0 r L -1
pr (r ) ( L 1)
0, otherwise
Find the transformation function that will produce an
image whose intensity PDF is
3z 2
, for 0 z ( L -1)
pz ( z ) ( L 1) 3
0, otherwise
03/25/25 24
Histogram Matching: Example
Find the histogram equalization transformation for the input
image
2
r 2w r r
s T (r ) ( L 1) pr ( w)dw ( L 1) dw
0 ( L 1) 2
0
L 1
Find the histogram equalization transformation for the specified
histogram
z 3t 2 z z3
G ( z ) ( L 1) pz (t )dt ( L 1) dt s
0 0 ( L 1) 3 ( L 1) 2
1
zq G ( sk )
03/25/25 26
Example: Histogram Matching
Suppose that a 3-bit image (L=8) of size 64 × 64 pixels (MN
= 4096) has the intensity distribution shown in the following
table (on the left). Get the histogram transformation
function and make the output image with the specified
histogram, listed in the table on the right.
03/25/25 27
Example: Histogram Matching
Obtain the scaled histogram-equalized values,
G ( z1 ) 0.00 0 G ( z2 ) 0.00 0
G ( z3 ) 1.05 1 G ( z4 ) 2.45 2
G ( z5 ) 4.55 5 G ( z6 ) 5.95 6
G ( z7 ) 7.00 7
03/25/25 28
Example: Histogram Matching
03/25/25 29
Example: Histogram Matching
Obtain the scaled histogram-equalized values,
G ( z1 ) 0.00 0 G ( z2 ) 0.00 0
G ( z3 ) 1.05 1 s0 G ( z4 ) 2.45 2 s1
G ( z5 ) 4.55 5 s2 G ( z6 ) 5.95 6 s3
G ( z7 ) 7.00 7 s4 s5 s6 s7
03/25/25 30
Example: Histogram Matching
s0 1, s1 3, s2 5, s3 6, s4 7,
s5 7, s6 7, s7 7.
rk
0
1
2
3
4
5
03/25/25 6 31
7
Example: Histogram Matching
rk zq
0 3
1 4
2 5
3 6
4 7
5 7
6 7
7 7
03/25/25 32
Example: Histogram Matching
03/25/25 33
Example: Histogram Matching
03/25/25 34
03/25/25 35
Local Histogram Processing
Define a neighborhood and move its center from
pixel to pixel
03/25/25 37
Using Histogram Statistics for Image
Enhancement
Average Intensity L 1
M 1 N1
1
m ri p (ri )
MN
f ( x, y )
x 0 y 0
i 0
L 1
un (r ) (ri m) n p (ri )
i 0
Variance L 1 M 1 N1
1
u2 (r ) (ri f ( x, y ) m
2 2 2
m) p (ri )
i 0
MN x 0 y 0
03/25/25 38
Using Histogram Statistics for Image
Enhancement
Local average intensity
L 1
msxy ri psxy (ri )
i 0
Local variance
L 1
2
s xy (ri msxy ) psxy (ri )
2
i 0
03/25/25 39
Using Histogram Statistics for Image
Enhancement: Example
03/25/25 40
Spatial Filtering
linear spatial filtering using a 3X3 kernel
g(x, y), of the filter is the sum of products
of the kernel coefficients and the image pixels
encompassed by the kernel:
g(x,y)=w(-1,-1)f(x-1, y-1) + w(-1,0)f(x-1, y) +…
+ w(0,0)f(x, y) + …..+ w(1,1)f(x+1, y+1)
m 2a 1; n 2b 1
x = 0, 1, 2, …..M-1, y = 0, 1, 2…N-1. a and b are
nonnegetive integers. Our focus is on kernels of odd size in
both coordinate directions. Smallest meaningful size 3X3.
where x and y are varied so that the center (origin) of the
kernel visits every pixel in f once
03/25/25 42
Spatial Correlation & Convolution
The mechanics of spatial convolution are the same, except that the
correlation kernel is rotated by 180°
Thus, when the values of a kernel are symmetric about its center,
correlation and convolution yield the same result.
a b
w( x, y ) f ( x, y ) w(s, t ) f ( x s, y t )
s a t b
03/25/25 43
Spatial Convolution
a b
w( x, y ) f ( x, y ) w( s, t ) f ( x s, y t )
s a t b
03/25/25 44
Spatial Correlation & Convolution
Contd…
W4 W5 W6 z4 z5 z6
W7 W8 W9 z7 z8 z9
03/25/25 45
03/25/25 46
Construction of Spatial Filter
Kernels
First approach is based on formulating filters based on
mathematical properties. For example, a filter that computes the
average of pixels in a neighborhood blurs an image. Computing an
average is analogous to integration. Conversely, a filter that
computes the local derivative of an image sharpens the image.
(Page 238)
03/25/25 47
Separable Filter Kernels
w(s, t ) f ( x s, y t )
g ( x, y ) s a t b a b
w(s, t )
s a t b
03/25/25 54
03/25/25 55
Example: Gross Representation of Objects
03/25/25 56
Order-statistic (Nonlinear) Filters
— Nonlinear
03/25/25 57
Median Filter
03/25/25 59
Median Filter Contd.
03/25/25 60
Max Filter
03/25/25 61
Min Filter
The 0th percentile filter is the Min filter, used for the
opposite purpose.
Example nonlinear spatial filters
–Median filter: Computes the median gray-level value of the
neighborhood. Used for noise reduction.
– Max filter: Used to find the brightest points in an image
–Min filter: Used to find the dimmest points in an image
R = max{z | k =1,2,...,9}
R = min{z | k =1,2,...,9}
03/25/25 62
Sharpening Spatial Filters
(High Pass Filtering)
03/25/25 63
Sharpening Spatial Filters Contd.
(High Pass Filtering)
► Foundation
► Laplacian Operator
03/25/25 65
Sharpening Spatial Filters: Foundation
f
f ( x 1) f ( x)
x
► Derivatives of digital fn: Defined in terms of
differences
► Properties:
First derivative
(1)must be zero in flat segments (areas of constant gray-level
values);
2 f
2
f ( x 1) f ( x 1) 2 f ( x)
x
Similarly, any definition of a second derivative
must be zero in flat areas;
03/25/25 71
Sharpening Spatial Filters: Laplace
Operator Contd.
g ( x, y ) f ( x, y ) c 2 f ( x, y )
where,
f ( x, y ) is input image,
g ( x, y ) is sharpenend images,
c -1 if 2 f ( x, y ) corresponding to Fig. 3.37(a) or (b)
and c 1 if either of the other two filters is used.
03/25/25 72
LAPLACIA + ADDITION WITH ORIGINAL IMAGE
N DIRECTLY
73
03/25/25 74
Unsharp Masking and Highboost Filtering
► Unsharp masking
Sharpen images consists of subtracting an unsharp
(smoothed) version of an image from the original image
e.g., printing and publishing industry
► Steps
1. Blur the original image
03/25/25 75
Unsharp Masking and Highboost Filtering
Let f ( x, y ) denote the blurred image, unsharp masking is
g mask ( x, y ) f ( x, y ) f ( x, y )
Then add a weighted portion of the mask back to the original
g ( x, y ) f ( x, y ) k * g mask ( x, y ) k 0
03/25/25 76
Unsharp Masking: Demo
03/25/25 77
Unsharp Masking and Highboost Filtering:
Example
03/25/25 78
Image Sharpening based on First-Order
Derivatives
For function f ( x, y ), the gradient of f at coordinates ( x, y )
is defined as
f
g x x
f grad( f )
g y f
y
M ( x, y ) | g x | | g y |
z1 z2 z3
M ( x, y ) | z8 z5 | | z6 z5 |
z4 z5 z6
z7 z8 z9
03/25/25 80
Image Sharpening based on First-Order
Derivatives
Sobel Operators
M ( x, y ) | ( z7 2 z8 z9 ) ( z1 2 z2 z3 ) |
z1 z2 z3 | ( z3 2 z6 z9 ) ( z1 2 z4 z7 ) |
z4 z5 z6
z7 z8 z9
03/25/25 81
Image Sharpening based on First-Order
Derivatives
03/25/25 82
Example
03/25/25 83
Example:
Combining
Spatial
Enhancement
Methods
Goal:
Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail
03/25/25 84
Example:
Combining
Spatial
Enhancement
Methods
Goal:
Enhance the
image by
sharpening it
and by bringing
out more of the
skeletal detail
03/25/25 85