Image thresholding

On this page 7

Image thresholding is a segmentation technique that converts a grayscale image into a binary image by comparing each pixel intensity with a cutoff value, called the threshold. Pixels above the threshold become foreground and pixels below it become background. In computer vision, it is one of the oldest and fastest ways to separate objects from their surroundings, and it remains a standard preprocessing step for OCR, inspection and document analysis.

The method is simple, but choosing the threshold is the real problem. A fixed value works only under controlled lighting, so practical systems rely on automatic and adaptive methods that derive the cutoff from the image itself.

How thresholding classifies pixels

Thresholding classifies each pixel independently with a single comparison rule. For an 8-bit image with intensities from 0 to 255 and a threshold T, the output is 255 (or 1) where the input exceeds T and 0 elsewhere. Inverted variants swap the two labels, which is useful when objects are darker than the background, such as ink on paper.

The approach assumes that the intensity histogram is bimodal, with one peak for the background and one for the objects. When the two peaks are well separated, almost any threshold in the valley between them gives a clean result. When they overlap, no single value can separate the classes without errors.

The table below lists the common output rules supported by most image processing libraries.

RulePixel above thresholdPixel at or below threshold
BinaryMaximum value0
Binary inverted0Maximum value
TruncateSet to thresholdUnchanged
To zeroUnchanged0
To zero inverted0Unchanged
Thresholding output rules

Global, automatic and adaptive methods

Thresholding methods differ mainly in how they choose T and whether one value applies to the whole image. Global thresholding uses one value everywhere, automatic methods compute that value from the histogram, and adaptive thresholding computes a separate value for each neighborhood.

Otsu’s method is the best-known automatic technique. It tests every possible threshold and selects the one that maximizes the between-class variance, which is equivalent to minimizing the variance inside each class. It works well on bimodal histograms but fails when one class is very small or when lighting varies across the scene.

MethodHow T is chosenBest suited for
Fixed globalSet manuallyControlled industrial lighting
OtsuMaximizes between-class varianceClear bimodal histograms
TriangleGeometry of the histogram peakOne dominant class, small objects
Adaptive meanLocal mean minus a constantUneven illumination
Adaptive GaussianWeighted local mean minus a constantUneven light with fine detail
SauvolaLocal mean and standard deviationDegraded historical documents
Main thresholding methods compared

Adaptive methods depend on two parameters: the block size, which should be larger than the objects but smaller than lighting changes, and an offset constant that suppresses noise in flat regions. Typical block sizes range from 11 to 51 pixels at document resolution, and the value always depends on scale.

Thresholding compared with other segmentation approaches

Thresholding is the simplest form of image segmentation, and it differs from learned methods in what information it uses. It looks only at intensity, while semantic segmentation networks use shape, texture and context learned from labeled data.

TechniqueInformation usedTypical speedMain weakness
Image thresholdingPixel intensityMicroseconds to millisecondsFails under overlapping intensities
Edge detectionIntensity gradientsMillisecondsProduces boundaries, not regions
Background subtractionDifference from a reference frameMillisecondsNeeds a stable background
Semantic segmentationLearned visual featuresTens of milliseconds on GPUNeeds training data and compute
Thresholding and related techniques

In practice these techniques are combined. A thresholded mask is often followed by connected-component labeling to count and measure objects, and many deep learning pipelines threshold a probability map at 0.5 to produce the final segmentation mask.

Preparing images and avoiding common errors

Good results depend more on preprocessing and lighting than on the choice of algorithm. Converting color to grayscale, applying a light Gaussian blur to reduce noise and correcting uneven illumination usually help more than switching methods.

SymptomLikely causeFix
Speckled foregroundSensor noise near the thresholdBlur before thresholding, then morphological opening
Dark corners turn blackVignetting or uneven lightAdaptive method or background correction
Thin strokes break apartThreshold too strictLower the offset or apply closing
Objects merge togetherThreshold too looseRaise the threshold or use watershed splitting
Otsu picks a poor valueUnbalanced class sizesTriangle method or a restricted region
Typical problems and fixes

Morphological operations such as opening and closing are the standard cleanup after thresholding. A frequent mistake is tuning a fixed threshold on a few sample images and then deploying it where lighting conditions change, which silently degrades accuracy over time.

Where thresholding is used in practice

Thresholding remains common wherever speed, transparency and determinism matter more than handling complex scenes. It runs on low-power microcontrollers, needs no training data and gives results that engineers can explain pixel by pixel.

  • Document binarization before OCR and document layout analysis.
  • Industrial inspection of parts on a backlit conveyor.
  • Microscopy for counting cells or particles.
  • Medical imaging for isolating bone in CT scans by density range.
  • Post-processing of neural network probability maps and saliency maps.

Color images can also be thresholded per channel or in another color space. Thresholding the hue and saturation channels in HSV separates objects by color rather than brightness, which is a common technique for detecting colored markers, ripe fruit or warning lights. Multi-level thresholding extends the idea to three or more classes, for example separating background, soft tissue and bone in a single pass.

A non-technical view

Imagine sorting a pile of gray pebbles into two buckets: everything lighter than a chosen shade goes into one bucket, everything darker goes into the other. Image thresholding does the same with the dots that make up a picture, so the result is a pure black and white image.

The tricky part is picking the dividing shade. If the light is even, one shade works for the whole picture. If one corner is in shadow, the computer picks a different dividing shade for each small area, so every part of the picture is sorted fairly.

A concrete example

An archive is digitizing thousands of typed forms from the 1970s. The scans are 300 dpi grayscale, but the paper has yellowed unevenly and some pages have coffee stains. A first attempt with a fixed threshold of 128 turns stained areas solid black and erases faint typewriter letters near the margins.

The imaging engineer switches to Sauvola adaptive thresholding with a 31-pixel window, applies a light blur first and removes isolated specks with a small opening. Character recognition accuracy on a test set of 200 pages rises noticeably, and the number of pages that need manual correction drops by more than half, which shortens the project by several weeks.