License YOLO
Back to Ultralytics Glossary

Otsu's Method

Otsu's method automatically selects a grayscale threshold for binary segmentation by maximizing the statistical separation between dark and bright pixels.

Otsu's method automatically selects a brightness cutoff from a grayscale image's intensity histogram, which counts how many pixels have each gray level. Also called Otsu thresholding, it divides the pixels into a darker group and a brighter group while minimizing the overall spread of intensities within those groups. Applying the cutoff produces a binary mask, a basic form of image segmentation.

Nobuyuki Otsu introduced the algorithm in his 1979 paper "A Threshold Selection Method from Gray-Level Histograms". This classical computer vision technique requires no labeled examples or model training. It is a form of global thresholding: it calculates a cutoff for each input image and applies it to every pixel in that image. Fixed thresholding uses a preset cutoff across images.

How Otsu's Method Chooses a Threshold#

At each candidate cutoff, pixels at or below the cutoff go into one group and brighter pixels go into the other. For splits with two nonempty groups, the algorithm uses the histogram to find each group's mean intensity and share of the image. The between-class variance is the squared difference between the two means multiplied by the two pixel shares. Otsu's method picks the cutoff that maximizes this score. Because the image's total intensity variance stays fixed, this also minimizes the within-class variance, the weighted spread of intensities inside the two groups.

A histogram with two well-separated peaks, called a bimodal histogram, often yields a clean split. Otsu's method can also score a histogram without distinct peaks because it evaluates candidate cutoffs directly. The scores depend only on how often each gray level occurs: shuffling the pixels changes the shape of the mask but leaves the threshold unchanged.

Otsu Thresholding in Python and MATLAB#

OpenCV's threshold function returns both the selected cutoff and the binary image. This example uses the grayscale page bundled with scikit-image.

import cv2
from skimage import data

page = data.page()

# The Otsu flag makes OpenCV choose the cutoff automatically.
mode = cv2.THRESH_BINARY | cv2.THRESH_OTSU
cutoff, binary = cv2.threshold(page, 0, 255, mode)

cv2.imwrite("page_otsu.png", binary)
print(f"Otsu cutoff: {cutoff:.0f}")

The selected cutoff is 157. In page_otsu.png, intensities above 157 become white and all others become black. To make dark ink white in the foreground mask, use cv2.THRESH_BINARY_INV with the same Otsu flag.

In MATLAB, graythresh returns an Otsu cutoff normalized to the range 0 to 1. imbinarize(I, level) applies that cutoff to the grayscale image I.

Where Otsu Thresholding Fits in Computer Vision#

In document processing, Tesseract uses Otsu binarization to separate ink from paper before optical character recognition (OCR). In industrial visual inspection, the Signal Profiler app offers Otsu thresholding as an optional preprocessing mode in a pipeline that uses an Ultralytics YOLO26 detector to locate aluminum cross-sections and a classifier to identify their profile types.

In microscopy, ImageJ's Nucleus Counter offers Otsu thresholding before counting and measuring nuclei. Touching nuclei can merge into one connected region in the binary mask and be counted as one. The plugin offers watershed separation after thresholding to split touching regions before counting. A learned instance segmentation model can predict a separate mask for each object.

When a Single Cutoff Breaks Down#

In the scikit-image page example, the paper on the left is dark enough to fall into the same intensity group as the ink, obscuring text in the global mask. Local Otsu scores a neighborhood histogram around each pixel, so the cutoff can vary as the paper gets lighter across the page. Adaptive mean and adaptive Gaussian thresholding also vary the cutoff spatially, using a local average or a Gaussian-weighted average.

Noise spreads gray levels across the histogram. A Gaussian blur before thresholding can suppress speckle, but it can also weaken thin strokes. When the image contains more than two intensity groups, Multi-Otsu extends the same variance criterion to several global cutoffs. Two cutoffs create three classes, with the middle class containing intensities above the lower cutoff and at or below the upper one.

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