Histogram
Histogram
Histogram
(also referred to as filters, kernels, templates, or windows). Basically, a mask is a small (say,
3*3) 2-D array, such as the one shown in Fig. 2.1, in which the values of the mask coefficients
determine the nature of the process, such as image sharpening.
The histogram of a digital image with gray levels in the range [0, L-1] is a discrete function h(rk)
= (nk), where rk is the kth gray level and nk is the number of pixels in the image having gray level
rk. It is common practice to normalize a histogram by dividing each of its values by the total
number of pixels in the image, denoted by n. Thus, a normalized histogram is given by
for k=0,1,…… .,L-1. Loosely speaking, p(rk) gives an estimate of the probability of occurrence
of gray level rk. Note that the sum of all components of a normalized histogram is equal to 1.
Histograms are the basis for numerous spatial domain processing techniques.Histogram
manipulation can be used effectively for image enhancement. Histograms are simple to calculate
in software and also lend themselves to economic hardware implementations, thus making them
a popular tool for real-time image processing.
The vertical axis corresponds to values of h(rk) = nk or p(rk) = nk/n if the values are
normalized.Thus, as indicated previously, these histogram plots are simply plots of h(r k) = nk
versus rk or p(rk) = nk/n versus rk.
74
Digital Image Processing Question & Answers
Fig.3 Four basic image types: dark, light, low contrast, high contrast, and their
corresponding histograms.
We note in the dark image that the components of the histogram are concentrated on the low
(dark) side of the gray scale. Similarly, the components of the histogram of the bright image are
biased toward the high side of the gray scale.An image with low contrast has a histogram that
will be narrow and will be centered toward the middle of the gray scale. For a monochrome
image this implies a dull,washed-out gray look. Finally,we see that the components of the
histogram in the high-contrast image cover a broad range of the gray scale and, further, that the
distribution of pixels is not too far from uniform,with very few vertical lines being much higher
than the others. Intuitively, it is reasonable to conclude that an image whose pixels tend to
occupy the entire range of possible gray levels and, in addition, tend to be distributed
uniformly,will have an appearance of high contrast and will exhibit a large variety of gray tones.
75
Digital Image Processing Question & Answers
The net effect will be an image that shows a great deal of gray-level detail and has high dynamic
76
Digital Image Processing Question & Answers
range. It will be shown shortly that it is possible to develop a transformation function that can
automatically achieve this effect, based only on information available in the histogram of the
input image.
Histogram Equalization:
Consider for a moment continuous functions, and let the variable r represent the gray levels of
the image to be enhanced. We assume that r has been normalized to the interval [0, 1], with r=0
representing black and r=1 representing white. Later, we consider a discrete formulation and
allow pixel values to be in the interval [0, L-1]. For any r satisfying the aforementioned
conditions, we focus attention on transformations of the form
that produce a level s for every pixel value r in the original image. For reasons that will become
obvious shortly, we assume that the transformation function T(r) satisfies the following
conditions:
The requirement in (a) that T(r) be single valued is needed to guarantee that the inverse
transformation will exist, and the monotonicity condition preserves the increasing order from
black to white in the output image.A transformation function that is not monotonically increasing
could result in at least a section of the intensity range being inverted, thus producing some
inverted gray levels in the output image. Finally, condition (b) guarantees that the output gray
levels will be in the same range as the input levels. Figure 4.1 gives an example of a
transformation function that satisfies these two conditions.The inverse transformation from s
back to r is denoted
It can be shown by example that even if T(r) satisfies conditions (a) and (b), it is possible that the
corresponding inverse T-1 (s) may fail to be single valued.
77
Digital Image Processing Question & Answers
Fig.4.1 A gray-level transformation function that is both single valued and monotonically
increasing.
The gray levels in an image may be viewed as random variables in the interval [0, 1].One of the
most fundamental descriptors of a random variable is its probability density function (PDF).Let
pr(r) and ps(s) denote the probability density functions of random variables r and s,
respectively,where the subscripts on p are used to denote that p r and ps are different functions.A
basic result from an elementary probability theory is that, if p r(r) and T(r) are known and T -1 (s)
satisfies condition (a), then the probability density function p s(s) of the transformed variable s
can be obtained using a rather simple formula:
Thus, the probability density function of the transformed variable, s, is determined by the gray-
level PDF of the input image and by the chosen transformation function. A transformation
function of particular importance in image processing has the form
where w is a dummy variable of integration.The right side of Eq. above is recognized as the
cumulative distribution function (CDF) of random variable r. Since probability density functions
are always positive, and recalling that the integral of a function is the area under the function, it
follows that this transformation function is single valued and monotonically increasing, and,
therefore, satisfies condition (a). Similarly, the integral of a probability density function for
variables in the range [0, 1] also is in the range [0, 1], so condition (b) is satisfied as well.
78
Digital Image Processing Question & Answers
Given transformation function T(r),we find ps(s) by applying Eq. We know from basic calculus
(Leibniz’s rule) that the derivative of a definite integral with respect to its upper limit is simply
the integrand evaluated at that limit. In other words,
Substituting this result for dr/ds, and keeping in mind that all probability values are positive,
yields
Because ps(s) is a probability density function, it follows that it must be zero outside the interval
[0, 1] in this case because its integral over all values of s must equal 1.We recognize the form of
ps(s) as a uniform probability density function. Simply stated, we have demonstrated that
performing the transformation function yields a random variable s characterized by a uniform
probability density function. It is important to note from Eq. discussed above that T(r) depends
on pr(r), but, as indicated by Eq. after it, the resulting ps(s) always is uniform, independent of the
form of pr(r). For discrete values we deal with probabilities and summations instead of
probability density functions and integrals. The probability of occurrence of gray level r in an
image is approximated by
where, as noted at the beginning of this section, n is the total number of pixels in the image, nk is
the number of pixels that have gray level rk, and L is the total number of possible gray levels in
the image.The discrete version of the transformation function given in Eq. is
79
Digital Image Processing Question & Answers
Thus, a processed (output) image is obtained by mapping each pixel with level r k in the input
image into a corresponding pixel with level sk in the output image. As indicated earlier, a plot of
pr (rk) versus rk is called a histogram. The transformation (mapping) is called histogram
equalization or histogram linearization. It is not difficult to show that the transformation in Eq.
satisfies conditions (a) and (b) stated previously. Unlike its continuos counterpart, it cannot be
proved in general that this discrete transformation will produce the discrete equivalent of a
uniform probability density function, which would be a uniform histogram.
Fig.4.2 (a) Images from Fig.3 (b) Results of histogram equalization. (c) Corresponding
histograms.
80
Digital Image Processing Question & Answers
Let us return for a moment to continuous gray levels r and z (considered continuous random
variables), and let pr(r) and pz(z) denote their corresponding continuos probability density
functions. In this notation, r and z denote the gray levels of the input and output (processed)
images, respectively.We can estimate pr(r) from the given input image, while pz(z) is the
specified probability density function that we wish the output image to have.
81
Digital Image Processing Question & Answers
where t is a dummy variable of integration. It then follows from these two equations that
G(z)=T(r) and, therefore, that z must satisfy the condition
The transformation T(r) can be obtained once p r(r) has been estimated from the input image.
Similarly, the transformation function G(z) can be obtained because p z(z) is given. Assuming
that G-1 exists and that it satisfies conditions (a) and (b) in the histogram equalization process,
the above three equations show that an image with a specified probability density function can be
obtained from an input image by using the following procedure:
(4) Obtain the output image by applying above Eq. to all the pixels in the input image.
The result of this procedure will be an image whose gray levels, z, have the specified probability
density function pz(z). Although the procedure just described is straightforward in principle, it is
seldom possible in practice to obtain analytical expressions for T(r) and for G -1. Fortunately, this
problem is simplified considerably in the case of discrete values.The price we pay is the same as
in histogram equalization,where only an approximation to the desired histogram is achievable. In
spite of this, however, some very useful results can be obtained even with crude approximations.
where n is the total number of pixels in the image, nj is the number of pixels with gray level rj,
and L is the number of discrete gray levels. Similarly, the discrete formulation is obtained from
the given histogram pz (zi), i=0, 1, 2,……, L-1, and has the form
82
Digital Image Processing Question & Answers
As in the continuos case, we are seeking values of z that satisfy this equation.The variable vk was
added here for clarity in the discussion that follows. Finally, the discrete version of the above
Eqn. is given by
Or
Implementation:
We start by noting the following: (1) Each set of gray levels {r j} , {sj}, and {zj}, j=0, 1, 2, p , L-
1, is a one-dimensional array of dimension L X 1. (2) All mappings from r to s and from s to z
are simple table lookups between a given pixel value and these arrays. (3) Each of the elements
of these arrays, for example, sk, contains two important pieces of information: The subscript k
denotes the location of the element in the array, and s denotes the value at that location. (4) We
need to be concerned only with integer pixel values. For example, in the case of an 8-bit image,
L=256 and the elements of each of the arrays just mentioned are integers between 0 and 255.This
implies that we now work with gray level values in the interval [0, L-1] instead of the normalized
interval [0, 1] that we used before to simplify the development of histogram processing
techniques.
In order to see how histogram matching actually can be implemented, consider Fig. 5(a),
ignoring for a moment the connection shown between this figure and Fig. 5(c). Figure 5(a)
shows a hypothetical discrete transformation function s=T(r) obtained from a given image. The
first gray level in the image, r1 , maps to s1 ; the second gray level, r2 , maps to s2 ; the kth level rk
maps to sk; and so on (the important point here is the ordered correspondence between these
values). Each value sj in the array is precomputed, so the process of mapping simply uses the
actual value of a pixel as an index in an array to determine the corresponding value of s.This
process is particularly easy because we are dealing with integers. For example, the s mapping for
an 8-bit pixel with value 127 would be found in the 128th position in array {s j} (recall that we
start at 0) out of the possible 256 positions. If we stopped here and mapped the value of each
pixel of an input image by the
83
Digital Image Processing Question & Answers
Fig.5. (a) Graphical interpretation of mapping from r k to sk via T(r). (b) Mapping of zq to
its corresponding value vq via G(z) (c) Inverse mapping from sk to its corresponding value
of zk.
method just described, the output would be a histogram-equalized image. In order to implement
histogram matching we have to go one step further. Figure 5(b) is a hypothetical transformation
function G obtained from a given histogram pz(z). For any z q , this transformation function yields
a corresponding value vq. This mapping is shown by the arrows in Fig. 5(b). Conversely, given
any value vq, we would find the corresponding value z q from G-1. In terms of the figure, all this
means graphically is that we would reverse the direction of the arrows to map v q into its
corresponding zq. However, we know from the definition that v=s for corresponding subscripts,
so we can use exactly this process to find the z k corresponding to any value sk that we computed
previously from the equation sk = T(rk) .This idea is shown in Fig.5(c).
84
Digital Image Processing Question & Answers
Since we really do not have the z’s (recall that finding these values is precisely the objective of
histogram matching),we must resort to some sort of iterative scheme to find z from s.The fact
that we are dealing with integers makes this a particularly simple process. Basically, because vk =
sk, we have that the z’s for which we are looking must satisfy the equation G(z k)=sk, or (G(zk)-
sk)=0. Thus, all we have to do to find the value of z k corresponding to sk is to iterate on values of
z such that this equation is satisfied for k=0,1,2,…...., L-1. We do not have to find the inverse of
G because we are going to iterate on z. Since we are dealing with integers, the closest we can get
to satisfying the equation (G(zk)-sk)=0 is to let zk= for each value of k, where is the
smallest integer in the interval [0, L-1] such that
Given a value sk, all this means conceptually in terms of Fig. 5(c) is that we would start with and
increase it in integer steps until Eq is satisfied, at which point we let repeating this process for
all values of k would yield all the required mappings from s to z, which constitutes the
implementation of Eq. In practice, we would not have to start with each time because the values
of sk are known to increase monotonically. Thus, for k=k+1, we would start with and
increment in integer values from there.
Local Enhancement:
The histogram processing methods discussed in the previous two sections are global, in the sense
that pixels are modified by a transformation function based on the gray-level content of an entire
image. Although this global approach is suitable for overall enhancement, there are cases in
which it is necessary to enhance details over small areas in an image. The number of pixels in
these areas may have negligible influence on the computation of a global transformation whose
shape does not necessarily guarantee the desired local enhancement. The solution is to devise
transformation functions based on the gray-level distribution—or other properties—in the
neighborhood of every pixel in the image.