Image Histograms Cumulative histogram Effect of gray-level mapping on histogram Histogram equalization Histogram specification Local enhancement Color processing
Intensity Histogram 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. This histogram is a graph showing the number of pixels in an image at each different intensity value found in that image. For an 8-bit greyscale image there are 256 different possible intensities, and so the histogram will graphically display 256 numbers showing the distribution of pixels amongst those greyscale values.
Histogram Processing - How It Works
The operation is very simple. The image is scanned in a single pass and a running count of the number of pixels found at each intensity value is kept. This is then used to construct a suitable histogram. The histogram of an 8-bit image, for example can be thought of as a table with 256 entries, or ‘bins’, indexed from 0 to 255. In bin 0 we record the number of times a gray level of 0 occurs; in bin 1 we record the number of times a grey level of 1 occurs, and so on, up to bin 255. See algorithm 6.3 1. Assign zero values to all element of the
array hf ; 2. For all pixels (x,y) of the image f , increment hf [f(x,y)] by 1 .
Another way to get the histogram is to use the C code, as following: char image[rows][cols]; int histogram[256]; int row, col, i; for (i = 0; i < 256; i++) histogram[j]=0; for (row = 0; row < rows; row++) for(col = 0; col < cols; col++) histogram[(int) image[row][col]++;
Intensity Histogram
- Guidelines for Use
Histograms have many uses. One of the more common is to decide what value of threshold to use when converting a greyscale image to a binary one by thresholding. If the image is suitable for thresholding then the histogram will be bi-modal --- i.e. the pixel intensities will be clustered around two well separated values. A suitable threshold for separating these two groups will be found somewhere in between the two peaks in the histogram. If the distribution is not like this then it is unlikely that a good segmentation can be produced by thresholding. The intensity histogram for the input image is
Intensity Histogram
- Guidelines for Use
The object being viewed is dark in color and it is placed on a light background, and so the histogram exhibits a good bi-modal distribution. One peak represents the object pixels, one represents the background.
Intensity Histogram
- Guidelines for Use
The histogram is the the same but with the y-axis expanded to show more detail. It is clear that a threshold value of around 120 should segment the picture nicely as can be seen in
Intensity Histogram
- Guidelines for Use
The histogram of image is
This time there is a significant incident illumination gradient across the image, and this blurs out the histogram. The bi-modal distribution has been destroyed and it is no longer possible to select a single global threshold that will neatly segment the object from its background. Two failed thresholding segmentations are shown in
Intensity Histogram
- Guidelines for Use
The histogram is used and altered by many image enhancement operators. Two operators which are closely connected to the histogram are contrast stretching and histogram equalization They are based on the assumption that an image has to use the full intensity range to display the maximum contrast.
Intensity Histogram and Contrast stretching Contrast stretching takes an image in which the intensity values don't span the full intensity range and stretches its values linearly.
The histogram shows that most of the pixels have rather high intensity values.
Intensity Histogram and Contrast stretching Contrast stretching the image yields which has a clearly improved contrast.
The corresponding histogram is
Intensity Histogram and Contrast stretching Multiplication of gray levels by a constant gain will spread out the histogram evenly if a>1, increasing the spacing between occupied bins,or compress it if a<1, which can have the effect of merging bins
Intensity Histogram and Contrast stretching If we expand the y-axis, as was done in
We can see that now the pixel values are distributed over the entire intensity range. Due to the discrete character of the pixel values, we can't increase the number of distinct intensity values. That is the reason why the stretched histogram shows the gaps between the single values.
Intensity Histogram and Contrast stretching
The present image also has low contrast. However, if we look at its histogram, we see that the entire intensity range is used and we therefore cannot apply contrast stretching. On the other hand, the histogram also shows that most of the pixels values are clustered in a rather small area, whereas the top half of the intensity values is used by only a few pixels.
Intensity Histogram and histogram equalization The idea of histogram equalization is that the pixels should be distributed evenly over the whole intensity range, i.e. the aim is to transform the image so that the output image has a flat histogram. The image results from the histogram equalization
Intensity Histogram - Conclusions 1. consider the image intensities as random variables with a probability density function (pdf). 2. we can estimate the pdf from the empirical data given in the image itself . 3. record the frequency distribution of gray levels in an image. o for a b-bit image, you need an array of size 2b o loop through every pixel, recording the
Intensity Histogram - Conclusions 4. normalize the histogram by dividing each entry by the total number of pixels - gives an estimate for the pdf - each element of the array gives the probability of that gray level occurring at a randomly selected pixel . 5. contains global information about the image 6. discards all spatial information 7. an image has only one histogram, but many images may have the same histogram
Cumulative histogram Conclusions •Cumulative histogram - each array element gives the number of pixels with a gray-level less than or equal to the gray level corresponding to the array element -Cumulative easily constructed histogram frequencies ,from cj, arethe computed from histogram counts, hi using, j
c j =∑ hi i =0
Cumulative histogram Conclusions
- cumulative histogram has a steep slope in densely populated parts of the histogram - cumulative histogram has a gradual slope in sparsely populated parts of the histogram
Effect of gray-level mapping on histogram - Conclusions
1. adding a bias shifts the histogram 2. gain > 1 stretches the histogram (increasing contrast) 3. gain < 1 compresses histogram (reducing contrast) 4. nonlinear mapping stretches some regions and compresses others
Histogram Equalization Histogram modeling techniques (e.g. histogram equalization) provide a sophisticated method for modifying the dynamic range and contrast of an image by altering that image such that its intensity histogram has a desired shape. Unlike contrast stretching , histogram modeling operators may employ non-linear and non-monotonic transfer functions to map between pixel intensity values in the input and output images. Histogram equalization employs a monotonic, non-linear mapping which re-assigns the intensity values of pixels in the input image such that the output image contains a uniform distribution of intensities (i.e. a flat histogram). This technique is used in image comparison processes (because it is effective in detail enhancement) and in the correction of non-linear effects introduced by, say, a digitizer or display system.
Histogram Equalization
Histogram equalization involves finding a grey scale transformation function that creates an output image with a uniform histogram (or nearly so).
How do we determine this grey scale transformation function? Assume our grey levels are continuous and have been normalized to lie between 0 (black) and 1 (white).
We must find a transformation T that maps grey values r in the input imag F to grey values s = T(r) in the transformed image . It is assumed that T is single valued and monotonically increasing, and for
The inverse transformation from s to r is given by :
r = T-1 (s).
An example of such a transfer function is illustrated in the Figure
Histogram Equalization - Discrete Formulation We first need to determine the probability distribution of grey levels in the input image. where nk is the number of pixels having grey level k, and N is the total number of pixels in the image. The transformation now becomes Note that ,the index k=0,1,2,…255, and The values of sk will have to be scaled up by 255 and rounded to the nearest integer so that the output values of this transformation will range from 0 to 255. Thus the discretization and rounding of sk to the nearest integer will mean that the transformed image will not have a
Histogram Equalization - Discrete Formulation The mapping function we need is obtained simply by rescaling the cumulative histogram so that its values lie in the range 0-255. Thus, an image which is transformed using its cumulative histogram yields an output histogram which is flat . See Algorithm 6.4 p 125 Efford’s book
The original image and its histogram, and the equalized versions. Both images are quantized to 64 grey levels.
Histogram Equalization Guidelines for Use
The histogram confirms what we can see by visual inspection: this image has poor dynamic range. (Note that we can view this histogram as a description of pixel probability densities by simply scaling the vertical axis by the total number of image pixels and normalizing the horizontal axis using the number of intensity density levels (i.e. 256). However, the shape of the distribution will be the same in either case.)
Histogram Equalization Guidelines for Use In order to improve the contrast of this image, without affecting the structure (i.e. geometry) of the information contained therein, we can apply the histogram equalization operator.
Histogram Equalization Guidelines for Use Note that the histogram is not flat (as in the examples from the continuous case) but that the dynamic range and contrast have been enhanced. Note also that when equalizing images with narrow histograms and relatively few gray levels, increasing the dynamic range has the adverse effect of increasing visual grainyness. Compare this result with that produced by the linear contrast stretching operator After equalization operator
After contrast stretching
Histogram Equalization - Example
Although the contrast on the building is acceptable, the sky region is represented almost entirely by light pixels. This causes most histogram pixels to be pushed into a narrow peak in the upper graylevel region.
Histogram Equalization - Example The histogram equalization operator defines a mapping based on the cumulative histogram
Histogram Equalization - Example After histogram equalization
While histogram equalization has enhanced the contrast of the sky regions in the image, the picture now looks artificial because there is very little variety in the middle graylevel range. This occurs because the transfer function is based on the shallow slope of the cumulative histogram in the middle graylevel regions (i.e. intensity density levels 100 - 230) and causes many pixels from this region in the original image to be mapped to similar graylevels in the output image.
Histogram Equalization - Example We can improve on this if we define a mapping based on a sub-section of the image which contains a better distribution of intensity densities from the low and middle range graylevels. If we crop the image so as to isolate a region which contains more building than sky. We can then define a histogram equalization mapping for the whole image based on the cumulative histogram of this smaller region. Rather than saying that equalisation flattens a histogram, it is more accurate to say that it linearises the cumulative frequency distribution.
Histogram Equalization - Example Since the cropped image contains a more even distribution of dark and light pixels, the slope of the transfer function is steeper and smoother, and the contrast of the resulting image is more natural. This idea of defining mappings based upon particular sub-sections of the image is taken up by another class of operators which perform Local Enhancements
Histogram Equalization - Conclusions - use the cumulative histogram to generate a nonlinear gray-level mapping - cumulative histogram has a steep slope in densely populated parts of the histogram - cumulative histogram has a gradual slope in sparsely populated parts of the histogram - scale the entries based on bits per pixel and number of pixels - EqualizeOp.java - convenient because no user input is required - histogram equalization doesn't always get us the desired results
Histogram Specification Histogram equalization is limited in that it is capable of producing only one result: an image with a uniform intensity distribution. Sometimes it is desirable to be able to control the shape of the output histogram in order to highlight certain intensity levels in an image. This can be accomplished by the histogram specialization operator which maps a given intensity distribution into a desired distribution as an intermediate using a histogram equalized image stage The first step in histogram specialization, is to specify the desired output density function and write a transformation g(c). Then
defines a mapping from the equalized levels of the original image,
It is possible to combine these two transformations such that the image need not be histogram equalized explicitly:
Histogram Specification - Conclusions 1.We can specify the shape of the histogram we want our image to have 2. Specify (perhaps interactively) the histogram we would like 3. compute the cumulative histogram from the desired histogram 4. find the inverse of the desired cumulative histogram (may not be single-valued) 5. two-step process - perform histogram equalization on the image - perform a gray-level mapping using the inverse of the desired cumulative histogram
Local enhancement 1. histogram equalization and histogram specification techniques are based on gray-level distribution over the entire image 2. gray-levels containing important information in a small neighborhood (region of interest) may not be present in sufficient quantities to affect the computation of a mapping based on global information 3. at each pixel do the following - compute the cumulative histogram based on a small neighborhood around the pixel to be mapped - apply histogram equalization using this cumulative histogram
Color processing 1.can apply histogram equalization to color images 2. don't want to apply it using the RGB color model - equalizing R, G, and B bands independently causes color shifts 3. must convert to a color model that separates intensity information from color information (e.g. HSI) 4. can then apply histogram equalization on the intensity band
References http://www.netnam.vn/unescocourse/computervision/22.htm
http://homepages.inf.ed.ac.uk/rbf/HIPR2/histeq.htm