Histogram equalisation
Histogram equalisation redistributes image intensity values to increase contrast by using the image's cumulative distribution function as a mapping (useful in pre-processing, display and some segmentation tasks).
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Histogram equalisation is used by engineers to improve image contrast where the useful information is concentrated in a small range of gray levels (for example low-contrast X-ray or satellite images). It is a simple, usually global, contrast-enhancement step used before tasks such as thresholding, segmentation or feature extraction.
Key ideas
Normalized histogram / Probability mass function (PMF): p_r(rk) = H(rk)/N, where N is the total number of pixels. This is the empirical probability that a randomly chosen pixel has gray level rk. p_r is dimensionless and sum_k p_r(rk) = 1.
Probability density function (PDF): for a continuous intensity model r taking values in [0, L-1], p_r(r) is the probability density. (Used for theoretical derivations.)
Cumulative distribution function (CDF): the CDF of intensity r is the cumulative probability up to r. For discrete levels: CDF at rk is cdf_k = sum_{j=0..k} p_r(rj). For continuous r: s = T(r) = integral_0^r p_r(w) dw. CDF is dimensionless and ranges 0..1.
Histogram equalisation: a transformation that maps each input gray level r (or discrete rk) to an output gray level s (or sk) using the CDF so that the output intensities are spread across the available range more uniformly. The goal is to produce an output histogram as flat as possible (uniform) under ideal assumptions.
Transfer function (mapping): the function s = T(r) used in histogram equalisation. For discrete images the transfer function is implemented as a lookup table built from the discrete CDF.
Assumptions used in commonly-taught derivations:
- Gray levels are in the continuous interval [0, L-1] for theoretical proofs, or discrete set {0,1,...,L-1} for implementation.
- The mapping T(r) is monotonic non-decreasing and maps range [0, L-1] onto [0, L-1] (after scaling).
- For continuous case we assume differentiable CDF and invertibility where needed.
Derivation (continuous case, standard university proof that equalisation makes output PDF uniform):
- Define s = T(r) = (L-1) * integral_0^r p_r(w) dw. This maps r in [0,L-1] to s in [0,L-1].
- The inverse mapping (where T is invertible) is r = T^{-1}(s).
- Using change of variables: p_s(s) = p_r(r) * |dr/ds|.
- Differentiate s with respect to r: ds/dr = (L-1) * p_r(r). Thus dr/ds = 1 / ((L-1) * p_r(r)).
- Substitute: p_s(s) = p_r(r) * (1 / ((L-1) * p_r(r))) = 1/(L-1). So p_s(s) is constant on [0,L-1]. Therefore the transformed intensity s is uniformly distributed in the continuous ideal case.
Discrete implementation (what universities expect step-by-step):
- Given: number of gray levels L, image with total pixels N, and histogram counts H(rk) for k = 0..L-1.
- Compute PMF: p_r(rk) = H(rk) / N.
- Compute discrete CDF: cdf_k = sum_{j=0..k} p_r(rj).
- Compute mapping (transfer) s_k = floor((L-1) * cdf_k). Note: some textbooks use rounding instead of floor; state which is used and be consistent. A common variant rescales using the smallest non-zero CDF to utilize the full dynamic range when the minimum present gray level > 0: s_k = round(((cdf_k - cdf_min) / (1 - cdf_min)) * (L - 1)); state when you use this.
- Map each pixel with gray level rk to new level s_k using the lookup table.
What to draw for exam diagrams (draw by hand):
- Draw three plots vertically aligned: (a) original histogram as a bar plot with x-axis labeled "gray level r (0..L-1)" and y-axis labeled "H(r) (pixel count)"; (b) transfer curve s = T(r) as a plot with x-axis "r" and y-axis "s"; label points where CDF jumps; (c) equalised histogram as a bar plot with same axes as (a) showing a flatter distribution. Connect corresponding axes with arrows showing mapping.
Formulas
p_r(r_k) = H(r_k) / N — p_r(r_k) is the discrete probability (dimensionless); H(r_k) is pixel count (unit: count); N is total pixels (count). Applies to discrete images.
cdf_k = sum_{j=0..k} p_r(r_j) — cdf_k dimensionless, ranges from 0 to 1. Applies to discrete levels.
s_k = floor((L - 1) * cdf_k) — s_k is the mapped gray level (dimensionless), L is total distinct gray levels (e.g., 256). Use floor or rounding consistently.
T(r) = (L - 1) * integral_{0}^{r} p_r(w) dw — continuous transfer function, r and s in [0, L-1], dimensionless; used for theoretical derivation.
p_s(s) = p_r(r) * |dr/ds| — change-of-variable relation for probability densities; dimensionless; used in derivation.
ds/dr = (L - 1) * p_r(r) — derivative of the continuous mapping; dimensionless.
Worked examples
Example 1 — numeric discrete histogram (university exam style): Problem: A small image has gray levels 0..7 (L = 8). The histogram counts H(rk) are: H(0)=3, H(1)=3, H(2)=6, H(3)=6, H(4)=2, H(5)=2, H(6)=1, H(7)=1. Total pixels N = 24. Compute the equalised levels s_k and the new histogram counts.
Step 1: PMF: p_r(k)=H(k)/N.
- p_r(0)=3/24 = 0.125
- p_r(1)=0.125
- p_r(2)=6/24 = 0.25
- p_r(3)=0.25
- p_r(4)=2/24 = 0.0833333
- p_r(5)=0.0833333
- p_r(6)=1/24 = 0.0416667
- p_r(7)=1/24 = 0.0416667
Step 2: CDF cdf_k = cumulative sum:
- cdf_0 = 0.125
- cdf_1 = 0.25
- cdf_2 = 0.5
- cdf_3 = 0.75
- cdf_4 = 0.8333333
- cdf_5 = 0.9166667
- cdf_6 = 0.9583334
- cdf_7 = 1.0
Step 3: Mapping s_k = floor((L-1) * cdf_k) with L-1 = 7:
- s_0 = floor(7 * 0.125) = floor(0.875) = 0
- s_1 = floor(7 * 0.25) = floor(1.75) = 1
- s_2 = floor(7 * 0.5) = floor(3.5) = 3
- s_3 = floor(7 * 0.75) = floor(5.25) = 5
- s_4 = floor(7 * 0.8333333) = floor(5.8333331) = 5
- s_5 = floor(7 * 0.9166667) = floor(6.4166669) = 6
- s_6 = floor(7 * 0.9583334) = floor(6.7083338) = 6
- s_7 = floor(7 * 1.0) = 7
Step 4: Build new histogram by summing counts for output levels:
- output 0: from r=0 => 3 pixels
- output 1: from r=1 => 3
- output 2: none => 0
- output 3: from r=2 => 6
- output 4: none => 0
- output 5: from r=3 and r=4 => 6 + 2 = 8
- output 6: from r=5 and r=6 => 2 + 1 = 3
- output 7: from r=7 => 1 Total check: 3+3+0+6+0+8+3+1 = 24 (OK).
Final answer (bold): Mapped levels s_k = [0,1,3,5,5,6,6,7]; equalised histogram counts = [3,3,0,6,0,8,3,1].
Example 2 — continuous theoretical derivation (model answer): Question: Show that the continuous mapping s = (L-1)*∫_0^r p_r(w) dw produces a uniform output PDF p_s(s) (assuming T is invertible).
Model answer outline (full-marks steps):
- Define s = T(r) where T(r) = (L-1)*∫_0^r p_r(w) dw.
- Differentiate: ds/dr = (L-1) p_r(r). Rearranged: dr/ds = 1 / ((L-1) p_r(r)).
- Use change of variables: p_s(s) = p_r(r) * |dr/ds| = p_r(r) * (1 / ((L-1) p_r(r))) = 1/(L-1).
- Conclude p_s(s) constant over [0,L-1], i.e., uniform distribution. Show the mapping ranges properly from 0 to L-1.
Example 3 — short conceptual check: If input histogram is already uniform, what does equalisation do? Answer: CDF is linear so s = r and the image is unchanged. State this and justify with the formula.
Common mistakes
- Assuming equalisation always increases perceptual contrast; sometimes it increases noise or misrepresents local contrast, especially for multi-modal scenes.
- Ignoring quantization effects: discrete mapping can produce gaps or level collisions (multiple input levels mapped to same output), and students forget to explain this.
- Mixing continuous and discrete formulas without mentioning assumptions (e.g., using continuous proof to claim perfect uniformity in discrete images).
- Applying histogram equalisation independently to R, G, and B channels of a color image; this distorts colors. For color images, apply HE to a luminance/intensity channel (e.g., Y in YCbCr or V in HSV) or use CLAHE on luminance.
For your exam
Question types likely to appear:
- Definitions and short notes (define histogram, CDF, and histogram equalisation). Full-marks include the definition, units, and a short note on purpose and limitations.
- Derivation questions (prove that the continuous transform yields a uniform output PDF). Full-marks show the change-of-variable steps and assumptions about invertibility.
- Numericals (given H(r) compute s_k and new histogram). Full-marks show N, PMF, CDF table, mapping calculation, and new histogram with a check that totals match.
- Diagram questions (draw input histogram, mapping curve s = T(r), and output histogram). Full-marks include labeled axes, scales, and arrows showing mapping.
- Short programming/pseudocode: implement look-up table mapping; full-marks include explicit steps: compute H, normalize, compute CDF, compute s_k (state rounding), remap pixels.
What examiners look for: explicit normalization, step-by-step tables for discrete problems, state whether using floor or round, checking totals, and brief note on when histogram equalisation is inappropriate.
Quick check
- Define (in one sentence) the histogram of a grayscale image.
- Write the discrete formula for the mapping s_k used in histogram equalisation (state the scaling factor).
- If the input PDF p_r(r) is uniform on [0,L-1], what is T(r) and what does equalisation do?
Answers: 1) The histogram is H(r_k) = count of pixels with gray level r_k (a frequency distribution of intensity values). 2) s_k = floor((L-1) * sum_{j=0..k} H(r_j)/N). 3) T(r) = r (linear); equalisation leaves the image unchanged.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?