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