All Articles

Otsu's Thresholding: How We Perfectly Separate Text from Any Background

Otsu's Thresholding: How We Perfectly Separate Text from Any Background blog cover

In 1979, Japanese engineer Nobuyuki Otsu published a paper that would become one of the most-cited works in computer vision history. His insight: the "optimal" threshold for converting a grayscale image to black-and-white can be found automatically, by minimising the intra-class variance of pixel intensities.

We use Otsu's algorithm as the primary binarisation step in FontFinder's preprocessing pipeline — and it's one of the reasons our font detection works on such a wide variety of images.

The Problem: Choosing a Threshold

A grayscale image contains pixel values from 0 (black) to 255 (white). To turn it into a binary image — black text on white background — you need to decide: "pixels below value X become black, pixels above X become white." But what should X be?

Set it too low, and background noise becomes text. Set it too high, and thin strokes disappear. A fixed value of 128 works sometimes, but fails completely on dark images, light images, or anything with unusual contrast.

Otsu's Solution

Otsu's algorithm looks at the histogram of all pixel values and finds the threshold that best separates two populations: the "foreground" (text) and "background" pixels. It does this by minimising the weighted sum of the within-class variances of the two groups.

Mathematically, it iterates through every possible threshold value (0-255), calculates how "tight" each group's intensity distribution is at that threshold, and picks the value where both groups are most internally consistent.

When Otsu's Works Best

Otsu's algorithm performs exceptionally well when:

  • The image has clear bimodal intensity distribution (bright background, dark text)
  • There's reasonable contrast between text and background
  • Lighting is roughly uniform across the image

This covers screenshots, digital images, scanned documents, and most logo files — the majority of what FontFinder users upload.

When We Fall Back to Adaptive Thresholding

For photos of physical media — books, packaging, signage — lighting is rarely uniform. A shadow across part of the image means the "correct" threshold value is different in different regions.

In these cases, we use adaptive (local) thresholding: we compute a separate threshold for each small region of the image based on the local mean intensity. The result is much better text separation on physically photographed subjects.

Choosing the Winner

FontFinder runs both algorithms on every image and scores each result using an edge density metric. We count the number of distinct edges in the binarised image — clean text creates sharp, well-defined edges, while over-thresholded or under-thresholded results produce jagged or missing strokes. The algorithm with the better edge score wins.

Working Through the Maths

The algorithm is easier to trust once you have seen the quantity it optimises. For a given threshold t, every pixel falls into one of two classes: below t, or at or above t. Each class has a weight (the fraction of pixels it contains) and a variance (how spread out its intensities are). Otsu computes the weighted sum of those two variances and picks the t that makes it smallest.

The useful trick is that minimising within-class variance is mathematically equivalent to maximising between-class variance, and the between-class form is far cheaper to evaluate. It reduces to the two class weights multiplied by the squared difference of their means. Because both class weights and means can be updated incrementally as t advances from 0 to 255, the whole search is a single pass over a 256-bin histogram — a few microseconds, regardless of how large the image is.

That efficiency is why a 1979 algorithm is still in production code. It is not merely good enough; for a genuinely bimodal histogram it is optimal, and it costs almost nothing.

The Bimodality Assumption, and When It Breaks

Every guarantee Otsu offers rests on one assumption: that the histogram has two distinct peaks. When it does not, the method still returns a threshold, but that threshold is meaningless. Three common cases:

  • Text over a gradient. A headline on a colour-graded hero image produces a smeared, unimodal histogram. Otsu splits it at an arbitrary point, typically slicing the gradient in half rather than separating the letters.
  • Very sparse text. One word on a large white background is perhaps two percent dark pixels. The foreground peak is so small it barely registers, and the chosen threshold drifts into the background distribution, thickening the glyphs.
  • Anti-aliased screenshots. Rendered text has a wide band of intermediate greys along every edge. A global threshold has to assign that band to one side or the other, which either thins or fattens every stroke uniformly.

The first two are exactly why we run adaptive thresholding in parallel rather than trusting Otsu alone. The third is handled upstream, by capping resolution so that the anti-aliased band stays thin relative to stroke width.

Why Not Just Use Adaptive Thresholding Everywhere?

If local thresholding handles uneven lighting, the obvious question is why we keep the global method at all. The reason is that adaptive thresholding invents texture in flat regions. Where a neighbourhood contains no text — pure white background — the local mean is nearly equal to every pixel in it, so tiny amounts of sensor noise decide the outcome. The result is a field of speckle that morphological cleanup then has to remove, and that contour detection can mistake for characters.

Otsu has the opposite bias. On a clean screenshot it produces flawless, artifact-free output because a single global cut is genuinely the right model of the image. Running both and scoring the results lets each method cover the other's failure mode, which is cheaper and more reliable than trying to pick the right one in advance.

Otsu's Legacy

Nobuyuki Otsu's original paper, published in IEEE Transactions on Systems, Man, and Cybernetics, is among the most-cited works in the field. Its endurance is a reminder that a well-chosen objective function can outlast decades of architectural fashion. Document scanners, OCR pipelines, industrial inspection systems, and medical imaging tools all still call it — usually as one line, often without the caller knowing whose method it is.