The idea: count the grey levels, then remap them

A grey image is a grid of numbers. Here it is 8×8 = 64 pixels, and each pixel has one of L = 8 grey levels: 0 is black, 7 is white. The histogram counts how many pixels have each level. A dull, low-contrast image uses only a few levels next to each other. Histogram equalization builds a new level for each old level from the running sum of those counts, so the pixels spread out over the whole range from 0 to 7.

The low-contrast image's histogram has counts 8, 20, 24 and 12 at levels 2 to 5. The running sum is 8, 28, 52, 64. Times 7 and divided by 64, rounded, gives the new levels 1, 3, 6, 7. The output histogram has the same four bars at levels 1, 3, 6 and 7, spread over the range.
Histogram → running sum → s = round(7·c/64): the four used levels 2–5 move to 1, 3, 6 and 7.

Pick a transform to run it on the image, step by step. The top row shows the input image, the mapping curve and the output image. The bottom row shows the two histograms and the mapping table between them. Output → Input feeds the result back in, so you can transform it again.

A histogram of a small image

The histogram is a list of L counts n0 … n7. They always add up to the number of pixels, 64. Divided by 64 they are the fraction of the image at each level, a discrete probability distribution. The histogram says nothing about where the pixels are, only how many there are of each level.

imagecounts n0 … n7levels usedmean, sd
low contrast0 0 8 20 24 12 0 02–53.63, 0.93
dark4 20 26 8 4 2 0 00–51.91, 1.10
washed out0 0 2 4 8 26 20 42–75.09, 1.10

The standard deviation (sd) of the levels is a simple measure of contrast: small means most pixels look alike.

Point transforms: one table for every pixel

A point transform s = T(r) computes each new pixel from the old pixel's own level r, nothing else. With 8 levels, T is just a table of 8 numbers, a lookup table. All pixels of one level look up the same entry, so they move together: one histogram bar moves to a new place as a whole. Two bars can land on the same level and merge, but one bar can never split.

Filters that look at the neighbours of a pixel (blur, sharpen, edges) are not point transforms. See Convolution for those.

Equalization: the running sum is the mapping

Equalization wants every level to hold about the same number of pixels. It walks the levels from dark to light and keeps a running sum ck = n0 + … + nk: the number of pixels at level k or darker. Then it scales that to the level range and rounds:

s_k = round( (L − 1) · c_k / N ) = round( 7 · c_k / 64 )

The running sum divided by N is the cumulative distribution function (CDF) of the levels; see PDF vs CDF. Where the histogram is tall, the sum jumps, so those levels get pushed far apart. Where it is empty, the sum stays flat, and no output level is wasted there. On the low-contrast image (Demo 1: equalize a low-contrast image):

level r2345
count n8202412
running sum c8285264
7 · c / 640.883.065.697
new level s1367

The levels now run from 1 to 7 instead of 2 to 5, and the sd grows from 0.93 to 2.06. The table is the whole method: it costs one pass to count, eight additions, and one lookup per pixel.

Why the result is not flat

A perfectly flat histogram would have 8 pixels at every level. That cannot happen here: the 24 pixels at level 4 all go to the same new level, so that bar stays 24 tall. With few levels, equalization only spreads the bars; it cannot cut them. On the dark image, levels 4 and 5 both map to 7 and merge: 6 levels in, 5 levels out (Demo 4: a dark image, two levels merge).

Equalizing an equalized image changes nothing. The output's running sum already rises in even steps, so every used level maps to itself (Demo 3: equalizing twice changes nothing).

Stretching, gamma and log

The other transforms use a fixed formula. They do not look at the counts (stretching only reads the darkest and the lightest level).

Transfer curves on a 0 to 7 grid of input level r against output level s: the identity diagonal, contrast stretching from (2, 0) to (5, 7), gamma 0.5 bending above the diagonal, the log curve even higher at the dark end, gamma 2 bending below the diagonal, and the equalization staircase for the low-contrast image through 1, 3, 6, 7.
Above the diagonal a curve brightens, below it darkens; a steep part spreads levels apart and a flat part squeezes them.
transforms = T(r)table for r = 0 … 7good for
contrast stretching(r − rmin) · 7 / (rmax − rmin)0 0 0 2 5 7 7 7 (low contrast)a narrow range, used evenly
gamma 0.57 · (r/7)0.50 3 4 5 5 6 6 7dark images: lifts the shadows
log7 · ln(1 + r) / ln 80 2 4 5 5 6 7 7a huge range, such as a Fourier spectrum
gamma 27 · (r/7)20 0 1 1 2 4 5 7washed-out images: darkens the bright end
negative7 − r7 6 5 4 3 2 1 0looking at dark detail, film negatives

Every value is rounded and kept in 0…7. On the dark image, gamma 0.5 sends levels 1, 2, 3 to 3, 4, 5 (Demo 5: gamma 0.5 lifts the shadows); log sends them to 2, 4, 5 (Demo 6: log transform on the dark image). Gamma 2 does the opposite on the washed-out image: levels 5 and 6, which hold 46 of the 64 pixels, go to 4 and 5 (Demo 7: gamma 2 on a washed-out image). Screens and cameras use gamma curves too, around γ = 2.2, to match how eyes see brightness.

Equalization vs stretching

On the low-contrast image both use the full range 0–7 or close to it, but they place the bars differently (Demo 2: contrast stretching on the same image):

contrast stretchinghistogram equalization
mapping for 2, 3, 4, 50, 2, 5, 71, 3, 6, 7
built fromthe darkest and lightest level onlyevery count (the CDF)
shapea straight line: equal gapssteep where the histogram is tall
mean, sd after3.81, 2.304.63, 2.06
one stray pixel at 0 and one at 7does nothing (range is already 0–7)barely changes the result

Stretching is predictable and keeps the look of the image. Equalization adapts to the image, which helps on pictures with a few crowded levels but can make noise and flat areas look harsh.

See also Sampling and Aliasing for how a picture becomes pixels in the first place, and JPEG and the DCT for how grey levels are stored compactly.

See also Morphology: Erosion and Dilation, which cleans up a thresholded binary image by removing noise specks and filling holes.

See also Thresholding and Otsu's Method, which cuts the histogram into two classes instead of spreading it out.

What the page leaves out

Real images have 256 levels (8 bits) and millions of pixels, so the bars are thin and equalization comes out much flatter. Colour images are usually equalized on the brightness channel only. Common variants: the form s = round((c − cmin) · (L − 1) / (N − cmin)), which sends the darkest used level to 0; adaptive equalization (AHE, CLAHE), which uses a histogram per tile and clips tall bars; and histogram matching to a target shape. Piecewise-linear stretching, bit-plane slicing, thresholding, and filters that use neighbours (smoothing, sharpening, edge detection) are left to other pages.

On the mind map

Open the Machine Learning mind mapMachine LearningLearning setup: what feedback, how to scoreLearning setupLinear models: weighted sum w·x + bLinear modelsOther classic models: trees, margins, neighboursOther classic modelsUnsupervised: find structure without labelsUnsupervisedTraining: loss, gradient, small stepTrainingNeural networks: layers of σ(W·a + b)Neural networksArchitectures: how layers share informationArchitecturesLanguage models: predict the next tokenLanguage modelsLLM speed and tuning: cheaper to run, safer to useLLM speed and tuningRetrieval and agents: give the model data and toolsRetrieval and agentsConvolutionConvolutionslide a small kernel over the imageCNN layer by layerCNN layer by layerfc1 holds 78% of the weightsCanny Edge DetectionCanny Edge DetectionBlur, gradient, thin, two thresholds, linkHarris CornersHarris CornersR = det − k·trace² large: cornerHistogram Equalizationrunning sum of counts = new levelsOtsu ThresholdingOtsu ThresholdingMax between-class variance picks tHough TransformHough TransformCollinear pixels' curves cross in one cellErosion and DilationErosion and DilationErode = B fits, dilate = B hitsCNN vs TransformerCNN vs Transformerlocal field vs attention to allRNN, LSTM, GRURNN, LSTM, GRUhidden state; gates fight vanishingSelf-attentionSelf-attentionsoftmax(QKᵀ/√d_k) weights the VDiffusion modelDiffusion modellearn to predict the added noiseVAE vs GANVAE vs GANELBO loss vs generator-discriminator gameContrastive learning, CLIPContrastive learning, CLIPmatch pairs in one space, InfoNCE lossResidual connectionsResidual connectionsx + F(x) lets gradients skip layersOpen the Machine Learning mind mapMachine Learninghow layers share informationArchitecturesConvolutionConvolutionslide a small kernel over the imageCNN layer by layerCNN layer by layerfc1 holds 78% of the weightsCanny Edge DetectionCanny Edge DetectionBlur, gradient, thin, two thresholds, linkHarris CornersHarris CornersR = det − k·trace² large: cornerHistogram Equalizationrunning sum of counts = new levelsOtsu ThresholdingOtsu ThresholdingMax between-class variance picks tHough TransformHough TransformCollinear pixels' curves cross in one cellErosion and DilationErosion and DilationErode = B fits, dilate = B hitsCNN vs TransformerCNN vs Transformerlocal field vs attention to allRNN, LSTM, GRURNN, LSTM, GRUhidden state; gates fight vanishingSelf-attentionSelf-attentionsoftmax(QKᵀ/√d_k) weights the VDiffusion modelDiffusion modellearn to predict the added noiseVAE vs GANVAE vs GANELBO loss vs generator-discriminator gameContrastive learning, CLIPContrastive learning, CLIPmatch pairs in one space, InfoNCE lossResidual connectionsResidual connectionsx + F(x) lets gradients skip layers
This page on the Machine Learning mind map · 81 pages in 10 branches · open it to see the whole map and test yourself in recall mode.