License YOLO
Back to Ultralytics Glossary

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.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning