This code is about building an algorithm without using the built-in functions in MATLAB, the code gives the histogram of a given image, then normalize and equalize that image, after that, it calculates the accumulative histogram and the transformation function and enhances the image.
The histogram of a digital image with intensity levels in the range [0, L-1] is a discrete function h(rk) = nk, where rk is the kth intensity value and nk is the number of pixels in the image with intensity rk. It is common practice to normalize a histogram by dividing each of its components by the total number of pixels in the image, denoted by the product MN, where, as usual, M and N are the rows and columns dimensions of the image. Thus, a normalized histogram is given by p(rk) = rk / MN, for k=0,1,2….,k-1. Loosely speaking, p(rk) is an estimate of the probability of occurrence of intensity level rk in an image. The sum of all components of a normalized histogram is equal to 1.
2. Histogram Equalization
Consider for a moment continuous intensity values and let the variable r denote the intensities of an
image to be processed. As usual, we assume that r is in the range [0, L-1], with r=0 representing black
and r=L-1 representing white. For r satisfying these conditions, we focus attention on transformations
(intensity mappings) of the form:
S = T(r); 0<= r <= L-1
that produce an output intensity level s for every pixel in the input image having intensity r. We assume that:
(a) T(r) is a monotonically increasing function in the interval 0<= r <= L-1 and
(b) 0<= T(r) <= L-1 for 0<= r <= L-1
A fundamental descriptor of a random variable is its probability density function (PDF). For discrete values, we deal with probabilities (histogram values) and summations instead of probability density functions and integrals. The probability of occurrence of intensity level in a digital image is approximated by
where MN is the total number of pixels in the image, nk is the number of pixels that have an intensity and L is the number of possible intensity levels in the image (e.g., 256 for an 8-bit image). A plot of pr(rk) versus rk is commonly referred to as a histogram. The discrete form of the transformation is
Thus, an output image is obtained by mapping each pixel in the input image with intensity rk into a corresponding pixel with level sk in the output image. The transformation (mapping) T(rk) in this equation is called a histogram equalization or histogram linearization transformation.
3. Histogram Matching (Specification)
As indicated in the preceding discussion, histogram equalization automatically determines a transformation function that seeks to produce an output image that has a uniform histogram. When an automatic enhancement is desired, this is a good approach because the results from this technique are predictable and the method is simple to implement.
Let r and z denote the intensity levels of the input and output images, respectively. pr(r) and pz(z) denote the corresponding continuous probability density functions. 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.
The discrete form of the transformation is
Similarly, given a specific value of Sk, the discrete formulation involves computing the transformation function
For a value of q, so that
G(zq) = sk
Where pz(zi) is the ith value of the specified histogram. As before, we find the desired value zq by obtaining the inverse transformation:
We may summarize the histogram-specification procedure as follows:
- Compute the histogram pr(r) of the given image, and use it to find the histogram equalization transformation in Eq. (1.1). Round the resulting values, to the integer range [0, L-1].
- Compute all values of the transformation function G using the Eq. (1.3-2) for q=0,1,2,…,L-1, where pz(zi) are the values of the specified histogram. Round the values of G to integers in the range [0, L-1]. Store the values of G in a table.
- For every value of sk ,k=0,1,2,…L-1, use the stored values of G from step 2 to find the corresponding value of zq so that G(zq) is closest to sk and store these mappings from s to z.
When more than one value of satisfies the given (i.e., the mapping is not unique), choose the smallest value by convention.
- Form the histogram-specified image by first histogram-equalizing the input image and then
mapping every equalized pixel value, sk, of this image to the corresponding value zq in the
histogram-specified image using the mappings found in step 3.
- Acharya and Ray, Image Processing: Principles and Applications, Wiley-Interscience 2005 ISBN 0-471-71998-6
- Russ, The Image Processing Handbook: Fourth Edition, CRC 2002 ISBN 0-8493-2532-3
- “Histogram Equalization” at Generation5 (archive)
Other codes you might be interested in:
- Car Number Plate Detection Using MATLAB and Image Processing
- MATLAB Code For Face Detection
- Resize an Image in Matlab
- Car License Plate Detection Using Morphological Operators
- LSB Based Image Steganography Using MATLAB