Image processing ------ histogram equalization

Source: Internet
Author: User

I. Mathematical derivation of histogram equalization

The general idea of histogram equalization: first, consider the continuous function and let the variable R represent the gray level of the image to be enhanced. Suppose it is normalized to the interval [0, 1], and r = 0 indicates black and r = 1 indicates white. Then consider a discrete formula and allow the pixel value within the range [0, L-1.

For continuous functions, assume that the transform function is

S = T (r), 0 = <r <= 1

In the original image, a gray value S is generated for each pixel value R. The transform function must meet the following conditions:

(1) T (r) is a single value in the interval and increases monotonically. This is to ensure the existence of its inverse function, and the output image increases from the black to the white order;

(2) When 0 = <r <= 1, 0 = <T (r) <= 1. This ensures that the output gray level has the same range as the input.

Represent the inverse function of S = T (r)

The gray level of an image can be regarded as a random variable with a range of [0, 1. One of the most important basic descriptions of a random variable is its probability density function. And represent the probability density functions of random variables R and S. The lower mark and the lower mark are used to represent different functions. A basic result is obtained from the basic probability theory: if it is known and meets the condition (1), then the probability density function of the variable s can be obtained by the following simple formula:

Therefore, the probability density function of variable S is determined by the gray-level probability density function of the input image and the selected transformation function.

An important transform function in image processing:

The value of the product function is positive, and the function points are the area under a function curve, which contains the cumulative distribution function of the random variable R, therefore, it complies with the condition that the single value is added monotonically, so the condition (1) is met ). Similarly, the interval [0, 1] also meets the condition (2 ). The integration process is as follows:

Use this result to replace Dr/DS with the above formula. The probability is positive and the following result is obtained:

Because it is a probability density function, we can obtain that the value of the range [0, 1] is 0, and the form given in the above formula is the uniform probability density function. In other words, the transformation function given above will obtain a random variable. Its features are a uniform probability density function, which is irrelevant to the function form. In summary, we can see that this is the basic principle of histogram equalization. The right of these formulas is the cumulative distribution function of random variable R. In this way, it is converted to the cumulative distribution function of the input image at the gray level.

The following describes discrete functions. For discrete values, the sum of the probability of the function is processed, rather than the integral of the probability density function. The probability of gray-level appearance in an image is similar:

N indicates the total number of pixels in the image, NK indicates the number of pixels in gray scale, and l indicates the total number of pixels in the image. The discrete form of the transform function in formula 4 is:

Therefore, the processed image (that is, the output image) is obtained by ing the gray-level pixels in the input image to the corresponding pixels in the output image whose gray-level is SK. Different from the continuous form, it is generally not proved that the discrete transformation can generate the discrete value of the even probability density function (for the even histogram ). However, it can be easily seen that the above application has a general tendency to expand the input image histogram, so that the grayscale level of the histogram balanced image can span a larger range. So far, the entire process of proof is provided.

II. General implementation process of histogram equalization:

(1) count the number of pixels in each gray level of the original input image, where l indicates the total gray level;

(2) Calculate the original image histogram, that is, the probability density of each gray level, and N is the total number of pixels of the original image;

(3) Calculate the Cumulative Distribution Function

(4) Calculate the final output gray level,

In formula, int [] is the integer operator. To make gmin = 0, gmax = L-1, the formula is simplified

(5) use the FK ing relationship between FK (Gray Level Function of the original image) and GK to modify the gray level of the original image and obtain the output image. The histogram is approximately evenly distributed.

3. program description and code

To verify the processing effect of direct and balanced image processing, write code to verify the above derivation. The software used is MATLAB. The following code is used:


Clear all;

Img‑imread('lena.jpg ');

Subplot (2, 2, 1 );

Imshow (IMG );

Title ('source image );

Gray_img = rgb2gray (IMG );


[M, N] = size (gray_img );

PR = zeros (1,256 );

For I = 1:256

Pr (I) = length (find (gray_img = i-1)/(m * n );


Subplot (2, 2 );

Bar (PR );

Title ('histogram of the original origin ')

S = zeros (1,256 );

For I = 1:256

For j = 1: I

S (I) = Pr (j) + S (I );



S1 = round (S * 255) + 0.5 );

For I = 1:256

Q (I) = sum (Pr (find (S1 = I )));


Subplot (2, 2, 4 );

Bar (q)

Title ('processed histograms ')

Res_img = gray_img;

For I = 1:256

Res_img (find (gray_img = i-1) = S1 (I );


Subplot (2, 2, 3); imshow (res_img );

Title ('balanced graph effect ');


1 is the running result of lena.jpg, and 2 is another processing image:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.