Histogram equalization algorithm and code

Source: Internet
Author: User

The histogram equalization algorithm consists of three steps: the first step is to count the number of occurrences of each gray level of the histogram, the second step is the cumulative normalized histogram, and the third step is to calculate the new pixel value.

Step 1:

For (I = 0; I For (j = 0; j <width; j ++ ){
N [s [I] [J] ++;
}
}

For (I = 0; I <L; I ++ ){

P [I] = N [I]/(width * Height );

}

Here, N [I] indicates the number of pixels whose gray level is I, L indicates the maximum gray level, and width and height indicate the width and height of the original image, respectively, P [I] indicates the probability that pixels with a gray level of I appear in the entire image (in fact, P [] stores the normalized histogram of this image ).

Step 2:

For (I = 0; I <= L; I ++ ){
For (j = 0; j <= I; j ++ ){
C [I] + = P [J];
}
}

C [] This array stores the cumulative normalized histogram.

Step 3:

Max = min = s [0] [0];
For (I = 0; I For (j = 0; j <width; j ++ ){
If (max <s [I] [J]) {

Max = s [I] [J];

} Else if (min> S [I] [J]) {

Min = s [I] [J];

}
}
}

Find the maximum and minimum values of pixels.

For (I = 0; I For (j = 0; j <width; j ++ ){
T [I] [J] = C [s [I] [J] * (max-min) + min;
}
}

T [] [] is the result of the final histogram equalization.

Image before processing:

Processed image:

For color images, histogram equalization generally does not allow the preceding operations on the R, G, and B components, instead, Convert RGB to HSV to perform histogram equalization for the V component. For how to Convert RGB and HSV, see my other article. The method has been encapsulated into a function (not encapsulated by me, haha). You can use it directly.

References:

Http://zh.wikipedia.org/wiki/%E7%9B%B4%E6%96%B9%E5%9B%BE%E5%9D%87%E8%A1%A1%E5%8C%96

 

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: info-contact@alibabacloud.com 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.