COE031 Compiler
Fall 2024-2025
Histogram Processing
Digital Image Processing
Assist. Prof. Dr. Ali HAMİTOĞLU
Department of Computer Engineering
24/11/24 13:35:24 Presented by Dr. Ali HAMİTOĞLU 1
Goals
• Review Of Previous Lecture
• Image Histogram
• Histogram Equalization
– Contrast Enhancement
• Histogram Specification
24/11/24 13:35:25 Presented by Dr. Ali HAMİTOĞLU 2
Previous Lecture
24/11/24 13:35:25 Presented by Dr. Ali HAMİTOĞLU 3
1st Derivative (cont.)
• The gradient of an image:
The gradient points in the direction of most rapid increase
in intensity.
Gradient direction
The edge strength is given by the gradient magnitude
24/11/24 13:35:25 Presented by Dr. Ali HAMİTOĞLU 4
The Laplacian (cont…)
• The Laplacian equation can be implemented using
convolution with the kernel shown below:
0 1 0
1 -4 1
0 1 0
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 5
But That Is Not Very Enhanced!
The result of a Laplacian filtering is
not an enhanced image
We have to do more work in order to
get our final image
Subtract the Laplacian result from the
original image to generate our final Laplacian
sharpened image Filtered Image
Scaled for Display
g ( x, y) = f ( x, y) - Ñ f 2
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 6
Unsharp masking
Used by the printing industry
Subtracts an unsharped (smooth) image from the
original image f(x,y).
1. Blur the image
b(x,y)=Blur{f(x,y)}
2. Subtract the blurred image from the original (the
result is called the mask)
gmask(x,y)=f(x,y)-b(x,y)
3. Add the mask to the original
g(x,y)=f(x,y)+k gmask(x,y) with k non negative
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 7
Sobel Operators
Edge detection is crucial in DIP
Based on the previous equations we can derive the
Sobel Operators
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
1 2 1 -1 0 1
To filter an image it is filtered using both operators the
results of which are added together
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 8
Today’s Lecture
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 9
Image Histograms
• The histogram of an image shows us the
distribution of grey levels in the image
• Highly useful in image processing, especially in
segmentation
Frequencies
Grey Levels
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 10
Image Histograms
• Let rk , for k=0,1,2,…,L-1, denote the intensities of an L-level digital
image f(x,y).
• The unnormalized histogram of f is defined as
• where nk is the number of pixels in f with intensity rk
• The subdivisions of the intensity scale are called histogram bins.
• Histogram shape is related to image appearance.
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 11
Image Histograms
• Normalized histogram of f is defined as
• M is the rows and N is the number of columns.
• Mostly, we work with normalized histograms.
• The sum of p(rk) for all values of k is always 1.
• The components of p(rk) are estimates of the probabilities of intensity
levels
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 12
Histogram Examples (cont…)
Dark Image
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 13
Histogram Examples (cont…)
Bright Image
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 14
Histogram Examples (cont…)
Low Contrast Image
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 15
Histogram Examples (cont…)
High Contrast Image
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 16
Histogram Examples (cont…)
High Contrast Image
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 17
Histogram Examples (cont…)
• A high contrast image has the pixels distributed
uniformly
• A high contrast image tend to occupy the entire
range of possible intensity levels
• It will exhibit a large variety of gray tones.
• Such image will have a lot of details and has a
high dynamic range.
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 18
Image Histograms
• Can we transform a given image into another one whose
histogram is uniformly distributed?
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 19
Histogram Equalization
• Histogram equalization is used to enhance contrast of an image
using its histogram.
• Let us assume the variable r denote the intensities of an input image
– r is in the range [0, L -1],
• We need a transformations (intensity mappings) of the form:
• s is the output intensity value
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 20
Histogram Equalization
• Assumptions
• In some cases where inverse is needed, so
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 21
Histogram Equalization
Monotonic increasing function Strictly monotonic increasing function
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 22
Histogram Equalization
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 23
Histogram Equalization
a. T(r) is a strictly monotonically increasing function
in the interval 0 £ r £ L -1;
b. 0 £ T (r ) £ L -1 for 0 £ r £ L -1.
T (r ) is continuous and differentiable.
ps (s)ds = pr (r )dr
s = T (r ) 0 £ r £ L -1
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 24
Histogram Equalization
cumulative distribution function (CDF)
r
s = T (r ) = ( L - 1) ò pr ( w)dw of random variable r.
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 ø
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 25
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
Histogram equalization or histogram linearization
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 26
Example: Histogram Equalization
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 27
Example: Histogram Equalization
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 28
Example: Histogram Equalization
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 29
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.
Get the histogram equalization transformation function and give the ps(sk) for each sk.
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 30
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
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 31
Example: Histogram Equalization
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 32
Example
(a) Image from Phoenix Lander. (b) Result of histogram equalization. (c) Histogram of image
(a). (d) Histogram of image (b). (Original image courtesy of NASA.)
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 33
Histogram Processing
• Histogram is an approximation to a PDF
• No new allowed intensity levels are created
– Discrete histogram equalization may not produce a perfectly flat
histograms
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 34
Histogram Processing
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 35
Histogram Processing
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 36
Histogram Processing
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 37
Histogram Processing
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 38
HISTOGRAM MATCHING (SPECIFICATION)
• Histogram equalization generates an output image with a
uniform histogram.
• Sometime, we specify the shape of the histogram that we
want the processed image to have.
• In such applications, histogram equalization is not
suitable.
• The solution is Histogram matching
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 39
HISTOGRAM MATCHING (SPECIFICATION)
• The goal of histogram matching is to take an input
image and generate an output image that is based
upon the shape of a specific (or reference)
histogram.
• The method used to generate images that have a
specified histogram is called histogram matching or
histogram specification.
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 40
Histogram Matching
Histogram matching (histogram specification)
— generate a processed image that 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
Define a random variable z with the probability
z
G ( z ) = ( L - 1) ò pz (t ) dt = s
0
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 41
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
• Use the specified PDF and obtain the transformation function G(z)
z
G( z ) = ( L - 1) ò pz (t )dt = s
0
• Mapping from s to z
z = G -1 (s)
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 42
Histogram Matching: Discrete Cases
• Obtain pr(rj) from the input image and then obtain the values of sk,
round the value to the integer range [0, L-1].
k
( L - 1) k
sk = T (rk ) = ( L - 1)å pr (rj ) = å nj
j =0 MN j =0
• Use the specified PDF and obtain the transformation function
G(zq), round the value to the integer range [0, L-1].
q
G ( zq ) = ( L - 1)å pz ( zi ) = sk
i =0
• Mapping from sk to zq
-1
zq = G (sk )
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 43
HISTOGRAM MATCHING (SPECIFICATION)
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 44
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.
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 45
Example: Histogram Matching
Obtain the histogram-equalized values,
s0 = 1, s1 = 3, s2 = 5, s3 = 6, s4 = 7,
s5 = 7, s6 = 7, s7 = 7.
Compute all the values of the transformation function G:
Fractional Values are rounded
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 46
Example: Histogram Matching
rk ® zq
s0 = 1, s1 = 3, s2 = 5, s3 = 6, s4 = 7,
s5 = 7, s6 = 7, s7 = 7. 0®3
1® 4
rk
2®5
0
1 3®6
2 4®7
3
5®7
4
5 6®7
6 7®7
7
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 47
Example: Histogram Matching
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 48
Example: Histogram Matching
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 49
Example: Histogram Matching
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 50
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 51
Example: Histogram Matching
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 52
Reference
• Read 3rd Chapter Digital image processing
24/11/24 13:35:25 Presented by Dr. Ali HAMİTOĞLU 53
Thank You J
24/11/24 13:35:36 Presented by Dr. Ali HAMİTOĞLU 54