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.
| Rule | Pixel above threshold | Pixel at or below threshold |
|---|---|---|
| Binary | Maximum value | 0 |
| Binary inverted | 0 | Maximum value |
| Truncate | Set to threshold | Unchanged |
| To zero | Unchanged | 0 |
| To zero inverted | 0 | Unchanged |
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.
| Method | How T is chosen | Best suited for |
|---|---|---|
| Fixed global | Set manually | Controlled industrial lighting |
| Otsu | Maximizes between-class variance | Clear bimodal histograms |
| Triangle | Geometry of the histogram peak | One dominant class, small objects |
| Adaptive mean | Local mean minus a constant | Uneven illumination |
| Adaptive Gaussian | Weighted local mean minus a constant | Uneven light with fine detail |
| Sauvola | Local mean and standard deviation | Degraded historical documents |
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.
| Technique | Information used | Typical speed | Main weakness |
|---|---|---|---|
| Image thresholding | Pixel intensity | Microseconds to milliseconds | Fails under overlapping intensities |
| Edge detection | Intensity gradients | Milliseconds | Produces boundaries, not regions |
| Background subtraction | Difference from a reference frame | Milliseconds | Needs a stable background |
| Semantic segmentation | Learned visual features | Tens of milliseconds on GPU | Needs training data and compute |
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.
| Symptom | Likely cause | Fix |
|---|---|---|
| Speckled foreground | Sensor noise near the threshold | Blur before thresholding, then morphological opening |
| Dark corners turn black | Vignetting or uneven light | Adaptive method or background correction |
| Thin strokes break apart | Threshold too strict | Lower the offset or apply closing |
| Objects merge together | Threshold too loose | Raise the threshold or use watershed splitting |
| Otsu picks a poor value | Unbalanced class sizes | Triangle method or a restricted region |
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.