A binary robot-vision mask can contain two bright pixels that touch only at a corner. Whether those pixels belong to one object depends on the connectivity rule supplied to the labeling operation. SciPy's default for a two-dimensional image connects the center pixel to its four edge neighbors; diagonal neighbors are excluded. A full eight-neighbor structure produces a different object count without changing a single input pixel.
The fixture here is deliberately small and synthetic. It demonstrates a segmentation contract, not a claim about a camera, a gripper, or a deployed inspection line. The important decision is how adjacency is defined before centroids, bounding boxes, or object IDs are consumed downstream.
Start with a diagonal pair
Consider this mask, where 1 means foreground and 0 means background:
mask = np.array([
[1, 0, 0],
[0, 1, 0],
[0, 0, 0],
], dtype=np.uint8)
The two foreground pixels touch diagonally. Calling `scipy.ndimage.label(mask)` without a structure uses an automatically generated two-dimensional structure with squared connectivity one:
from scipy import ndimage
labels4, count4 = ndimage.label(mask)
The default structure is the cross:
[[0, 1, 0],
[1, 1, 1],
[0, 1, 0]]
Under that rule, the label matrix is:
[[1, 0, 0],
[0, 2, 0],
[0, 0, 0]]
and `count4` is 2. Each pixel is a separate feature because no edge-sharing path connects them.
Make diagonal policy explicit
To treat diagonal contact as connected, construct a structure with connectivity two:
diagonal = ndimage.generate_binary_structure(2, 2)
labels8, count8 = ndimage.label(mask, structure=diagonal)
For rank two, connectivity ranges from one, which excludes diagonals, to two, which includes all positions in the three-by-three neighborhood. The resulting structure is all true:
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]
Now the label matrix is:
[[1, 0, 0],
[0, 1, 0],
[0, 0, 0]]
and `count8` is 1. The labels agree because the diagonal pixels are members of one connected component. The values 2 and 1 are not confidence scores; they are counts under two different adjacency contracts.
Why a one-pixel corner can change a robot decision
Suppose a downstream stage rejects components with fewer than two pixels, or sends each label to a separate pose estimator. With four-connectivity, the fixture yields two one-pixel components. With eight-connectivity, it yields one two-pixel component. A component-size threshold, crop, or centroid will therefore change even though the input mask is identical.
The choice should follow the physical meaning of the mask. A laser stripe that is continuous only through diagonal raster cells may need eight-connectivity. Separate fiducial marks that happen to touch at a corner may require four-connectivity or a preceding morphological separation step. The library cannot infer that meaning from the array.
Check two-way connections
SciPy requires the structure to be centrosymmetric so that a connection is mutual. A non-centrosymmetric pattern could say that pixel A reaches pixel B while B cannot reach A, which is not a valid undirected component relation. `generate_binary_structure` produces a valid symmetric pattern for the stated rank and connectivity. If a custom structure is used, test that it equals its reversal before labeling.
Keep labels and features tied to the same rule
`label` returns an integer array and the number of objects. If an output array is supplied, it is updated in place and must be able to store the largest label. For a reproducible vision record, save the structure, connectivity, input shape, foreground convention, and label count alongside any centroids or bounding boxes. Recomputing object properties later with a different connectivity can silently change object identity.
An auditable fixture can assert the complete distinction:
assert count4 == 2
assert count8 == 1
assert labels4[0, 0] != labels4[1, 1]
assert labels8[0, 0] == labels8[1, 1] == 1
It can also verify that zero is background and nonzero input values are treated as foreground. In a real pipeline, retain the original mask before any relabeling so a reviewer can reproduce the decision.
Connectivity is part of the model, not a cosmetic parameter. Four-neighbor and eight-neighbor labeling answer different questions about diagonal contact. Decide that policy before computing robot poses, record it with the article's mask or image metadata, and test a corner-touching fixture. That small check prevents a one-pixel geometry detail from turning into an unexplained object split or merge.
The same rule matters when a mask is tiled or downsampled. A diagonal connection in a low-resolution tile may represent a continuous edge in the original camera image, while two marks that touch after resizing may have been separate at full resolution. Store the preprocessing scale with the connectivity choice, and repeat the corner fixture after every resize or threshold change. If the pipeline later merges neighboring tiles, label IDs from each tile cannot be compared directly; the seam policy must decide whether touching components are reconciled. These are data-contract decisions, not properties that `label` can recover after the fact.
For review, show both the binary input and the labeled output. A reviewer should be able to see that zero remains background, that labels are positive integers, and that the reported count matches the number of unique nonzero labels. A compact record can include `np.unique(labels[labels > 0])`, the structure matrix, and the exact SciPy version. This makes a changed object count traceable to a deliberate connectivity or preprocessing change rather than to a mysterious vision regression.
Sources
[1] SciPy, scipy.ndimage.label: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html
[2] SciPy, scipy.ndimage.generate_binary_structure: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.generate_binary_structure.html