Computer Vision
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
Also easier to detect types of processing previously
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
E.g. K = 16, 10 pixels have intensity value = 2
Histograms: only statistical information
No indication of location of pixels
DR TRIPTY SINGH 6
0 1 1 3 4
7 2 5 5 7
6 3 2 1 1
1 4 4 2 1
A 3-bit image of size 4×5 is shown below.
Compute the histogram equalized image.
Steps:
•Find the range of intensity values.
•Find the frequency of each intensity value.
•Calculate the probability density function for each frequency.
•Calculate the cumulative density function for each frequency.
•Multiply CDF with the highest intensity value possible.
•Round off the values obtained in step-5.
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.
0 2 2 Output: 4The new image
5 is as follow:
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
Half of pixels are gray, half are white
Same histogram = same statisics
Distribution of intensities could be different
Can we reconstructDRimage from histogram? No!
TRIPTY SINGH 11
Histograms
So, a histogram for a grayscale image with intensity
values in range
would contain exactly K entries
E.g. 8‐bit grayscale image, K = 28 = 256
Each histogram entry is defined as:
h(i) = number of pixels with intensity I for all 0 < i < K.
E.g: h(255) = number of pixels with intensity = 255
Formal definition
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
2. Divide by total number of pixels
1. Sum up all pixel intensities
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
Low contrast: image uses few 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
Low contrast Normal
DR contrast
TRIPTY SINGH High contrast 18
Contrast Equation?
Many different equations for contrast exist
Examples:
Michalson’s equation for contrast
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
luminances or if min and max intensities are small
DR TRIPTY SINGH 20
Histograms and Dynamic Range
Dynamic Range: Number of distinct pixels in image
High Dynamic Range Low Dynamic Range Extremely low
(64 intensities) Dynamic Range
(6 intensity
values)
Difficult to increase image dynamic range (e.g. interpolation)
HDR (12‐14 bits) capture typical,DRthen down‐sample
TRIPTY SINGH 21
High Dynamic Range Imaging
High dynamic range means very bright and very dark
parts in a single image (many distinct values)
Dynamic range in photographed scene may exceed
number of available bits to represent pixels
Solution:
Capture multiple images at different exposures
Combine them using image processing
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
Original Histogram after GIF conversion
Image
Fix? Scaling image by 50% and
Interpolating values recreates
some lost colors
But GIF artifacts still visible
DR TRIPTY SINGH 24
Effect of Image Compression
Example: Effect of JPEG compression on line graphics
JPEG compression designed for color images
Original histogram has only
2 intensities (gray and
white)
JPEG image appears dirty,
fuzzy and blurred
Its Histogram contains gray values
not in original
DR TRIPTY SINGH 25
Computing Histograms
Receives 8-bit image,
Will not change it
Create array to store
histogram computed
Get width and height of
image
Iterate through image
pixels, add each
intensity to appropriate
histogram bin
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
Such a large histogram impractical to display
Solution? Binning!
Combine ranges of intensity values into histogram columns
Number (size of set) of pixelsDR TRIPTY Pixel’s intensity is
such that
SINGH 28
between ai and ai+1
Calculating Bin Size
Typically use equal sized bins
Bin size?
Example: To create 256 bins from 14‐bit image
DR TRIPTY SINGH 29
Binned Histogram
Create array to store
histogram computed
Calculate which bin to
add pixel’s intensity
Increment corresponding
histogram
DR TRIPTY SINGH 30
Color Image Histograms
Two types:
1. Intensity histogram:
Convert color
image to gray scale
Display histogram
of gray scale
2. Individual Color
Channel Histograms:
3 histograms (R,G,B)
DR TRIPTY SINGH 31
Color Image Histograms
Both types of histograms provide useful information about
lighting, contrast, dynamic range and saturation effects
No information about the actual color distribution!
Images with totally different RGB colors can have same R, G
and B histograms
Solution to this ambiguity is the Combined Color Histogram.
More on this later
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
Last entry of Cum. Total number of
DR TRIPTY SINGH
pixels in image 33
histogram
Point Operations
Point operations changes a pixel’s intensity value according t o some
function (don’t care about pixel’s neighbor)
Also called a homogeneous operation
New pixel intensity depends on
Pixel’s previous intensity I(u,v)
Mapping function f( )
Does not depend on
Pixel’s location (u,v)
Intensities of neighboring
pixels
DR TRIPTY SINGH 34
Some Homogeneous Point Operations
Addition (Changes brightness)
Multiplication (Stretches/shrinks image contrast range)
Real‐valued functions
Quantizing pixel values
Global thresholding
Gamma correction
DR TRIPTY SINGH 35
Point Operation Pseudocode
Input: Image with pixel intensities I(u,v) defined on
[1 .. w] x [1 .. H]
Output: Image with pixel intensities I’(u,v)
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
• Implemented as imageJ method
DR TRIPTY SINGHthreshold( ) 42
Thresholding Example
DR TRIPTY SINGH 43
Thresholding and Histograms
Example with ath =
128
Thresholding splits histogram, merges halves into a0 a1
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
Log transformation of Fourier transform shows more detail
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
Old pixel value
New pixel value
Constant
Map narrow
range of dark input
values into wider
range of output values
or vice versa
(2002)
Varying γ gives aDR TRIPTY SINGH 47
Power Law Example
Original
Magnetic
Images taken from Gonzalez & Woods, Digital Image Processing
Resonance (MR) image s = r 0.6
of fractured
human spine
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
Original intensity range
If amin = 0 and amax = 255
New intensity range
DR TRIPTY SINGH 52
Effects of Automatic Contrast Adjustment
Linearly stretching
range causes gaps
in histogram
Original Result of automatic
Contrast Adjustment
DR TRIPTY SINGH 53
Modified Contrast Adjustment
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
modified image into uniform distribution
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