Image Thresholding
Image thresholding turns pixel values into masks. Global, Otsu, and adaptive methods differ in how they choose the cutoff and handle uneven lighting.
Image thresholding assigns pixels to groups by comparing their values with one or more cutoffs. In its most common form, binary thresholding, each pixel becomes either foreground or background. The result is a binary mask: a map of which pixels passed the test. This makes thresholding a simple form of image segmentation that works without model training.
How Image Thresholding Works#
In standard binary thresholding, grayscale values strictly above the cutoff become 1, and values at or below it become 0. An 8-bit mask often uses 255 instead of 1 so that foreground pixels display as white. Inverse binary thresholding swaps the output values, which makes dark ink white in a foreground mask. The cutoff uses the input scale: 128 is near the midpoint for an 8-bit grayscale image, while 0.5 is the midpoint for values normalized to 0–1.
Grayscale is one input choice. A range test in a color space such as HSV can isolate pixels by hue and saturation when the target and background have similar brightness. Multilevel thresholding divides a single channel into more than two bands: two cutoffs produce three intensity groups. Each group can be stored as a separate integer label in a single image.
How to Choose a Threshold Value#
A global threshold uses one cutoff everywhere in the image. A fixed global cutoff works when lighting is stable and the foreground and background occupy distinct intensity ranges. A histogram counts how many pixels have each intensity; a valley between two well-separated peaks gives a starting cutoff. To calculate a cutoff for each image, Otsu's method selects the split that maximizes the squared difference between the two groups' mean intensities multiplied by both groups' pixel proportions. Nobuyuki Otsu introduced this criterion in his 1979 threshold selection paper.
Adaptive thresholding calculates a cutoff for each pixel from its neighborhood. Adaptive mean uses the neighborhood average minus an offset. Adaptive Gaussian gives nearby pixels more weight before subtracting the offset. In a scan with uneven lighting, paper in shadow can be as dark as ink in brighter areas. A local cutoff separates ink and paper by their contrast within each neighborhood. When this contrast is weak, faint strokes can disappear from the mask.
flowchart TD
A{"Does one cutoff separate foreground and background?"}
A -->|Yes| C{"Is the same cutoff reliable across images?"}
A -->|No| B{"Is local foreground contrast clear?"}
C -->|Yes| F["Fixed global threshold"]
C -->|No| G["Per-image global threshold"]
B -->|Yes| D["Adaptive threshold"]
B -->|No| E["Lighting correction or learned segmentation"]How to Threshold an Image in Python, MATLAB, or Photoshop#
In Python, OpenCV can apply a fixed cutoff with cv2.threshold or calculate local cutoffs with cv2.adaptiveThreshold. This example uses the bundled scikit-image page scan and marks dark pixels as foreground in both masks.
import cv2
from skimage import data
page = data.page()
# Set dark ink to white foreground in both masks.
mode = cv2.THRESH_BINARY_INV
_, global_mask = cv2.threshold(page, 128, 255, mode)
local_mask = cv2.adaptiveThreshold(page, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, mode, 31, 10)
cv2.imwrite("global_mask.png", global_mask)
cv2.imwrite("adaptive_mask.png", local_mask)In global_mask.png, parts of the shadowed paper become white alongside the ink. In adaptive_mask.png, each cutoff comes from a 31-by-31 neighborhood, so the text stays distinct across more of the page.
In MATLAB, imbinarize(I) applies a global Otsu cutoff to a grayscale image. imbinarize(I, 0.5) uses a fixed threshold on the normalized 0–1 scale, and imbinarize(I, "adaptive", "ForegroundPolarity", "dark") calculates local cutoffs for dark objects. In Photoshop, a Threshold adjustment layer applies a global cutoff; its slider sets the level for the black-and-white preview.
Where Thresholding Fits in Computer Vision#
In document processing, Tesseract performs internal binarization on grayscale inputs before optical character recognition. Its local methods include Adaptive Otsu and Sauvola, which account for changes in background tone.
In industrial visual inspection, NI Vision uses thresholding to isolate connected regions for measurement, such as flaws on silicon wafers. Touching foreground regions can merge into one component, reducing the count and combining their areas.
Learned instance segmentation predicts a separate mask for each object and can apply a cutoff to model scores during postprocessing. In Ultralytics YOLO26 instance segmentation, mask decoding thresholds the predicted mask logits, or raw scores: a pixel inside the object's bounding box becomes foreground only when its mask logit is greater than zero.










