Skip to content
AI360Xpert
Beta

Global Thresholding

Global thresholding applies one cutoff to the whole image, which works when lighting is even and the histogram holds two clear peaks.

One cutoff in the valley between two histogram peaks cleanly separates dark background from bright object.
One cutoff in the valley between two histogram peaks cleanly separates dark background from bright object.

Why Does This Exist?

When a backlit conveyor shows dark parts on a bright belt, or a scanned page shows ink on paper, one number separates everything: pick the valley between the two histogram humps and cut there. Global thresholding applies that single cutoff to every pixel, the fastest segmentation available and fully deterministic across frames. It is the first of the three cutoff strategies under thresholding: use it when lighting is even, adaptive when it gradients across the frame, Otsu when the valley's position is unknown.

This page covers picking the cutoff, the mean shortcut with a worked example, and the uneven-lighting failure that sends you to the sibling pages.

Think of It Like This

One pass mark for the whole class

A teacher sets a single pass mark of 50 for every student in every section. Grading is instant and perfectly consistent, and it is fair when every section sat the same paper under the same lights. But if one section wrote a harder paper in a darker room, the single mark fails half of them for lighting, not knowledge.

The analogy stops at bimodality. A pass mark works on any score spread, but a global cutoff only works when scores cluster into two humps with a valley between. One smeared hump means no single mark separates anything.

How It Actually Works

The single-cutoff rule

Fix TT once; every pixel above becomes foreground. In practice TT comes from inspecting the histogram valley (documents often split near 127 to 180), from a calibrated constant in controlled rigs, or from the image mean as a zero-tuning start. cv2.threshold(gray, T, 255, cv2.THRESH_BINARY) applies it in one pass.

Worked mean example on eight pixels [0,0,0,0,200,200,200,255][0, 0, 0, 0, 200, 200, 200, 255]: the sum is 855, the mean 855/8=106.875855 / 8 = 106.875, so T=107T = 107 after rounding. Four pixels sit above (the three 200s and the 255), four below: a clean 4-4 split from a statistic that cost one reduction. The mean works here because the two clusters balance; a mostly-background image would drag it into the background hump.

The bimodality requirement

Plot the histogram first. Two peaks with a deep valley means a global cut exists; one wide hump or a sloped plateau means it does not, and no TT will separate content from background. Controlled imaging (backlights, enclosures, flatbed scanners) manufactures bimodality physically, which is why factories prefer rigs over algorithms.

The uneven-lighting failure

A shadow gradient adds a ramp to every pixel, so one TT cuts foreground on the bright side and background on the dark side simultaneously. The symptom is a mask split along the lighting, not the content: half the object missing, half the background kept. The fix is structural, not parametric: flatten the field first (background subtraction, top-hat) or switch to per-pixel cutoffs on the adaptive page.

Code

import numpy as np
px = np.array([0, 0, 0, 0, 200, 200, 200, 255])T = int(round(px.mean()))  # zero-tuning cutoffprint(px.mean(), T)# -> 106.875 107print(np.where(px > T, 255, 0).tolist())# -> [0, 0, 0, 0, 255, 255, 255, 255]

Watch Out For

Mean dragged by class imbalance

The mean sits between the humps only when pixel counts roughly balance. In a mostly-background frame it sinks into the background peak and foreground floods. The symptom is a mask that is nearly all white or all black. Check the histogram's two-peak shape before trusting any moment-based cutoff.

Tuning T against the lighting

When a shadow crosses the frame, no single TT works and sweeping it just trades one half's errors for the other's. The symptom is a threshold that "needs retuning every hour" as the sun moves. Stop tuning and change strategy: flatten illumination or go adaptive.

The Quick Version

  • One cutoff TT for all pixels: fastest, deterministic, needs even lighting.
  • The histogram must show two peaks with a valley; place TT in the valley.
  • Mean shortcut: [0,0,0,0,200,200,200,255][0,0,0,0,200,200,200,255] averages 106.875, cutting 4-4.
  • Imbalanced classes drag moment-based cutoffs into the majority hump.
  • Shadows and gradients defeat every global TT; flatten light or go adaptive.