10 Histogram
10 Histogram
Lecture 32:
Histograms and Point
Operations
Dr Tripty Singh
Computer Science and Enginnering
Dept. Amrita School of Computing
Histograms
Histograms plots how many times (frequency) each
intensity value in image occurs
Example:
Image (left) has 256 distinct gray levels (8 bits)
Histogram (right) shows frequency (how many times) each
gray level occurs
DR TRIPTY SINGH 2
Histograms
Many cameras display real time histograms of scene
Helps avoid taking over‐exposed pictures
applied to image
DR TRIPTY SINGH 3
Histogram equalization
DR TRIPTY SINGH 4
Histograms
Histogram Equalization is a mathematical technique to widen the dynamic
range of the histogram. Sometimes the histogram is spanned over a short
range, by equalization the span of the histogram is widened. In digital
image processing, the contrast of an image is enhanced using this very
technique.
Use of Histogram Equalization:
It is used to increase the spread of the histogram. If the histogram
represents the digital image, then by spreading the intensity values over a
large dynamic range we can improve the contrast of the image.
Algorithm:
• Find the frequency of each value represented on the horizontal axis of
the histogram i.e. intensity in the case of an image.
• Calculate the probability density function for each intensity value.
• After finding the PDF, calculate the cumulative density function for each
intensity’s frequency.
• The CDF value is in the range 0-1, so we multiply all CDF values by the
largest value of intensity i.e. 255.
• Round off the final values to integer values.
DR TRIPTY SINGH 5
Histograms
Intensity
values
DR TRIPTY SINGH 7
1. Overview of calculation:
2. Range of intensity values = [0, 1, 2, 3, 4, 5, 6, 7]
3. Frequency of values = [1, 6, 3, 2, 3, 2, 1, 2]
4. total = 20 = 4*5
5. Calculate PDF = frequency of each intensity/Total sum of all
frequencies, for each i value of intensity
6. Calculate CDF =cumulative frequency of each intensity
7. value = sum of all PDF value (<=i)
8. Multiply CDF with 7.
9. Round off the final value of intensity.
DR TRIPTY SINGH 8
Range Frequency PDF CDF 7*CDF Round-off
0 1 0.0500 0.0500 0.3500 0
0.3000
1 6 0.3500 2.4500 2
0.1500
2 3 0.5000 3.5000 4
0.1000
3 2 0.6000 4.2000 4
0.1500
4 3 0.7500 5.2500 5
0.1000
5 2 0.8500 5.9500 6
0.0500
6 1 0.9000 6.3000 6
0.1000
7 2 1.0000 7.0000 7
DR TRIPTY SINGH 9
Interpretation:
The pixel intensity in the image has
modified.
0 intensity is replaced by 0.
1 intensity is replaced by 2.
2 intensity is replaced by 4.
3 intensity is replaced by 4.
4 intensity is replaced by 5.
5 intensity is replaced by 6.
6 intensity is replaced by 6.
7 intensity is replaced by 7.
7 4 6 6 7
6 4 4 2 2
2 5 5 4 2
DR TRIPTY SINGH 10
Histograms
Differentimages can have same histogram
3 images below have same histogram
Number (size of
DRset) of pixels
TRIPTY SINGH 12
Interpreting Histograms
Log scale makes low values more visible
Difference between darkest and lightest
DR TRIPTY SINGH 13
Histograms
Histograms help detect image acquisition issues
Problems with image can be identified on histogram
Over and under exposure
Brightness
Contrast
Dynamic Range
Point operations can be used t o alter histogram. E.g
Addition
Multiplication
Exp and Log
Intensity Windowing (Contrast Modification)
DR TRIPTY SINGH 14
Image Brightness
Brightness of a grayscale image is the average
intensity of all pixels in image
DR TRIPTY SINGH 15
Detecting Bad Exposure using Histograms
Exposure? Are intensity values spread (good) out or
bunched up (bad)
Image
Histogram
Underexposed Properly
DR TRIPTY SINGH Overexposed 16
Exposed
Image Contrast
The contrast of a grayscale image indicates how easily
objects in the image can be distinguished
High contrast image: many distinct intensity values
DR TRIPTY SINGH 17
Histograms and Contrast
Good Contrast? Widely spread intensity values
+ large difference between min and max intensity values
Image
Histogram
DR TRIPTY SINGH 19
Contrast Equation?
These equations work well for simple images with 2
luminances (i.e. uniform foreground and
background)
Does not work well for complex scenes with many
DR TRIPTY SINGH 20
Histograms and Dynamic Range
Dynamic Range: Number of distinct pixels in image
DR TRIPTY SINGH 22
Detecting Image Defects using Histograms
No “best” histogram shape, depends on application
Image defects
Saturation: scene illumination values outside the sensor’s range are set to
its min or max values => results in spike at ends of histogram
Spikes and Gaps in manipulated images (not original). Why?
DR TRIPTY SINGH 23
Image Defects: Effect of Image Compression
Histograms show impact of image compression
Example: in GIF compression, dynamic range is reduced to
only few intensities (quantization)
Original
Histogram
DR TRIPTY SINGH 25
Computing Histograms
DR TRIPTY SINGH 26
ImageJ Histogram Function
ImageJ has a histogram function
( getHistogram( ) )
Prior program can be simplified if we use it
Returns histogram as an
array of integers
DR TRIPTY SINGH 27
Large Histograms: Binning
High resolution image can yield very large histogram
Example: 32‐bit image = 2 32 = 4,294,967,296 columns
Solution? Binning!
Combine ranges of intensity values into histogram columns
DR TRIPTY SINGH 29
Binned Histogram
Increment corresponding
histogram
DR TRIPTY SINGH 30
Color Image Histograms
Two types:
1. Intensity histogram:
Convert color
of gray scale
2. Individual Color
Channel Histograms:
3 histograms (R,G,B)
DR TRIPTY SINGH 31
Color Image Histograms
DR TRIPTY SINGH 32
Cumulative Histogram
Useful for certain operations (e.g. histogram equalization) later
Analogous t o the Cumulative Density Function (CDF)
Definition:
Recursive definition
Monotonically increasing
DR TRIPTY SINGH 34
Some Homogeneous Point Operations
Addition (Changes brightness)
Real‐valued functions
for v = 1 .. h
for u = 1 .. w
set I(u, v) = f (I(u,v))
DR TRIPTY SINGH 36
Non‐Homogeneous Point Operation
New pixel value depends on:
Old value + pixel’s location
(u,v)
DR TRIPTY SINGH 37
Clamping
Deals with pixel values outside displayable range
If (a > 255) a = 255;
If (a < 0) a = 0;
Function below will clamp (force) all values to fall
within range [a,b]
DR TRIPTY SINGH 38
Example: Modify Intensity and Clamp
Point operation: increase image contrast by 50%
then clamp values above 255
Increase contrast
by 50%
DR TRIPTY SINGH 39
Inverting Images
2 steps
1. Multiple intensity by ‐1
2. Add constant (e.g. amax)
to put result in range
[0,amax ]
Implemented as
ImageJ method
invert( ) Original Inverted Image
DR TRIPTY SINGH 40
Image Negatives (Inverted Images)
Image negatives useful for enhancing white
or grey detail embedded in dark regions of an
Images taken from Gonzalez & Woods, Digital Image Processing
image
Note how much clearer the tissue is in the negative
image of the mammogram below
Original Negative
s = 1.0 - r
Image Image
(2002)
DR TRIPTY SINGH 41
Thresholding
DR TRIPTY SINGH 43
Thresholding and Histograms
Example with ath =
128
DR TRIPTY SINGH 44
Basic Grey Level Transformations
3 most common gray level transformation:
Images taken from Gonzalez & Woods, Digital Image Processing
Linear
Negative/Identity
Logarithmic
Log/Inverse log
Power law
n t h power/n t h root
(2002)
DR TRIPTY SINGH 45
Logarithmic Transformations
Maps narrow range of input levels => wider
range of output values
Inverse log transformation does opposite
transformation
The general form of the log transformation is
New pixel value s = c * log(1 + r) Old pixel value
s = log(1 + r)
DR TRIPTY SINGH 46
Power Law Transformations
Power law transformations have the form
Power
s=c*r γ
Images taken from Gonzalez & Woods, Digital Image Processing
Map narrow
range of dark input
values into wider
range of output values
or vice versa
(2002)
Magnetic
Images taken from Gonzalez & Woods, Digital Image Processing
s = r 0.4
Different power
values highlight
different details
(2002)
DR TRIPTY SINGH 48
Intensity Windowing
A clamp operation, then linearly stretching image
intensities to fill possible range
To window an image in [a,b] with max intensity M
DR TRIPTY SINGH 49
Intensity Windowing Example
Contrasts
easier to see
DR TRIPTY SINGH 50
Point Operations and Histograms
Effect of some point operations easier to observe on histograms
Increasing brightness
Raising contrast
Inverting image
Point operations only shift, merge histogram entries
Operations that merge histogram bins are irreversible
Combining histogram
operation easier to
observe on histogram
DR TRIPTY SINGH 51
Automatic Contrast Adjustment
Linearly stretching
range causes gaps
in histogram
DR TRIPTY SINGH 54
Histogram Equalization
Adjust 2 different images to make their histograms
(intensity distributions) similar
Apply a point operation that changes histogram of
Histogram
Cumulative
Histogram
DR TRIPTY SINGH 55
Histogram Equalization
Spreading out the frequencies in an image (or
equalizing the image) is a simple way to improve dark
or washed out images
Can be expressed as a transformation of histogram
rk: input intensity
sk: processed intensity
k: the intensity
range (e.g 0.0 – 1.0)
processed intensity sk T (rk input intensity
)
Intensity range
DR TRIPTY(e.g
SINGH
0 – 255) 56
Images taken from Gonzalez & Woods, Digital Image Processing
(2002)
DR TRIPTY SINGH
Equalization Transformation Function
57
Equalization Transformation Functions
Different equalization function (1‐4) may be used
Images taken from Gonzalez & Woods, Digital Image Processing
(2002)
DR TRIPTY SINGH 58
Images taken from Gonzalez & Woods, Digital Image Processing
(2002)
Equalization Examples
DR TRIPTY SINGH
1
59
Images taken from Gonzalez & Woods, Digital Image Processing
(2002)
Equalization Examples
DR TRIPTY SINGH
2
60
Images taken from Gonzalez & Woods, Digital Image Processing
(2002)
Equalization Examples
DR TRIPTY SINGH
61
4
3