Image Processing & Filtering

image thresholding

Thresholding is the simplest way to turn a grayscale image into a black-and-white (binary) one: pick a cutoff value, then label every pixel brighter than it as foreground (white, 1) and everything else as background (black, 0). It answers the question, is this pixel part of the object or not, with a single decision per pixel. Despite its crudeness it is the front door to segmentation: separating text from paper, cells from a slide, or a bright tool from a dark conveyor belt.

The basic version is global thresholding: g(x) = 1 if f(x) > t, else 0, using one threshold t for the whole image. The hard part is choosing t. Otsu's method automates it by assuming the histogram is bimodal, two humps for foreground and background, and searching every candidate t to find the one that best separates the two groups. Concretely it maximizes the between-class variance w0*w1*(mu0 - mu1)^2, where w0 and w1 are the pixel fractions of the two classes and mu0, mu1 their mean intensities; this is mathematically equivalent to minimizing the variance within the two classes. The chosen t is the valley between the two humps.

A single global threshold fails when lighting is uneven: a shadow can make foreground in one corner darker than background in another, so no constant cutoff works. Adaptive (local) thresholding solves this by computing a different threshold for each pixel from its neighbourhood, typically the local mean or a Gaussian-weighted mean minus a small constant C. This tracks slowly varying illumination and is the standard choice for scanning documents or photographing whiteboards. After thresholding, morphological cleanup usually removes speckle and fills holes.

A photo of a printed page lit from one side: a global Otsu threshold blacks out the shadowed half, while adaptive thresholding with an 11x11 mean window cleanly recovers crisp text across the whole page.

Pitfall: Otsu assumes two well-separated, comparably sized peaks. On images with one dominant class, gradients instead of two flat regions, or heavy noise, it places the threshold poorly. Pre-smoothing and choosing global vs adaptive deliberately matters more than the threshold value itself.

Also called
binarizationOtsu's methodadaptive thresholding