You are on page 1of 61

CII4F3 / Digital Image Processing

Histogram
Dr.Eng. Ir. Wikky Fawwaz Al Maki, S.T., M.Eng., Dr. Ema Rahmawati

Intelligent Computing and Multimedia (ICM)

1 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Overview

Image Enhancement
Basic of Histogram
Histogram Calculation
Histogram Equalization

2 11/02/2020 CIG4E3 / Pengolahan Citra Digital


IMAGE ENHANCEMENT

3 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Image Enhancement

“An image manipulation process to obtain an


enhanced image for a specific purposes”

4 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Image Enhancement Approach

Frequency domain-based method


– Manipulating the frequency representation of an image
– Example: Applying FT-based transformation to an image

Spatial domain-based method


– Manipulating the image pixels directly
– Example: histogram based method

5 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Image Histogram
Histogram : gray level value
RGB  every color channel
Equation:
nk
pr (rk )  ; 0  rk  1; k  0,1,..., L  1
n

– L: number of level
– pr(rk): probability the kth level occurs
– nk: number of occurrence of kth level
– n: total number of pixels in an image
6 11/02/2020 CIG4E3 / Pengolahan Citra Digital
Histogram

77 11/02/2020
11/02/2020 CIG4E3 / Pengolahan Citra Digital
BASIC OF HISTOGRAM

8 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram : Definition
Histograms plots how many times (frequency) each  
intensity valuein image occurs

Example: 
Image (left) has 256 distinct gray levels (8 bits)
Histogram (right) shows frequency (how many times) each  gray level occurs 

9 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram : Definition
Many cameras display real time histogra
ms of scene

Helps avoid taking over‐exposed pictures

Also easier to detect types of processing p
reviously  applied to image

10 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram : Definition

E.g. K = 16, 10 pixels have 
intensity value = 2

Histograms: only statistical
information

No indication of location of pixels

11 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram : Definition
Different images 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 reconstruct image from histogram? No!

12 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram : Definition
 
A histogram for a grayscale image with intensity  values in range
would contain exactly K entries

E.g. 8‐bit grayscale image, 
Each histogram entry is defined as:
E.g: 

Formal definition

13 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Interpreting Histogram
Log scale makes low values more visible

Difference between darkest and lightest

14 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Use of Histogram
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 to alter histogram. 
Addition
Multiplication
Exp and Log
Intensity Windowing (Contrast Modification)

15 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Image Brightness

Brightness of a grayscale image is the average intensity
of all pixels in image

16 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Detecting Bad Exposure using Histograms
Exposure? Are intensity values spread (good) out or bunched up (bad)

17 11/02/2020 CIG4E3 / Pengolahan Citra Digital


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

18 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram and Image Contrast
Good Contrast?

Widely spread intensity values


+ large difference between
min and max intensity values

19 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Contrast Equation
 Many different equations for contrast exist

Examples:

Michalson’s equation for contrast

20 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Contrast Equation : Problem

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 orif min and max intensities are small

21 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histograms and Dynamic Range
Dynamic Range: Number of distinct 
pixels in image

Difficult to increase image dynamic 
range (e.g. interpolation) 

HDR (12‐
14 bits) capture typical, then down‐
sample

22 11/02/2020 CIG4E3 / Pengolahan Citra Digital


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

23 11/02/2020 CIG4E3 / Pengolahan Citra Digital


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?

24 11/02/2020 CIG4E3 / Pengolahan Citra Digital


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

Histogram after GIF conversion


Original Image

Fix? Scaling image by 50% and Interpolating


values recreates some lost colors
But GIF artifacts still visible

25 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Effect of Image Compression

Example: Effect of JPEG 
compression on line graphics

JPEG compression designed
 for color images

26 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Large Histograms: Binning
High resolution image can yield very large histogram
Example: 32‐bit image = 232= 4,294,967,296 columns
Such a large histogram impractical to display
Solution? Binning!
Combine ranges of intensity values into histogram columns

27 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Calculating Bin Size

 
Typically use equal sized bins

Bin size? 

Example: To create 256 bins from 14‐bit
 image

28 11/02/2020 CIG4E3 / Pengolahan Citra Digital


HISTOGRAM EQUALIZATION

29 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram equalization
Employing a transformation to original image histogram to obtain a
new histogram with uniform distribution (linearization)

Basic concept: transforming the probability density function to


uniform density  continuous form

Discrete form

30 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Equalization in continuous domain
 dr 
Histogram : p s ( s )   pr ( r ) 
 ds  r T 1 ( s )
r
Transforma si : s  T ( s )   pr ( w)dw ; 0  r 1
0

Uniform :
 1 
p s ( s )   pr ( r )   1 r T 1 ( s )  1 0  s 1
 pr (r )  r T 1 ( s )

31 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Equalization in continuous domain

pr(r) s=T(r) ps(s)

2 1.0 1.0

0.8

0.6
1 0.5
0.4

0.2

r r s
0 0 0
1 2 0.2 0.4 0.6 0.8 1.0 0.5 1.0

Original probability density function Transformation function Resulting uniform density

32 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Equalization in the discrete form

k nj k 0  rk  1
sk  T (rk )     pr ( r j )
j 0 n j 0 k  0,1,..., L  1

1
rk  T ( sk ) 0  sk  1

33 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Example
Image with size 64x64 pixel (8 gray level
value) has the following distribution: Histogram:

rk nk pr(rk)=nk/n 0,3

r0=0 790 0,19 0,25

probability (p r (rk))
r1=1/7 1023 0,25 0,2

r2=2/7 850 0,21 0,15

r3=3/7 656 0,16 0,1

r4=4/7 329 0,08 0,05

r5=5/7 245 0,06 0


0 1/7 2/7 3/7 4/7 5/7 6/7 1
r6=6/7 122 0,03 gray level (rk)

r7=1 81 0,02
34 11/02/2020 CIG4E3 / Pengolahan Citra Digital
Transformation function
0
s0  T (r0 )   pr (rj )  pr (r0 )  0.19
j 0
1
s1  T (r1 )   pr (rj )  pr (r0 )  pr (r1 )  0.44
j 0
2
s2  T (r2 )   pr (rj )  pr (r0 )  pr (r1 )  pr (r2 )  0.65
j 0
3 4
s3  T (r3 )   pr (rj )  0.81; s4  T (r4 )   pr (r j )  0.89
j 0 j 0
5 6
s5  T (r5 )   pr (rj )  0.95; s6  T (r6 )   pr (rj )  0.98
j 0 j 0
7
s7  T (r7 )   pr (rj )  1.00
j 0

35 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Transformation function
1,2

1
transformed value (s k)

0,8

0,6

0,4

0,2

0
0 1/7 2/7 3/7 4/7 5/7 6/7 1
gray level (rk)

36 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Transformation function
8 gray level sk is rounded to the nearest value

s0 = 0.19  1/7
s1 = 0.44  3/7
s2 = 0.65  5/7
s3 = 0.81  6/7
s4 = 0.89  6/7
s5 = 0.95  1
s6 = 0.98  1
s7 = 1.00  1

37 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Mapping
Only 5 gray level available at uniform histogram

– r0 (790 pixel)  s0 = 1/7

– r1 (1023 pixel)  s1 = 3/7

– r2 (850 pixel)  s2 = 5/7

– r3 (656 pixel), r4 (329 pixel)  s3 = 6/7

38 – r11/02/2020
5 (245 pixel),r6 (122 pixel),r7 (81 pixel)  s4 = 7/7
CIG4E3 / Pengolahan Citra Digital
Uniform distribution?

Histogram : approximation to probability density function

It is difficult to obtain a uniform distribution

39 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram table
rk nk pr(rk)=nk/n Sk Sk x 7 Normal(Sk)
r0=0 790 0,19 0,19 1,33  1 s0=1/7
r1=1/7 1023 0,25 0,44 3,08  3 s1=3/7
r2=2/7 850 0,21 0,65 4,55  5 s2=5/7
r3=3/7 656 0,16 0,81 5,67  6 s3=6/7
r4=4/7 329 0,08 0,89 6,23  6 s4=6/7
r5=5/7 245 0,06 0,95 6,65  7 s5=7/7
r6=6/7 122 0,03 0,98 6,86  7 s6=7/7
r7=1 81 0,02 1,00 7 s7=1

40 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram equalization result

41 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram equalization result 2

11/02/2020 CIG4E3 / Pengolahan Citra Digital


HISTOGRAM SEPCIFICATION

43 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram Specification
Drawback of histogram equalization : the histogram can’t be
obtained based on purposes

In fact, sometimes we need to stress a specific interval of image


gray levels  histogram specification

44 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Histogram Specification
A 64x64 pixel image with 8 gray level
has the following distribution : Image histogram:
rk nk pr(rk)=nk/n
0,3
r0=0 790 0,19
0,25
r1=1/7 1023 0,25

probability (p r (rk))
0,2
r2=2/7 850 0,21
0,15
r3=3/7 656 0,16
0,1
r4=4/7 329 0,08
0,05
r5=5/7 245 0,06
0
r6=6/7 122 0,03 0 1/7 2/7 3/7 4/7 5/7 6/7 1
r7=1 81 0,02 gray level (rk)

45 11/02/2020 CIG4E3 / Pengolahan Citra Digital


The desired histogram
zk pz(zk)
0,35
z0=0 0,00
0,30
z1=1/7 0,00

probability (p z (z k))
0,25
z2=2/7 0,00
0,20
z3=3/7 0,15 0,15
z4=4/7 0,20 0,10
z5=5/7 0,30 0,05

z6=6/7 0,20 0,00


0 1/7 2/7 3/7 4/7 5/7 6/7 1
z7=1 0,15
gray level (z k)

46 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Step 1 : Histogram equalization
After histogram equalization, we get :
0,3
rjsk nk ps(sk)
0,25
r0s0=1/7 790 0,19

probability (p s(sk))
0,2
r1s1=3/7 1023 0,25
0,15
r2s2=5/7 850 0,21
0,1
r3,r4 s3=6/7 985 0,24
0,05
r5,r6,r7 s4=7/7 448 0,11
0
0 1/7 2/7 3/7 4/7 5/7 6/7 1
gray level (sk)

47 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Step 2: Find the transformation function
k
vk  G ( z k )   p z ( z j )
j 0

v0 = G(z0) = 0,00
v1 = G(z1) = 0,00
v2 = G(z2) = 0,00
v3 = G(z3) = 0,15
v4 = G(z4) = 0,35
v5 = G(z5) = 0,65
v6 = G(z6) = 0,85
v7 = G(z7) = 1,00
48 11/02/2020 CIG4E3 / Pengolahan Citra Digital
Step 2: Find the transformation function

rk nk pr(rk)=nk/n Sk Sk x 7 Normal(Sk)
r0=0 790 0,19 0,19 1,33  1 s0=1/7
r1=1/7 1023 0,25 0,44 3,08  3 s1=3/7
r2=2/7 850 0,21 0,65 4,55  5 s2=5/7
r3=3/7 656 0,16 0,81 5,67  6 s3=6/7
r4=4/7 329 0,08 0,89 6,23  6 s4=6/7
r5=5/7 245 0,06 0,95 6,65  7 s5=7/7
r6=6/7 122 0,03 0,98 6,86  7 s6=7/7
r7=1 81 0,02 1,00 7 s7=1
49 11/02/2020 CIG4E3 / Pengolahan Citra Digital
Step 2: Find the transformation function
Perform histogram equalization to the desired histogram

zk pz(zk) Vk Vk x 7 Normal(Vk)
z0=0 0,00 0,00 0,00 v0=0
z1=1/7 0,00 0,00 0,00 v1=0
z2=2/7 0,00 0,00 0,00 v2=0
z3=3/7 0,15 0,15 1,05  1 v3=1/7
z4=4/7 0,20 0,35 2,45  2 v4=2/7
z5=5/7 0,30 0,65 4,45  4 v5=4/7
z6=6/7 0,20 0,85 5.95  6 v6=6/7
z7=1 0,15 1,00 7 v7=1
50 11/02/2020 CIG4E3 / Pengolahan Citra Digital
Graph of Transformation function
1,2

1
transformation (v k)

0,8

0,6

0,4

0,2

0
0 1/7 2/7 3/7 4/7 5/7 6/7 1
gray level (z k)

51 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Step 3: Apply inverse of G to the level of histogram
equalization

Mapping the value of sk to the nearest values of G(zk)

s0 = 1/7 ≈ 0.14  G(z3) = 0.15; z3 = 3/7


s1 = 3/7 ≈ 0.43  G(z4) = 0.35; z4 = 4/7
s2 = 5/7 ≈ 0.71  G(z5) = 0.65; z5 = 5/7
s3 = 6/7 ≈ 0.86  G(z6) = 0.85; z6 = 6/7
s4 = 1  G(z7) = 1.00; z7 = 1

52 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Step 4: Map the value of rk to zk
Notice the mapping from original histogram to histogram equalization

r0 = 0  z3 = 3/7
r1 = 1/7  z4 = 4/7
r2 = 2/7  z5 = 5/7
r3 = 3/7  z6 = 6/7
r4 = 4/7  z6 = 6/7
r5 = 5/7  z7 = 1
r6 = 6/7  z7 = 1
r7 = 1  z7 = 1

53 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Result

zk nk pz(zk)=nk/n 0,30

r0=0 0 0 0,25

probability (p z (z k))
r1=1/7 0 0 0,20

r2=2/7 0 0 0,15

0,10
r3=3/7 790 0,19
0,05
r4=4/7 1023 0,25
0,00
r5=5/7 850 0,21
0 1/7 2/7 3/7 4/7 5/7 6/7 1
r6=6/7 985 0,24 gray level (z k)

54
r7=1
11/02/2020
448 0,11 CIG4E3 / Pengolahan Citra Digital
Algorithm: 512 x 512 Image
Var x,y,i,minval,minj,j : integer; Histspec : array[0..255] of integer;
Invhist : array[0..255] of integer; Sum : real;
Begin
Hist_Equalization(Image) {equalisasi histogram}
For i:= 0 to 255 do {histogram yang dispesifikasikan telah disimpan di spec}
Sum:= 0.0;
For j:= 0 to i do Sum := sum + spec[j]
Histspec[i] = round(255 * sum)
Endfor {didapat fungsi transformasi}
for i:= 0 to 255 do {pemetaan histogram}
minval := abs(i – histspec[0]; minj := 0;
for j:= 0 to 255 do
if abs(i – histspec[j]) < minval then
minval := abs(i – histspec[j])
minj := j
endif
invhist[i]:= minj
endfor
endfor
for y:= 0 to 511 do {ubah nilai tiap pixel pada citra}
for x:= 0 to 511 do image[x,y] = invhist[image(x,y)]

55 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Example 1 : histogram specification

11/02/2020 CIG4E3 / Pengolahan Citra Digital


Example 2 : histogram specification

57 11/02/2020 CIG4E3 / Pengolahan Citra Digital


Example 3 : Histogram Specification

11/02/2020 CIG4E3 / Pengolahan Citra Digital


Method for Histogram specification

559 11/02/2020
11/02/2020 CIG4E3 / Pengolahan Citra Digital
Local enhancement
Histogram equalization and specification : global (applied to all of
the pixels)
Sometimes we need an enhancement in specific area of the image

60 11/02/2020 CIG4E3 / Pengolahan Citra Digital


THANK YOU
61 11/02/2020 CIG4E3 / Pengolahan Citra Digital

You might also like