Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Thresholding

Thresholding converts a gray-level image into regions by comparing pixel intensities to one or more thresholds; key methods include global, adaptive and Otsu's automatic threshold selection.

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

Thresholding is one of the simplest and most used segmentation tools in image processing. Engineers use thresholding to separate foreground from background (for example, parts vs. background in machine-vision inspection, text from paper in OCR, or lesion vs. healthy tissue in medical images) before further steps such as feature extraction or object recognition.

Key ideas

Start from first principles: an image is a 2-D function I(x,y) giving a gray level (intensity) at each pixel (x,y). Gray levels are usually integers in a finite range, e.g. 0..255. We will define and use terms as we go.

  • Thresholding: For a given threshold T, a binary threshold function maps each pixel to one of two values. Standard binary thresholding is defined as: output 1 (foreground) if I(x,y) > T, else 0 (background). This is the basic decision rule used in segmentation.

  • Global thresholding: A single scalar T is applied to the entire image. Assumption: foreground and background intensities form separable clusters (often visible as a bimodal histogram).

  • Bimodal histogram: A histogram of gray levels h(i) is called bimodal if it has two distinct peaks separated by a valley; this suggests two dominant classes (background and foreground).

  • Adaptive (local) thresholding: The threshold T(x,y) is computed using statistics of a local neighborhood (window) around (x,y), e.g. local mean or local Gaussian-weighted mean. This is used when lighting or background intensity varies across the image.

  • Otsu's automatic threshold selection: A global, non-parametric method that picks T maximizing the between-class variance (equivalently minimizing within-class variance) of the two groups produced by thresholding. Otsu's method assumes that an optimal threshold separates the histogram into two classes and derives a closed-form criterion based on class probabilities and means.

Derivation of Otsu's criterion (common exam derivation):

  1. Let the image have L gray levels 0..L-1. Let n_i be the number of pixels with intensity i, and N = sum_i n_i total pixels.
  2. Define the normalized histogram (probabilities) p_i = n_i / N. Then sum_i p_i = 1.
  3. For a threshold T, define two classes: C0 = {0..T} and C1 = {T+1..L-1}.
  4. Class probabilities: omega_0 = sum_{i=0..T} p_i, omega_1 = sum_{i=T+1..L-1} p_i = 1 - omega_0.
  5. Class means: mu_0 = (1/omega_0) * sum_{i=0..T} i * p_i, mu_1 = (1/omega_1) * sum_{i=T+1..L-1} i * p_i.
  6. Total mean mu_T = sum_{i=0..L-1} i * p_i.
  7. Between-class variance: sigma_b^2 = omega_0 (mu_0 - mu_T)^2 + omega_1 (mu_1 - mu_T)^2.
  8. Algebraic simplification (standard step): sigma_b^2 = omega_0 * omega_1 * (mu_0 - mu_1)^2. Otsu chooses T that maximizes sigma_b^2 over T=0..L-2. Equivalently, since the total variance sigma_T^2 is independent of T, minimizing the within-class variance sigma_w^2 = omega_0 sigma_0^2 + omega_1 sigma_1^2 (where sigma_0^2 and sigma_1^2 are class variances) is the same as maximizing sigma_b^2, using the identity sigma_T^2 = sigma_w^2 + sigma_b^2.

Assumptions behind Otsu: classes are well separated by histogram peaks; image noise and overlap of classes reduce effectiveness. Otsu is global; it fails if illumination is spatially varying, so use adaptive methods in that case.

Diagrams to draw for exam answers:

  • Draw a grayscale histogram (horizontal axis intensity 0..255, vertical axis frequency). Mark a vertical line at T and label the two areas under the histogram as C0 and C1. Label peaks and valley.
  • For Otsu, draw the same histogram and annotate p_i, omega_0, omega_1, mu_0, mu_1 and the total mean mu_T on the intensity axis.

Variants and other definitions:

  • Two-sided thresholding (range thresholding): keep pixels within [T1, T2].
  • Multilevel thresholding: generalize to more than two classes with several thresholds T1 < T2 < ... < Tk producing k+1 classes.
  • Hysteresis thresholding: uses two thresholds T_low and T_high and connectivity to retain weak edges connected to strong edges (used in edge detection). Not the same as segmentation thresholding but related.

Formulas

p_i = n_i / N where p_i is the probability (normalized histogram) of gray level i; n_i is count (unit: pixels); N is total pixel count (pixels). Applies to any discrete grayscale image.

omega_0(T) = sum_{i=0..T} p_i class probability of background (unitless), for threshold T.

omega_1(T) = sum_{i=T+1..L-1} p_i = 1 - omega_0(T) class probability of foreground (unitless).

mu_0(T) = (1/omega_0) * sum_{i=0..T} i * p_i mean gray level of class C0 (gray levels in 0..L-1; defined only if omega_0(T) > 0).

mu_1(T) = (1/omega_1) * sum_{i=T+1..L-1} i * p_i mean gray level of class C1 (gray levels; defined only if omega_1(T) > 0).

mu_T = sum_{i=0..L-1} i * p_i total mean intensity (gray levels).

sigma_b^2(T) = omega_0 * omega_1 * (mu_0 - mu_1)^2 between-class variance for threshold T (square gray level units). Otsu maximizes this over T.

Binary output B(x,y) = 1 if I(x,y) > T, else 0 (alternative definitions may include >= or invert 0/1 depending on convention). Output is unitless (binary).

For adaptive mean thresholding in a window W around (x,y):

T(x,y) = mean_{(u,v) in W} I(u,v) - C where C is a constant subtracted to tune sensitivity (in gray levels).

Worked examples

Example 1 — small image and Otsu selection (numerical, exam style): Image: 4x4 pixels (N = 16). Intensities: first 8 pixels value 50, next 8 pixels value 200. Step 1: histogram: n_50 = 8, n_200 = 8, all other n_i = 0. So p_50 = 8/16 = 0.5, p_200 = 0.5. Step 2: total mean mu_T = 500.5 + 2000.5 = 25 + 100 = 125. Step 3: any threshold T in [50..199] separates the two classes; choose T such that C0 contains 50 and C1 contains 200. Then omega_0 = 0.5, omega_1 = 0.5. Step 4: mu_0 = 50, mu_1 = 200. Step 5: sigma_b^2 = omega_0 * omega_1 * (mu_0 - mu_1)^2 = 0.50.5(150)^2 = 0.25*22500 = 5625. Conclusion: any T in the gap (e.g. T = 125) is optimal. Final binary image: eight pixels 0 (if we set foreground=1 for >T) and eight pixels 1. Final answer: T = any value with 50 <= T < 200 (e.g., 125); sigma_b^2 = 5625.

Example 2 — adaptive mean thresholding (5x5 numeric): Image I (rows): [ 50 52 55 200 205 ] [ 48 51 53 202 203 ] [ 49 50 52 199 204 ] [ 47 49 51 201 206 ] [ 46 48 50 198 200 ] Take window W = 3x3 centered at pixel (row3,col3) with values: [51,53,202;50,52,199;49,51,201] — note this window crosses bright region. Compute local mean: mean = (51+53+202+50+52+199+49+51+201)/9 = (908)/9 ≈ 100.89. Choose C = 10. Then T(center) = 100.89 - 10 = 90.89. Since I(center) = 52 < 90.89, binary output is 0 (background). This shows adaptive method uses local stats so center remains background despite nearby bright pixels. Final answer: local T ≈ 90.9; B(center)=0.

Example 3 — quick model answer: explain why global threshold fails under non-uniform illumination and adaptive helps: include a short paragraph describing illumination gradient and show that global T mislabels shadow areas while local mean adjusts per neighborhood.

Common mistakes

  • Forgetting that Otsu's sigma_b^2 must be computed for every candidate T and picking the T that maximizes it.
  • Using global thresholding on images with strong illumination gradients — expected to fail; students sometimes claim it should always work.
  • Mixing up class indices when computing mu_0 and mu_1 (which intensities belong to which class depends on T).
  • Not carrying units or stating pixel counts when doing numerical examples — always state N and n_i.

For your exam

Types of questions you will get on thresholding:

  • Definitions: define thresholding, global vs adaptive, Otsu's method, bimodal histogram.
  • Short notes: explain when to use adaptive vs global, describe multilevel thresholding and hysteresis.
  • Derivation: derive Otsu's between-class variance formula and show sigma_b^2 = omega_0omega_1(mu_0 - mu_1)^2.
  • Numerical: small histogram or small image where you must compute p_i, omegas, mus, and find the Otsu threshold or apply adaptive thresholding numerically.
  • Diagrams: draw a histogram and mark classes and means; explain visually where threshold goes.

What a full-marks answer includes:

  • Clear definitions with notation and conventions stated.
  • Correct derivation steps for Otsu (show sums and algebraic simplification).
  • For numericals, show histogram counts, normalized probabilities, class sums, intermediate arithmetic with N and units, and the final threshold value.
  • When asked to discuss suitability, list assumptions and failure cases (e.g., unimodal histogram, non-uniform illumination).

Quick check

  1. Define global thresholding in one sentence.
  2. What does Otsu's method maximize: within-class variance, between-class variance, or total variance?
  3. In adaptive mean thresholding, what parameter is commonly subtracted from the local mean and why?

Answers: 1) Global thresholding applies a single scalar T to the whole image and maps pixels to foreground if their intensity exceeds T and to background otherwise. 2) Between-class variance. 3) A constant C (positive) is subtracted to make the threshold more selective (reduce false positives); C is tuned experimentally.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?