Histogram processing
What will we learn?
What is the histogram of an image?
How can the histogram of an image be computed?
How much information does the histogram provide about
the image?
What is histogram equalization and what happens to an
image whose histogram is equalized?
How can the histogram be modified through direct
histogram specification and what happens to an image
when we do it?
What other histogram modification techniques can be
applied to digital images and what is the result of applying
such techniques?
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
What is a histogram?
The histogram of a monochrome image is a graphical
representation of the frequency of occurrence of each
gray level in the image.
The data structure that stores the frequency values is a
1D array of numerical values, h, whose individual
elements store the number (or percentage) of image
pixels that correspond to each possible gray level.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram and Normalized Histogram:
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram example
Ex.: histogram for a hypothetical image containing
128×128 pixels and 8 gray levels.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
4 Ways of Plotting Histograms in Matlab:
I = imread('cameraman.tif');
h = imhist(I);
subplot(221);
imhist(I);
subplot(222);
bar([1:10:256],h(1:10:256));
subplot(223);
stem([1:10:256],h(1:10:256),'fill');
subplot(224);
plot(h);
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
4 Ways of Plotting Histograms in Matlab:
1400
1000
1200
800
1000
600
800
400 600
400
200
200
0
0
0 50 100 150 200 250 -50 0 50 100 150 200 250 300
1400 2000
1200
1500
1000
800
1000
600
400
500
200
0 0
0 50 100 150 200 250 300 0 50 100 150 200 250 300
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram of a Low-Contrast Image:
(Examp.)
In MATLAB: imhist
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram of a High-Contrast Image:
(Examp.)
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram of a Dark Image:
(Examp.)
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram of a Bright Image:
(Examp.)
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Images with Their Histograms:
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Interpreting image histograms
Histograms have become a popular tool for conveying
image statistics and helping determine certain
problems in an image.
A histogram carries significant qualitative and
quantitative information about the corresponding
image (e.g., minimum, average, and maximum gray level
values, dominance of bright or dark pixels, etc.).
A histogram is not enough to draw qualitative
conclusions about the overall quality of the image,
presence or absence of noise, etc.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Interpreting image histograms
Although a histogram provides the frequency distribution
of gray levels in an image, it tells us nothing about the
spatial distribution of the pixels whose gray levels are
represented in the histogram.
Histograms can be used whenever a statistical
representation of the gray level distribution in an image is
desired.
Histograms can also be used to enhance or modify the
characteristics of an image, particularly its contrast.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Different Images With Same Histogram:
When calculating an image histogram, the actual position of the
pixels is not used.
1. Many images have the same histogram, and
2. An image cannot be reconstructed from its histogram.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Draw histogram of any image
without using imhist function.
Hints:
data=[1 3 5 7 4 8 0 1 3];
histArray =zeros(1,10);
x=0:1:9;
Forloop to count how many times particular value appears
for n=1:length(data)
histArray(1,data(n)+1)=histArray(1,data(n)+1)+1;
% every time particular value, add 1 into to corresponding bin
end
bar(histArray)
Courtesy mathworks.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram Equalization:
o Histogram equalization is a technique by which the
gray-level distribution of an image is changed in such a
way as to obtain a uniform (flat) resulting
histogram, in which the percentage of pixels of every
gray level is the same.
o To perform histogram equalization, it is necessary to
use an auxiliary function, called transformation
function, T(r).
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Transformation Function:
o Such transformation function must satisfy two criteria:
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
CDF:
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram example
Ex.: histogram for a hypothetical image containing
128×128 pixels and 8 gray levels.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram Equalization: ( Examp.)
𝒓𝒌 𝒏𝒌 𝑷(𝒓𝒌 ) 𝒔𝒌 𝒏𝒌 𝑷(𝒔𝒌 ) 𝒓𝒌 → 𝒔𝒌
0 1120 0.068 0.0687→ 0 1120 0.068
0→0
1 3214 0.196 0.2647→ 2 3214 0.196
1→2
2 4850 0.296 0.5607→ 4 4850 0.296
2→4
3 3425 0.209 0.7697→ 5 3425 0.209
3→5
4 1995 0.122 0.8917→ 6 1995 0.122
784 + 541 0.048+
4→6
5 784 0.048 0.9397→ 7
6 541 0.033 0.9727→ 7
+ 455 = 0.033+
0.028= 5→7
7 455 0.028 1.007→ 7
1780 0.109 6→7
TOTAL 16,384 1.0 16,384 1.0
7→7
1 and 3 are missing in the 𝑠𝑘 column and hence in the o/p histogram.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram equalization
Keeping in mind that CDF acts as a T/F function:
o Pixels with values 5/6/7 which originally
have least probabilities; and hence
corresponding portion in CDF is flat.
o Hence these 3 values are mapped to 7 Tall
corresponding Bar in the output histogram.
o Conversely, where CDF curve is steep, some
values are missing in the o/p histogram e.g.,
there is no 3 in the o/p histogram.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram equalization
In MATLAB: histeq
Examp.:
I = imread('sydney_low_contrast.png');
I = im2double(I);
J = histeq(I);
figure, subplot(2,2,1), imshow(I), ...
subplot(2,2,2), imshow(J), ...
subplot(2,2,3), imhist(I), ylim('auto'),...
subplot(2,2,4), imhist(J), ylim('auto')
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram equalization
Examp.:
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Global vs. local histogram equalization
In MATLAB: histeq and adapthisteq
Examp.:
I = imread('coins.png');
figure, subplot(1,2,1), imshow(I), ...
subplot(1,2,2), imhist(I), ylim('auto')
J = histeq(I);
figure, subplot(1,2,1), imshow(J), ...
subplot(1,2,2), imhist(J), ylim('auto')
K = adapthisteq(I);
figure, subplot(1,2,1), imshow(K), ...
subplot(1,2,2), imhist(K), ylim('auto')
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Global vs. local histogram equalization
Examp.:
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Direct histogram specification
STEPS:
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Direct histogram specification
Explanation of STEPS:
Step-1: 𝒓𝒌 → 𝒔𝒌 (mapping, Same as in Histogram
Equalization)
Step-2: 𝒛𝒌 → 𝒗𝒌 (Similar to Step-1)
Step-3: 𝒔𝒌 → 𝒛𝒌 (Using G values such that 𝐺(𝑧𝑘 ) is
closest to 𝑠𝑘 )
Arbitration: When 𝑠 → 𝑧 mapping is not unique, choose
the smallest value among the contenders for breaking ties.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Direct histogram specification
Examp.:
Original Desired
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Direct histogram specification
Example 9.6:
Step-1: 𝒓𝒌 → 𝒔𝒌
This step is same as Histogram Equalization Process
and
hence
can be copied from previous Examp.
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram Equalization: ( Prev. Examp.)
Step-1: 𝒓𝒌 → 𝒔𝒌
𝒓𝒌 𝒏𝒌 𝑷(𝒓𝒌 ) 𝒔𝒌 𝒏𝒌 𝑷(𝒔𝒌 )
𝒓𝒌 → 𝒔𝒌
0 1120 0.068 0.0687→ 0 1120 0.068
0→0
1 3214 0.196 0.2647→ 2 3214 0.196
1→2
2 4850 0.296 0.5607→ 4 4850 0.296
3 3425 0.209 0.7697→ 5 3425 0.209
2→4
4 1995 0.122 0.8917→ 6 1995 0.122
3→5
5 784 0.048 0.9397→ 7 784 + 541 0.048+ 4→6
5→7
+ 455 = 0.033+
0.028=
6 541 0.033 0.9727→ 7
7 455 0.028 1.007→ 7
1780 0.109 6→7
TOTAL 16,384 1.0 16,384 1.0 7→7
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Direct histogram specification
Step-2: 𝒛𝒌 → 𝒗𝒌 (Similar to Step-1)
𝒛𝒌 𝒏𝒌 𝑷(𝒛𝒌 ) 𝒗𝒌 = 𝑮(𝒛𝒒 ) 𝒏𝒌 𝑷(𝒗𝒌 )
𝒛𝒌 → 𝒗𝒌
0 0 0.0 0.07→ 0
0→0
1 0 0.0 0.07→ 0 0 0.0 1→0
2 0 0.0 0.07→ 0
2→0
3 1638 0.1 0.17→ 1 1638 0.1
4 3277 0.2 0.37→ 2 3277 0.2
3→1
5 6554 0.4 0.77→ 5 6554 0.4
4→2
6 3277 0.2 0.97→ 6 3277 0.2
5→5
7 1638 0.1 1.007→ 7 1638 0.1 6→6
TOTAL 16,384 1.0 16,384 1.0
7→7
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Direct histogram specification
Step-3: 𝒔𝒌 → 𝒛𝒌 [Using G values such that 𝐺(𝑧 ) is closest to 𝑠 ]
𝑘 𝑘
Step-1: 𝒓𝒌 → 𝒔𝒌 Step-2: 𝒛𝒌 → 𝒗𝒌 Step-3: 𝒔𝒌 → 𝒛𝒒
𝒓𝒌 𝒏𝒌 𝑷(𝒓𝒌 ) 𝒔𝒌 𝒏𝒌 𝑷(𝒔𝒌 ) 𝒛𝒌 𝒏𝒌 𝑷(𝒛𝒌 ) 𝒗𝒌 = 𝑮(𝒛𝒌 ) 𝒛𝒌 ෩ (𝒛𝒒 )
𝑷
0 1120 0.068 0.0687→ 0 1120 0.068 0 0 0.0 0.07→ 0 0 0.068
1 3214 0.196 0.2647→ 2 3214 0.196 1 0 0.0 0.07→ 0 4 0.196
2 4850 0.296 0.5607→ 4 4850 0.296 2 0 0.0 0.07→ 0 5 0.296+
0.209=
3 3425 0.209 0.7697→ 5 3425 0.209 3 1638 0.1 0.17→ 1 5 0.505
4 1995 0.122 0.8917→ 6 1995 0.122 4 3277 0.2 0.37→ 2 6 0.122
784 + 0.048+
5 784 0.048 0.9397→ 7 541 + 0.033+
5 6554 0.4 0.77→ 5 7
455 = 0.028=
6 541 0.033 0.9727→ 7 1780 0.109
6 3277 0.2 0.97→ 6 7
0.109
7 455 0.028 1.007→ 7 7 1638 0.1 1.007→ 7 7
1.0
16,384 1.0 16,384 1.0
Direct histogram specification
Step-3: 𝒔𝒌 → 𝒛𝒌 [Using G values such that 𝐺(𝑧 ) is closest to 𝑠 ]
𝑘 𝑘
𝒔𝒌 → 𝒛𝒌
0→0
2→4
4→5
5→5
6→6
7→7
7→7
7→7
Direct histogram specification
Summary
𝒓𝒌 → 𝒔𝒌 𝒛𝒌 → 𝒗𝒌 𝒔𝒌 → 𝒛𝒌
0→0 0→0 0→0
1→2 1→0 2→4
2→4 2→0 4→5
3→5 3→1 5→5
4→6 4→2 6→6
5→7 5→5 7→7
6→7 6→6 7→7
7→7 7→7 7→7
Direct histogram specification
Summary
𝒓𝒌 → 𝒔𝒌 𝒔𝒌 → 𝒛𝒌
0→0 0→0
1→2 2→4
2→4 4→5
3→5 5→5
4→6 6→6
5→7 7→7
6→7 7→7
7→7 7→7
Direct histogram specification
Summary
𝒓𝒌 → 𝒔𝒌 → 𝒛𝒌
0→0→0
1→2→4
2→4→5
3→5→5
4→6 →6
5→7 →7
6→7 →7
7→7 →7
Direct histogram specification
Example 9.6:
Original Desired Result
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Direct histogram specification
In MATLAB: histeq
I = imread('sydney_low_contrast.png');
Examp.: Id = im2double(I);
figure, imhist(Id), ylim('auto'), ...
title ('Original histogram');
des_hist = uint8(zeros(1,256));
des_hist(1:128) = linspace(256,0,128);
des_hist(129:end) = linspace(0,256,128);
x_axis = 0:255;
figure, bar(x_axis, des_hist), axis tight,
...
title('Desired histogram');
hgram = im2double(des_hist);
Jd = histeq(Id,hgram);
figure, imhist(Jd), ylim('auto'), ...
title ('Resulting histogram');
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Direct histogram specification
Examp.:
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Direct histogram specification
Interactive histogram matching tool (ihmdemo)
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Other histogram modification techniques
Histogram sliding (Examp.):
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram sliding
In MATLAB: imadd and imsubtract
Examp.:
I = imread('schonbrunn_gray_low_contrast.png');
figure, imhist(I), ylim('auto'), title ('Original histogram');
I2 = imadd(I, 50);
figure, imhist(I2), ylim('auto'), ...
title ('Sliding to the right by 50');
I3 = imsubtract(I,50);
figure, imhist(I3), ylim('auto'), ...
title ('Sliding to the left by 50');
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Other histogram modification techniques
Histogram stretching (Examp.):
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Other histogram modification techniques
Histogram shrinking (Examp.):
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
Histogram stretching and shrinking
In MATLAB: imadjust
%% Histogram stretching
I = imread('schonbrunn_gray_low_contrast.png');
figure, imhist(I), ylim('auto'), title ('Original histogram');
I2 = imadjust(I);
figure, imhist(I2), ylim('auto'), title ('After histogram stretching');
figure, subplot(1,2,1), imshow(I), subplot(1,2,2), imshow(I2)
%% Histogram shrinking
I = imread('schonbrunn_gray.png');
figure, imhist(I), ylim('auto'), title ('Original histogram');
Id = im2double(I);
Jd = imadjust(Id, [], [49/255 140/255]);
J = uint8(255.*Jd);
figure, imhist(J), ylim('auto'), title ('After histogram shrinking');
figure, subplot(1,2,1), imshow(I), subplot(1,2,2), imshow(J)
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.