Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 46 ยท Pixel Operations

Thresholding

Turn a grayscale image into a binary mask with a global threshold, find the best cutoff with Otsu's method written from scratch, and handle uneven light.

Thresholding is the simplest way to answer a yes-or-no question about every pixel: "is this pixel part of the thing I care about?" You pick a cutoff value, and every pixel brighter than the cutoff becomes True while every pixel at or below it becomes False. The result is a binary mask, an image with only two states, and almost every later step in a classical pipeline (counting objects, measuring shapes, cleaning up regions) starts from such a mask.

The hard part is not the comparison. It is choosing the cutoff. In this lesson you will start with a hand-picked global threshold, write Otsu's method from scratch so the computer picks the cutoff from the histogram, and then see why a single cutoff fails on unevenly lit images and how a local (adaptive) threshold fixes that. If images as arrays are new to you, read Images as Arrays first; the mask tricks here build on Boolean Masks.

A global threshold

A global threshold uses one number for the whole image. For a grayscale uint8 image, that number lives between 0 and 255. The comparison img > t is a vectorized NumPy expression, so there is no loop over pixels.

The demo below builds a small synthetic image: a dark background (about 40), two bright rectangles (about 200 and 190), and Gaussian noise on top. The helper show prints an image as text so you can see it without a plotting library. Darker values use sparse characters and brighter values use dense ones.

Python A first binary mask

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that mask is a boolean array, so mask.sum() counts the foreground pixels and img[mask] pulls out their intensities. The two rectangles hold 36 and 24 pixels, and exactly 60 pixels pass the threshold, so this cutoff separated the noisy image perfectly.

Why the cutoff matters

A bad cutoff gives a bad mask. Too low and the noisy background leaks into the foreground; too high and part of the object disappears. You can see this by counting how many pixels survive a range of cutoffs:

Python Sweeping the threshold

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At 30 almost the whole image (235 of 288 pixels) is called foreground, because the background itself is centred on 40. At 60 the count drops to 72, a dozen noisy background pixels above the 60 real object pixels. Only the wide plateau in the middle (the 60 pixels at 120 and the 56 at 180, where the dimmer object starts to erode) gives a stable answer. At 250 nothing is left. A good threshold lives in the valley between two peaks of the histogram, which leads straight to Otsu.

Note: Whether the object is brighter or darker than the background decides the direction of the comparison. Use img > t for a bright object on a dark background and img <= t (or ~(img > t)) for a dark object on a bright background, such as printed text on paper.

Otsu's method from scratch

Picking a cutoff by eye does not scale. Otsu's method (published by Nobuyuki Otsu in 1979) picks the cutoff automatically from the image histogram. Its idea: try every possible threshold t, split the pixels into a dark class (below t) and a bright class (at or above t), and measure how well separated the two classes are. The best threshold is the one that maximizes the between-class variance:

TEXT
w0 = share of pixels below t        m0 = mean intensity of that class
w1 = share of pixels at or above t  m1 = mean intensity of that class

between-class variance = w0 * w1 * (m0 - m1)^2

Large variance means the two class averages are far apart and each class holds a healthy share of the pixels. It is mathematically equivalent to minimizing the spread inside the two classes, but the between-class form is cheaper to compute.

flowchart LR
    A["grayscale image"] --> B["histogram<br/>256 bins"]
    B --> C["for each t: split into<br/>dark and bright class"]
    C --> D["between-class variance<br/>for each t"]
    D --> E["t with the largest value"]
    E --> F["mask = img >= t"]

Otsu scans every candidate cutoff over the histogram, never over the pixels, and keeps the one that separates the classes best.

Python Otsu's method in a loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Otsu returns 81 here, a value in the empty gap between the background noise (whose brightest pixel is 79) and the objects (whose dimmest pixel is 162). Any threshold inside that gap gives the same mask, so the exact number is not important; what matters is that the algorithm found the gap without any help.

The same thing without a loop

The loop above is easy to read but it recomputes sums at every step. Cumulative sums give the same variance for all thresholds at once, which is the version you will usually write in practice.

Python Vectorized Otsu

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The best variance (4059.3) matches the loop version. The threshold can differ by a level or two, because every value inside the empty gap scores identically, and argmax just returns the first one it meets. In real photographs there is usually a clear winner.

Warning: Otsu assumes the histogram has two classes. With three or more regions (say sky, building and shadow) it still returns one cutoff, but that cutoff splits only one pair of classes. Multi-level extensions exist; for now just remember the assumption.

Uneven lighting breaks global thresholds

Real scenes are rarely lit evenly. A document photographed beside a window is bright on one edge and dim on the other. Because a global threshold compares each pixel to the same number, dim-but-empty paper can look "darker than the cutoff" while bright ink can look "lighter than the cutoff".

An adaptive (local) threshold avoids this by deciding per pixel. For each pixel it compares the value to the average of its own neighbourhood and calls the pixel foreground only if it is clearly darker (or brighter) than its surroundings. The local average is a blur, so scipy.ndimage.uniform_filter does the job; you will meet it again in Convolution and Correlation.

The synthetic page below has a lighting ramp from 60 on the left to 210 on the right and three dark ink strokes that are 55 levels darker than the paper next to them.

Python Global versus adaptive

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The global mask is a mess: it floods the dark left side with false positives (137 of them) and finds only 135 of the 168 ink pixels. The adaptive mask recovers all 168 ink pixels with zero false positives. The offset 15 matters: without it, noise would make roughly half of every flat area "darker than its local mean". The window size=9 must be larger than the stroke width (2 pixels here) but small compared with the lighting change. Real code tunes both numbers; the usual compromise is a window a few times wider than the thickest feature.

Working with the mask

A binary mask is just a boolean array, so everything from Boolean Masks applies. You can count, measure, combine and paint with it:

Python Combining and using masks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two-sided thresholds like band are common when you pick a range of intensities, for example to isolate one organ in a medical scan or one color channel level in a color mask. Combining masks with &, | and ~ lets you build selections the same way you would filter table rows.

Choosing a method

flowchart TD
    A["Is the lighting even<br/>across the whole image?"] -->|yes| B["Does the histogram<br/>show two peaks?"]
    A -->|no| C["Adaptive threshold<br/>local mean or Gaussian"]
    B -->|yes| D["Otsu"]
    B -->|no| E["Hand-picked cutoff,<br/>or fix the lighting first"]

A quick decision guide. When in doubt, plot the histogram: the histogram lesson shows how.

A few practical tips: smooth very noisy images first (see Blurring and Denoising), because noise puts stray pixels on the wrong side of the line; check the mask by looking at it, not just at its count; and expect to clean the result with morphological operations, since a threshold output almost always has tiny speckles and small holes.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A dark object sits on a bright background. Which expression gives a mask of the object?

    The object is darker than the cutoff, so the pixels at or below t are foreground. img > t would select the background instead.
  2. 2. What does Otsu's method maximize when it tries each candidate threshold?

    Otsu splits the histogram at each t and keeps the t where the dark and bright classes are best separated, measured by their between-class variance.
  3. 3. Why does Otsu's method loop over histogram bins rather than over pixels?

    All the class sizes and means come from the 256 histogram counts, so the cost does not grow with the image size.
  4. 4. A photo of a page is bright on the left and dim on the right. Why does one global threshold fail?

    Ink in the bright region can be lighter than empty paper in the dim region. A local threshold compares each pixel to its own neighbourhood and avoids that problem.
  5. 5. In an adaptive threshold, why subtract a small offset from the local mean before comparing?

    In a flat area each pixel is about as likely to be below its local mean as above it. The offset demands that a pixel is clearly darker than its surroundings.

Summary

  • A threshold turns a grayscale image into a boolean mask with one comparison, img > t; flip the comparison for dark objects.
  • The cutoff should sit in the valley between the histogram peaks. Sweeping the threshold and counting pixels shows how sensitive the mask is.
  • Otsu's method tries every cutoff on the histogram and keeps the one with the largest between-class variance. Cumulative sums vectorize it.
  • Otsu assumes two classes. With uneven lighting a single global cutoff fails.
  • An adaptive threshold compares each pixel to its local mean (plus an offset), which cancels slow lighting changes.
  • Masks combine with &, | and ~, count with sum and mean, and select pixels with img[mask] or np.where.

Next up: move and reshape the pixels themselves in Geometric Transforms.