Intensity transformations and contrast stretching
Intensity transformations change pixel grey levels by a mapping function; contrast stretching rescales a chosen input range to increase image contrast, with common transforms including linear (piecewise), log and gamma correction.
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
Intensity transformations and contrast stretching are simple, fast ways an engineer uses to change image brightness and contrast before further processing. They are used in pre-processing for feature detection, visualization (making details visible), and as a step before segmentation, filtering or compression.
Key ideas
Contrast: the difference in intensity between local or global features. Higher contrast means larger separations of grey levels for useful details.
Contrast stretching (dynamic range expansion): a family of transforms that map a narrow input intensity range to a wider output range to increase perceived contrast. A simple form is linear stretching between two anchor points.
Dynamic range / range of intensities: the interval of possible grey levels; for an L-level image typically 0..L-1 (e.g. L = 256 for 8-bit images).
Saturation / clipping: when a mapping sends values outside the output range, they are clipped to the nearest valid value (e.g., <0 -> 0, >255 -> 255). This can lose information.
Piecewise-linear mapping: a mapping composed of linear segments. Useful to increase contrast in some ranges while compressing others (for example midtone emphasis).
Gamma correction: a non-linear power-law transform s = c·r^γ that brightens (γ < 1) or darkens (γ > 1) midtones while preserving black and white endpoints. r is usually normalized to [0,1].
Log transform: s = c·log(1 + r) compresses high intensities and expands low intensities; useful when image has large dynamic range and you want to reveal dark details.
Negative (intensity inversion): s = (L-1) − r maps dark to bright and vice versa; useful to enhance light details on dark backgrounds and for photographic negatives.
Gray-level slicing (intensity-level slicing): emphasize a chosen range [A, B] by mapping it to higher output while keeping others lower. Two variants: with background (retain original background levels but boost [A, B]) and without background (set outside [A, B] to 0). Often implemented with a piecewise-linear T(r).
Lookup table (LUT): an array that records s = T(r) for each possible r. Implementations use LUTs for speed: transform each pixel by indexing.
Derivation universities commonly ask (two-point linear mapping):
Given two points (r1, s1) and (r2, s2) that you wish to map linearly, derive line equation s(r).
- Assume s = a·r + b.
- Substitute the two point conditions: s1 = a·r1 + b and s2 = a·r2 + b.
- Subtract: s2 - s1 = a·(r2 - r1) => a = (s2 - s1)/(r2 - r1).
- Solve b = s1 - a·r1.
This is the general linear equation. For min-max contrast stretching you set (r1, s1) = (r_min, s_min) and (r2, s2) = (r_max, s_max) and substitute.
Diagram to draw for an exam answer (describe what to draw and label):
- Draw a 2D plot: horizontal axis labelled "r (input grey level)" from 0 to 255, vertical axis labelled "s (output grey level)" from 0 to 255.
- Mark points (r_min, s_min) and (r_max, s_max) and draw a straight line joining them; if piecewise, draw the other segments joining (0,0) to (r_min,s_min) and (r_max,s_max) to (255,255) as needed.
- Label axes, the two anchor points and show arrow indicating mapping direction.
Assumptions commonly stated in answers:
- Pixel values are discrete but treated as continuous in algebraic derivations; final values must be rounded and clipped to the integer range 0..L-1.
- r is normalized to [0,1] when applying power-law transforms unless otherwise stated.
- For order-preserving enhancement, T(r) should be monotonically nondecreasing to avoid reversing the relative order of intensities.
Formulas
General intensity transform: s = T(r) — s: output grey level (dimensionless), r: input grey level (dimensionless). Applies to any pointwise transform.
Linear mapping: s = a·r + b — a, b: constants (dimensionless). Applies when transform is affine linear.
Slope for two-point mapping: a = (s2 - s1)/(r2 - r1) — r1, r2, s1, s2 dimensionless; requires r2 ≠ r1.
Intercept: b = s1 - a·r1 — same symbols as above.
Min–max contrast stretch: s = (r - r_min)·(s_max - s_min)/(r_max - r_min) + s_min — r_min,r_max,r,s_min,s_max dimensionless; requires r_max ≠ r_min. Use when you want r_min→s_min and r_max→s_max.
Piecewise-linear (three-segment) mapping: s(r) =
if r ≤ r1: s = (s1/ r1)·r
if r1 < r ≤ r2: s = ((s2 - s1)/(r2 - r1))·(r - r1) + s1
if r > r2: s = ((L-1 - s2)/(L-1 - r2))·(r - r2) + s2 — where 0 < r1 < r2 < L-1 and values are dimensionless; L is number of levels. If r1 = 0 or r2 = L-1, handle those endpoints explicitly to avoid division by zero (e.g., the first/third segment reduces to s = 0 at r = 0 or s = L-1 at r = L-1).
Log transform: s = c · ln(1 + r) — r dimensionless (often raw 0..255), c chosen so s fits output range; ln is natural logarithm.
Choose c to map R_max to L-1: c = (L-1) / ln(1 + R_max) — R_max: maximum input grey level (e.g., 255), L: number of grey levels (e.g., 256).
Gamma (power-law) transform: s = c · r^γ — r normalized to [0,1] (dimensionless), γ (gamma) > 0 dimensionless, c chosen often = 1; multiply final s by (L-1) for integer outputs if r uses integer domain.
Histogram-based equalisation (reference formula): s = (L-1) · cumulative_probability(r) — where cumulative_probability(r) is the cumulative distribution function of input intensities. (Histogram equalisation is a related technique; see the separate topic Histogram equalisation.)
Notes on units: grey levels are dimensionless numbers; the SI-unit column is therefore "dimensionless" for every formula. Always state when r must be normalized to [0,1].
Worked examples
Example 1 — Min–max linear contrast stretch (8-bit image)
Given: 8-bit image (L = 256, grey levels 0..255). Let r_min = 50, r_max = 200. We want to map r_min → s_min = 0 and r_max → s_max = 255.
Formula: s = (r - r_min)·(s_max - s_min)/(r_max - r_min) + s_min
Calculation: slope = (255 - 0)/(200 - 50) = 255/150 = 1.7
So s = 1.7·(r - 50)
Compute for three input pixels:
- r = 50: s = 1.7·(50 - 50) = 0. Final integer = 0.
- r = 125: s = 1.7·(125 - 50) = 1.7·75 = 127.5 → round to nearest integer 128.
- r = 200: s = 1.7·(200 - 50) = 1.7·150 = 255 → 255.
Remember to clip values <0 or >255 and to round to integer if image uses integer storage.
Example 2 — Gamma correction (power-law) on three sample pixels
Given: 8-bit image. Choose normalization r_norm = r/255 to map to [0,1]. Use c = 1 and present results mapped back to 0..255.
Input values: r = 64, 128, 192.
Compute r_norm: 64/255 ≈ 0.25098, 128/255 ≈ 0.50196, 192/255 ≈ 0.75294.
Case A: γ = 0.5 (brighten midtones)
s_norm = r_norm^0.5. Multiply by 255 and round.
- r = 64: s_norm ≈ sqrt(0.25098) ≈ 0.50098 ⇒ s ≈ 0.50098·255 ≈ 127.8 ⇒ 128.
- r = 128: s_norm ≈ sqrt(0.50196) ≈ 0.70854 ⇒ s ≈ 180.7 ⇒ 181.
- r = 192: s_norm ≈ sqrt(0.75294) ≈ 0.86773 ⇒ s ≈ 221.4 ⇒ 221.
Case B: γ = 2 (darken midtones)
s_norm = r_norm^2.0.
- r = 64: s_norm ≈ (0.25098)^2 ≈ 0.06299 ⇒ s ≈ 16.1 ⇒ 16.
- r = 128: s_norm ≈ (0.50196)^2 ≈ 0.25196 ⇒ s ≈ 64.3 ⇒ 64.
- r = 192: s_norm ≈ (0.75294)^2 ≈ 0.56792 ⇒ s ≈ 144.8 ⇒ 145.
Example 3 — Log transform mapping to full 8-bit range
Given: r in 0..255, choose c so that r = 255 maps to s = 255.
c = 255 / ln(1 + 255) = 255 / ln(256) ≈ 255 / 5.545177 = 45.98
Compute r = 0, 15, 255:
- r = 0: s = 45.98·ln(1 + 0) = 0 ⇒ 0.
- r = 15: s = 45.98·ln(16) = 45.98·2.772589 ≈ 127.5 ⇒ 128.
- r = 255: s = 45.98·ln(256) = 255 ⇒ 255.
Show in answers how dark values expand and bright values compress with a log transform.
Common mistakes
- Mixing up input/output axes when sketching s(r) graphs (label axes carefully).
- Using linear scaling with r_max = r_min (division by zero); always check and handle the constant image case.
- Applying the same per-channel transform to RGB images without converting to a luminance/lightness channel; doing per-channel stretching can change colours (use the separate topic Colour image processing and transformations for full treatment).
- Confusing log base 10 and natural log; most image processing formulas use natural logarithm (ln).
- Not using a LUT for discrete images when implementing transforms, which is slower and error-prone.
For your exam
Typical question types:
- Definitions: define intensity transform, contrast stretching, gamma correction.
- Short notes: explain log transform vs gamma correction, advantages and when to use each.
- Derivation: derive the two-point linear mapping and the min–max stretching formula.
- Numerical: given r_min,r_max and some pixel values compute mapped s values (show rounding and clipping).
- Diagram: draw and label piecewise-linear mapping and explain effect on contrast.
What a full-marks answer includes:
- Correct definitions with bolded key terms; explicit domain and range of mapping.
- Step-by-step derivation for linear mapping, showing algebra and assumptions (r2 ≠ r1).
- A clear diagram with axes labelled and anchor points marked (for mapping questions).
- Correct numerical computation with units (dimensionless), rounding and clipping shown.
- Remarks about implementation (LUT, per-channel caution for color images).
Quick check
- What is the general form of a power-law transform?
- For min–max stretch mapping r_min→0 and r_max→255, what is the slope used?
- Why must you clip results after applying a transform?
Answers: 1) s = c·r^γ. 2) slope = 255/(r_max - r_min). 3) To ensure output values lie in the valid storage range (e.g., 0..255) and to avoid invalid pixel values or wrap-around.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?