Lecture 6-7
Lecture 6-7
By Oge Marques Copyright © 2011 by John Wiley & Sons, Inc. All rights reserved.
What is a histogram?
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
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)
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: 𝒓𝒌 → 𝒔𝒌
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
𝒔𝒌 → 𝒛𝒌
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:
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.