A robot inspection program can select a threshold from an image histogram without knowing which pixels belong to the intended part. Otsu's method supplies a particular gray-level partition objective. It does not supply object labels, spatial connectivity, or a physical definition of a defect. An original eight-pixel example separates the arithmetic choice from those additional meanings.

Concept illustration: The site-library robot above a metal-parts bin with a blue sensing pattern provides vision context. It does not show the hypothetical image, an executed thresholding pipeline, or a validated inspection result.

Define the partition before scoring it

Use grayscale values 0, 2, 6, and 8 with counts 3, 1, 2, and 2. The total count is eight, and the weighted sum is 30, giving an overall mean of 3.75. These dimensionless values are invented codes, rather than calibrated light measurements.

For this article, the lower class contains values less than or equal to threshold t. The upper class contains values greater than t. Let wL and wH be their fractions of the total, and let mL and mH be their class means. Require both classes to be nonempty.

The between-class score is B = wL wH (mL - mH) squared. This expression rewards separated class means while retaining their class weights. OpenCV documents the equivalent minimization of weighted within-class variance. Scikit-image separately documents an Otsu threshold operation and its histogram inputs. Neither reference establishes what the bright class means in an unspecified robot scene.

Score the first possible split

At t = 0, the lower class contains the three zero-valued pixels. Thus wL = 3/8 and mL = 0. The upper class contains [2, 6, 6, 8, 8], whose mean is 30/5 = 6, and wH = 5/8.

The between-class score becomes (3/8)(5/8)(0 - 6) squared = 8.4375. A large mean separation alone is not the complete score; the class fractions also enter. Comparing only the gap between means would omit one part of the declared objective.

Thresholds between zero and two yield the same partition for these observed values. The score cannot distinguish such thresholds because no pixel changes class. The numerical threshold and the induced partition consequently need separate descriptions when a histogram has unoccupied levels.

Score the middle split

At t = 2, the lower class is [0, 0, 0, 2]. Its mean is 0.5 and its fraction is one-half. The upper class [6, 6, 8, 8] has mean 7 and the same fraction.

The score is one-quarter times 6.5 squared, or 10.5625. It exceeds the previous score. At t = 6, the lower class becomes [0, 0, 0, 2, 6, 6], with mean 14/6. The upper class is [8, 8], with mean 8. Its score is (6/8)(2/8)(8 - 14/6) squared, approximately 6.020833.

The split after value two therefore has the largest score of the three distinct nonempty partitions. Under the declared comparison, values 6 and 8 belong to the upper class. This conclusion is an exhaustive calculation for the constructed histogram, not a reported output from an OpenCV or scikit-image execution.

Check the variance decomposition

The overall second moment is (1 times 2 squared + 2 times 6 squared + 2 times 8 squared)/8 = 25.5. Subtracting the overall mean squared gives total variance 25.5 - 3.75 squared = 11.4375.

At the winning split, the lower-class population variance is 0.75 and the upper-class population variance is 1. Their equally weighted within-class variance is 0.875. Adding that to between-class variance 10.5625 recovers 11.4375.

For a fixed histogram, the total variance does not change as the threshold moves. Maximizing its between-class part therefore minimizes its within-class part. The identity explains the two formulations without requiring a separate optimization rule for each. These are population moments of the finite pixel ledger; dividing by count minus one would change the calculation.

Rearrange the same pixels

Place the eight pixels into two rows: [0, 0, 0, 2] and [6, 6, 8, 8]. With upper-class pixels marked one, the mask is [0, 0, 0, 0] followed by [1, 1, 1, 1].

Now arrange them as [0, 6, 0, 8] and [0, 6, 2, 8]. The histogram is unchanged. The winning partition and score are unchanged, but the mask becomes [0, 1, 0, 1] in both rows. The first layout has one full bright row; the second has two separated bright columns.

A histogram objective discards these spatial arrangements. It cannot choose between them as a preferred shape because its input statistics contain no such distinction. A later region operation would be answering an additional question.

An exact positive affine rescaling also supplies a check. Replace every original code x by 2x + 10 without clipping. Class means undergo the same transformation, their difference doubles, and each between-class score quadruples. Class fractions stay fixed, so the winning pixel partition stays fixed even though the numerical threshold scale changes.

Keep threshold details in the record

For these values, any threshold t satisfying 2 <= t < 6 induces the winning partition under the stated less-than-or-equal convention. A specific API may select one numerical representative. This article makes no assertion about its tie-breaking implementation or floating-point binning.

The bright class can be called foreground only as an application choice. A dark part on a light background might need the other class, and neither name proves correct physical segmentation. Preserve the grayscale preparation, histogram bin interpretation, comparison convention, selected partition, and any later spatial rule. The score then has a precise interpretation without being mistaken for an object-recognition result.

Sources checked October 24, 2026

OpenCV4.13.0, Image Thresholding: https://docs.opencv.org/4.13.0/d7/d4d/tutorial_py_thresholding.html

Scikit-image0.25.2, threshold_otsu: https://scikit-image.org/docs/0.25.x/api/skimage.filters.html