# Histogram Equalization¶

Histogram equalization concept and algorithm applied to digital image color processing.

## 1. Grayscale images¶

Histogram equalization of a grayscale images $x$ (single channel) considers the probability density of the gray levels $i$, defined by:

$$\large p_x(i)=p(x=i)=\frac{n_i}{n}, \quad 0 \leq i \leq L$$

where:

• $n_i$ is the number of occurrences of gray level $i$
• $n$ total number of pixels
• $L$ total number of grey levels (8bits images this value is 256)

Considering that we have the ordered probability density $p_x(i)$, the equalization is given by the cumulative distribution function, defined by:

$$\large cdf_x(i)=\sum_{j=0}^{i}p_x(j)$$

The motivation comes from the continuous space concept of CDF, which could be understanded by:

$$CDF_X(x)=\int_{-\infty}^{x}p_x(t)dt$$

One easier and more performatic way to do the process of histogram equalization is by ordering the gray values and finding its sorted indices, instead of finding the probability distribution and do the cummulative sum of those values. At the end of this process, it is necessary to normalize the output value by the total number of pixels. The result is exactly the same.

## 2. Color images¶

Applies the same concept of grayscale images on multi-channel images.

### 2.1. RGB Histogram equalization¶

Applies histogram equalization, stretching each channel separately. This process does not preserve the hue distribution what it means that the colors might be changed.

### 2.2. HSV Histogram equalization¶

Applies histogram equalization preserving the hue distribution. This process can affect the saturation value or not.