Image Enhancement: Histogram Based Methods 1 1
Image Enhancement: Histogram Based Methods
11
What is the histogram of a digital image?
The histogram of a digital image with gray values 110 ,,, Lrrr i h di f iis the discrete function
nrp kk )(
nnk: Number of pixels with gray value rk
n: total Number of pixels in the image
The function p(rk) represents the fraction of the total numberThe function p(rk) represents the fraction of the total number of pixels with gray value rk.
22
Histogram provides a global description of the appearance ofHistogram provides a global description of the appearance of
the image.
If we consider the gray values in the image as realizations of a
random variable R, with some probability density, histogram
provides an approximation to this probability density In otherprovides an approximation to this probability density. In other
words,
)()Pr( kk rprR
33
Some Typical Histogramsyp gThe shape of a histogram provides useful information for
t t h tcontrast enhancement.
Dark image
44
Bright imageBright image
Low contrast image
55
High contrast image
Wh t i th d fi iti f t t f i ?What is the definition of contrast for an image?
If MAX and MIN are the gray values of an image contrast couldIf MAX and MIN are the gray values of an image, contrast could be defined as
contrast = (MAX MIN)/(MAX + MIN) 6contrast = (MAX - MIN)/(MAX + MIN) 6
Histogram EqualizationHistogram EqualizationWhat is the histogram equalization?
he histogram equalization is an approach to enhance a given image. The approach is to design a transformation T(.) such that th l i th t t i if l di t ib t d i [0 1]
Let us assume for the moment that the input image to be
the gray values in the output is uniformly distributed in [0, 1].
enhanced has continuous gray values, with r = 0 representing
bl k d 1 ti hitblack and r = 1 representing white.
We need to design a gray value transformation s = T(r), based
on the histogram of the input image, which will enhance the
image 7image. 7
As before, we assume that:
(1) T(r) is a monotonically increasing function for 0 r 1 (preserves order from black to white).
(2) T(r) maps [0,1] into [0,1] (preserves the range of allowed
Gray values)Gray values).
88
Let us denote the inverse transformation by r T -1(s) . Wey ( )
assume that the inverse transformation also satisfies the above
t dititwo conditions.
We consider the gray values in the input image and outputg y p g p
image as random variables in the interval [0, 1].
Let pin(r) and pout(s) denote the probability density of the
Gray values in the input and output images.
99
If pin(r) and T(r) are known, and r T -1(s) satisfies pin( ) ( ) ( )condition 1, we can write (result from probability theory):
dr
)(1
)()(sTr
inout dsdrrpsp
One way to enhance the image is to design a transformation
T(.) such that the gray values in the output is uniformly( ) g y p y
distributed in [0, 1], i.e. pout (s) 1, 0 s 1
In terms of histograms, the output image will have all
gray values in “equal proportion”gray values in equal proportion .
This technique is called histogram equalization 10This technique is called histogram equalization. 10
Equalization
1111
Next we derive the gray values in the output is uniformly
C id h f i
g y p ydistributed in [0, 1].
Consider the transformation
10)()( rdwwprTsr
in 10)()( ,0 rdwwprTs in
Note that this is the cumulative distribution function (CDF) of pin (r) and satisfies the previous two conditions.
From the previous equation and using the fundamentalFrom the previous equation and using the fundamental
theorem of calculus,
)(rpdrds
in1)()(
dsdrrpsp inout
1212
Therefore, the output histogram is given byp g g y
10,11)(
1)()( )(1
srpsp sTrinout ,
)()()( )(
)(1
rp
pp sTrsTrin
inout
The output probability density function is uniform, p p y y ,regardless of the input.
Thus, using a transformation function equal to the CDF of input gray values r, we can obtain an image with uniform gray al esvalues.
This usually results in an enhanced image, with an increase s usua y esu ts a e a ced age, w t a c easein the dynamic range of pixel values.
1313
How to implement histogram equalization?
Step 1:For images with discrete gray values, compute:
p g q
nnrp k
kin )( 10 kr 10 Lk
L: Total number of gray levelsnk: Number of pixels with gray value rkk p g y k
n: Total number of pixels in the image
Step 2: Based on CDF, compute the discrete version of the previous transformation :
k
jjinkk rprTs
0)()( 10 Lk
1414
Example:
Consider an 8-level 64 x 64 image with gray values (0, 1, …,
7). The normalized gray values are (0, 1/7, 2/7, …, 1). The7). The normalized gray values are (0, 1/7, 2/7, …, 1). The
normalized histogram is given below:
NB: The gray values in output are also (0 1/7 2/7 1) 15NB: The gray values in output are also (0, 1/7, 2/7, …, 1). 15
# pixels Fraction of # pixels
G lGray value Normalized gray value
1616
Applying the transformation,
k
jjinkk rprTs
0)()( we have
j 0
1717
1818
Notice that there are only five distinct gray levels --- (1/7, 3/7,y g y (
5/7, 6/7, 1) in the output image. We will relabel them as (s0,
)s1, …, s4 ).
With this transformation the output image will haveWith this transformation, the output image will have
histogram
1919
Histogram of output image
# pixels
Gray values
Note that the histogram of output image is only approximately, and not exactly, uniform. This should not be surprising, since there i lt th t l i if it i th di t 20is no result that claims uniformity in the discrete case. 20
Example Original image and its histogram
2121
Histogram equalized image and its histogram
2222
Comments:
Histogram equalization may not always produce desirable
results, particularly if the given histogram is very narrow. It, p y g g y
can produce false edges and regions. It can also increase
i “ i i ” d “ hi ”image “graininess” and “patchiness.”
2323
2424
Histogram SpecificationHistogram Specification(Histogram Matching)
Histogram equalization yields an image whose pixels are (in
th ) if l di t ib t d ll l ltheory) uniformly distributed among all gray levels.
Sometimes, this may not be desirable. Instead, we may want a
transformation that yields an output image with a pre specifiedtransformation that yields an output image with a pre-specified
histogram. This technique is called histogram specification.
2525
Given InformationGiven Information
(1) Input image from which we can compute its histogram .
(2) Desired histogram.
Goal
Derive a point operation H(r) that maps the input image intoDerive a point operation, H(r), that maps the input image into an output image that has the user-specified histogram.
Again, we will assume, for the moment, continuous-gray valuesvalues.
2626
Approach of derivation
z=H(r) = G-1(v=s=T(r))
Input image
Uniform image
Output imageT( ) G( )image image images=T(r) v=G(z)
Given Gaussian distribution
Fi d ti ti l di t ib ti ?Find negative exponential distribution?
2727
Suppose, the input image has probability density in p(r) . WeSuppose, the input image has probability density in p(r) . We
want to find a transformation z H (r) , such that the probability density of the new image obtained by this transformation is p (z)density of the new image obtained by this transformation is pout(z) , which is not necessarily uniform.
First apply the transformation
10)()( rdwwprTsr
i(*)
This gives an image with a uniform probability density.
10)()( ,0 rdwwprTs in
( )
If the desired output image were available, then the following
transformation would generate an image with uniform density:
10)()( zdwwpzGVz
(**) 2810)()( ,0 zdwwpzGV out (**) 28
From the gray values we can obtain the gray values z byg y g y y
using the inverse transformation, z G-1(v)
If instead of using the gray values obtained from (**), we
use the gray values s obtained from (*) above (both are
uniformly distributed ! ), then the point transformation
Z H( ) G 1[ T( )]
will generate an image with the specified density out p(z) ,
Z=H(r)= G-1[ v=s =T(r)]
will generate an image with the specified density out p(z) ,
from an input image with density in p(r) !
2929
For discrete gray levels, we haveg y
k
jinkk rprTs )()( 10 Lkj 0
k
k
jjoutkk szpzGv
0)()( 10 Lk
j0
If the transformation zk G(zk) is one-to-one, the inverse
f i G 1 ( ) b il d i d itransformation sk G-1 (sk) , can be easily determined, since
we are dealing with a small set of discrete gray values.
In practice, this is not usually the case (i.e., ) zk G(zk) is not ) d i l h h ione-to-one) and we assign gray values to match the given
histogram, as closely as possible.3030
Algorithm for histogram specification: g g p
(1) Equalize input image to get an image with uniform gray values using the discrete equation:gray values, using the discrete equation:
k
jinkk rprTs0
)()( 10 Lkj 0
(2) Based on desired histogram to get an image with if l i th di t tiuniform gray values, using the discrete equation:
k
k
joutkk szpzGv )()( 10 Lkkj
joutkk p0
)()(
(3) )]([)( 11 rTGzv=sGz )]([)( rTGzv sGz
3131
Example:
Consider an 8-level 64 x 64 previous image.
# pixels
Gray value
3232
It is desired to transform this image into a new image, using aIt is desired to transform this image into a new image, using a transformation Z=H(r)= G-1[T(r)], with histogram as specified below:
# pixels# pixels
Gray values
3333
The transformation T(r) was obtained earlier (reproduced( ) ( p
below):
N t th t f ti G b fNow we compute the transformation G as before.
3434
3535
Computer z=G-1 (s)Notice that G is not invertible.
G-1(0) = ?G-1(1/7) = 3/7G-1(2/7) = 4/7
G-1(4/7) = ?G-1(3/7) = ?G (4/7) ?G-1(5/7) = 5/7G-1(6/7) = 6/7G 1(6/7) = 6/7G-1(1) = 1
3636
Combining the two transformation T and G-1 , compute H( ) G 1[ T( )]z=H(r)= G-1[v=s=T(r)]
r =0z=G-1(v=s=T(r))= z=G-1(1/7)=3/7From column 2
r =2/7z=G-1(v=s=T(r))= z=G-1(5/7)=5/7
r =3/7z=G-1(v=s=T(r))= z=G-1(6/7)=6/7 37r 3/7z G 1(v s T(r)) z G 1(6/7) 6/7
r =1/7z=G-1(v=s=T(r))= z=G-1(3/7)=?
37
r =4/7z=G-1(v=s=T(r))= z=G-1(6/7)=6/7
r =5/7z=G-1(v=s=T(r))= z=G-1(1)=1
r =6/7z=G-1(v=s=T(r))= z=G-1(1)=16/7 G (v s ( )) G ( )
r =1z=G-1(v=s=T(r))= z=G-1(1)=1
/ ( ( )) ( / ) / / 38r =1/7z=G-1(v=s=T(r))= z=G-1(3/7)= ? 3/7 or 4/7 38
Applying the transformation H to the original image yields an pp y g g g yimage with histogram as below:
Again, the actual histogram of the output image does not exactly but only approximately matches with the specified histogram. This is because we are dealing with discrete histograms.
3939
Original image and its histogramg g g
40
Histogram specified image and its histogram
40
Desired histogram
4141