Histogram:
In a typical 8-bit image, there are discrete gray scale levels from 0 to . The histogram of an image represents the probability density function (pdf) of the pixel values in the image over the entire gray scale range. The ith entry of the histogram is
(1) |
(2) |
(3) |
Gray level mapping:
The appearance (brightness, contrast, etc.) of an image represented by its histogram can be modified by a user specified gray level mapping function where is a pixel in the input image and is the corresponding pixel in the output image. This mapping function can be specified in different ways, such as a piecewise linear function, or based on the histogram of either or both of the input and output images.
Programming issues:
The above mapping functions can be carried out for each pixel in the image. However, this is not an efficient way computationally. A more effieicnt way to carry out this gray scale mapping is to u= use a lookup table, which stores the pre-computed mapping for each of the gray levels. The gray level of a pixel in the input image is used as the address to the table and the content of the table entry is the gray level of the corresponding pixel of the output image. Based on the lookup table, the mapping function only needs to be carried out times, instead of () times (size of the image).
Common mapping functions:
Here are some common gray scale mapping functions :
As a special case of piecewise linear mapping, thresholding is a simple way to do image segmentation, in particular, when the histogram of the image is bimodal with two peaks separated by a valley, typically corresponding to some object in the image and the background. A thresholding mapping maps all pixel values below a specified threshold to zero and all above to 255.
(4) |
This mapping is shown below which generates the negative of the input image:
(5) |
If in the image there are only a small number of pixels close to minimum gray level 0 and the maximum gray level , and the gray level of most of the pixels are concentrated in the middle range (gray) of the histogram, the above linear stretch method based on the minimum and maximum gray levels has very limited effect (as the slope is very close to 1).
In this case we can push a small percentage (e.g., , ) of gray levels at either of the two ends of the histogram toward 0 and , as shown in the figure below. (Note that the histograms of the input and output images in the figure below are not to scale.)
A mapping function can be specified by a set of break-points , with neighboring points connected by straight lines, such as shown here:
For example, to increase the contrast of the image of Paolina, we can linearly stretch the gray scales of the image so that the darkest and brightest gray levels are mapped to 0 and 255, respectively.
Code Segments:
Here and are the density and cumulative distribution functions.
for (i=0; i<L; i++) { h[i]=0; H[i]=0; } v=1.0/M/N; for (i=0; i<M; i++) for (j=0; j<N; j++) h[x[i][j]]=h[x[i][j]]+v; H[0]=h[0]; for (i=1; i<L; i++) H[i]=H[i-1]+h[i];
Assume and are fractions such as or .
w=0; min=0; while (w < cut_low) w+=h[min++]; w=0; max=L-1; while (w < cut_high) w+=h[max--];
slope=(L-1)/(max-min); for (i=0; i<L; i++) if (i < min) lookup[i]=0; else if (i >= max) lookup[i]=L-1; else lookup[i]=slope*(i-min);
for (i=0; i<M; i++) for (j=0; j<N; j++) y[i][j]=lookup[x[i][j]];