A robot vision pipeline reports five connected regions in a binary mask. Another pipeline reports three from the same foreground pixels. Before assuming that one threshold or detector changed, examine the definition of a neighboring pixel.
Four-connected and eight-connected labeling answer different graph-connectivity questions. A diagonal contact can leave two regions separate under one rule and join them under the other. Neither count alone establishes how many physical parts exist in the scene.
This article uses an original small mask to make the difference explicit. It is a mathematical image-analysis example, not an inspection result, software benchmark, or physical object-count test.
Define foreground and neighborhood separately
Let a binary mask contain ones for foreground and zeros for background. Keep that mask fixed initially. Labeling assigns a component to each foreground pixel according to paths through allowed foreground neighbors.
With 4-connectivity, a pixel can connect directly to its neighbors above, below, left, and right. A diagonal foreground pixel is not a direct neighbor.
With 8-connectivity, those four directions remain and the four diagonal directions are added. Connectivity is transitive: if one allowed foreground step reaches a second pixel, and another reaches a third, all three belong to the same component.
SciPy's ndimage.label reference, version 1.16.2, documents a structuring element that specifies connections and a cross-shaped default for two-dimensional arrays. The scikit-image 0.25.2 label reference separately describes orthogonal-hop connectivity. These official interfaces show why the neighborhood must be part of a labeling definition. Their examples are not reused below.
One mask, seven foreground pixels
Number rows and columns starting at one. Consider this five-by-five mask, written one row per line:
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
0 0 0 0 0
1 0 0 0 1
There are seven foreground pixels. Row one contains two adjacent pixels. Row two contains one pixel at column three. Row three contains an adjacent pair at columns four and five. The last row contains isolated pixels at columns one and five.
Under 4-connectivity, the row-one pair forms one component. The row-two pixel is separate because its closest foreground connections to the neighboring rows are diagonal. The row-three pair forms another component. Each last-row pixel forms its own component.
The total is five components, with areas of two, one, two, one, and one pixels. A possible label assignment is:
1 1 0 0 0
0 0 2 0 0
0 0 0 3 3
0 0 0 0 0
4 0 0 0 5
The numeric labels identify membership here; their particular order is not a physical property of the scene.
Adding diagonal graph edges changes the grouping
Under 8-connectivity, row-one column two connects diagonally to row-two column three. That pixel connects diagonally to row-three column four, which connects horizontally to column five.
Those five pixels therefore form one component, even though no single pixel directly touches every other pixel in the group. A chain of allowed steps is sufficient.
The two last-row pixels remain isolated. The all-zero fourth row prevents them from reaching the earlier foreground under either neighborhood rule. The eight-connected count is three, with component areas five, one, and one.
A possible labeling is:
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
0 0 0 0 0
2 0 0 0 3
No foreground pixel was added or removed between these two labelings. Only the graph's permitted neighbor connections changed.
A monotonic relationship follows
For a fixed mask, every path allowed by 4-connectivity is also allowed by 8-connectivity. Adding diagonal links can merge components but cannot split a previously connected component. The eight-connected count is therefore no greater than the four-connected count for this same two-dimensional foreground definition.
That statement does not apply to two arbitrary pipelines whose thresholds, masks, crops, or other processing differ. The fixed-mask condition is essential.
It also does not say that eight-connected labeling is universally preferable. The neighborhood rule should describe the intended digital interpretation rather than be selected because it produces a more convenient count.
Downstream area filtering can amplify the difference
Suppose a hypothetical next operation retains components with at least two foreground pixels. This is an invented rule, not an application acceptance threshold.
Under 4-connectivity, the two pairs survive and the three singletons do not. Two components remain, containing four retained pixels in total.
Under 8-connectivity, the five-pixel chain survives and the two singletons do not. One component remains, containing five retained pixels.
The same area threshold has consequently produced different retained masks because the preceding grouping differed. A count comparison after filtering cannot be interpreted fully without the neighborhood used before filtering.
Changing pixels is a separate operation
Now modify the original mask by turning row-two column two from zero to one. This new foreground pixel provides an orthogonal bridge between the row-one pair and row-two column three.
The four-connected component count falls from five to four. The eight-connected count remains three because those pixels were already joined through diagonal connections. Foreground area has increased from seven to eight.
This differs from changing connectivity alone. One operation changes the mask's data; the other changes how an unchanged mask is grouped. Confusing them can hide where an image pipeline's behavior actually changed.
A connected region is not automatically a physical part
Diagonal contact in a mask can represent a sampled thin feature, two nearby objects, a segmentation artifact, or another scene structure. Connectivity labels alone do not distinguish those interpretations.
One physical object may produce several disconnected foreground regions. Several physical objects may also appear as one connected region. Assigning physical identity requires information beyond the component graph, such as the image formation and segmentation assumptions.
Library semantics matter as well. In the cited SciPy interface, nonzero values are foreground. The cited scikit-image interface labels neighboring pixels of the same value and uses a specified background. For a genuine zero-and-one mask, that distinction does not change the examples. An arbitrary multi-valued label image need not behave like the binary case.
A reproducible robot-vision count therefore identifies the binary mask, foreground convention, neighborhood, and later component filtering. With those conditions attached, “five regions” or “three regions” becomes a precise statement about an image graph rather than an unsupported claim about physical objects.
Sources checked October 23, 2026
SciPy, ndimage.label, official version 1.16.2 documentation:
https://docs.scipy.org/doc/scipy-1.16.2/reference/generated/scipy.ndimage.label.html
scikit-image, measure.label, official 0.25.x documentation displayed as version 0.25.2:
https://scikit-image.org/docs/0.25.x/api/skimage.measure.html#skimage.measure.label
All grids and component calculations are original hypothetical examples. No source example arrays, figures, or executed software results are reproduced.
Illustration note: The site-library concept image shows a robot above metal parts with a blue sensing pattern. It does not show a validated segmentation mask, measured component counts, or verified physical-part detection.