A robot-vision review tool may crop a region around one labeled object before measuring it. The crop can be geometrically correct and still contain pixels assigned to another label. Treating every pixel inside the rectangle as a member of the requested region changes the measurement without requiring any error in the bounding-box calculation.
SciPy's find_objects provides slice tuples for labeled regions. The important distinction is between a rectangle that contains a label and a Boolean mask that identifies exactly its pixels. This article traces both representations through an original small array, including missing label numbers and an independent sum check.
Start with explicit label identities
Use this four-row, six-column integer array, with zero-based coordinates:
0 2 2 0 0 0
0 2 7 0 0 0
0 2 7 0 0 0
0 0 7 0 0 0
Zero is background. The positive identifiers present are 2 and 7; labels 1,3,4,5,6 are absent. The array is synthetic and already labeled. No image threshold, connected-component routine, or physical part detector is executed to produce it.
The find_objects reference states that zero labels are ignored, that the result contains minimal enclosing slice tuples, and that label l corresponds to list position l-1. Missing labels have None in their slots [1]. The distinction between list length and the number of present labels follows immediately in this example.
The returned list has seven entries, but only two are slice tuples. Entry 1 describes label 2; entry 6 describes label 7. The remaining five entries are None. Reporting seven detected objects because the list length is seven would count missing slots as objects.
Examine the rectangle for label 2
Label 2 occupies rows zero through two and columns one through two. Its bounding tuple is (slice(0,3),slice(1,3)). Slice endpoints here are exclusive, so the cropped rectangle has shape 3-by-2. Applying the tuple to the label array produces:
2 2
2 7
2 7
There are six cells in the rectangle, of which four belong to label 2 and two belong to label 7. The crop is still the smallest axis-aligned rectangle enclosing every occurrence of label 2. Removing either column or either extreme row would discard one of its pixels.
The rectangle's correctness therefore does not justify calling its six-cell area the area of label 2. It answers an enclosure question. Pixel membership requires an additional equality comparison against the requested label.
import numpy as np
from scipy.ndimage import find_objects, sum_labels
labels = np.array([
[0, 2, 2, 0, 0, 0],
[0, 2, 7, 0, 0, 0],
[0, 2, 7, 0, 0, 0],
[0, 0, 7, 0, 0, 0]])
boxes = find_objects(labels)
roi = boxes[2 - 1]
membership = labels[roi] == 2
assert len(boxes) == 7
assert sum(box is not None for box in boxes) == 2
assert labels[roi].size == 6
assert np.count_nonzero(membership) == 4
Measure values from a separate aligned array
Define an equally shaped value array by placing integers one through 24 in row order. These are arbitrary dimensionless values for checking the arithmetic, not measured pixel intensities. The value crop for label 2's rectangle is [[2,3],[8,9],[14,15]].
Adding every value in that rectangle gives 51. Applying membership first selects 2,3,8,14, whose sum is 27. The difference of 24 comes from the two label-7 values, 9 and 15, that the rectangle also contains. An unqualified rectangle sum would therefore combine two label identities.
SciPy's sum_labels reference describes summing a value array over specified labels using an explicit index argument [2]. Use that separate interface as a check on the crop-plus-mask calculation:
data = np.arange(1, 25).reshape(4, 6)
crop_sum = data[roi].sum()
selected_sum = data[roi][membership].sum()
reference = sum_labels(data, labels, index=[2, 7])
assert crop_sum == 51
assert selected_sum == 27
np.testing.assert_array_equal(reference, [27, 45])
All checks passed locally with SciPy 1.18.1, NumPy 2.5.2, and Python 3.14.7. The official pages consulted were labeled SciPy 1.18.0. The label-7 sum of 45 includes its third pixel, value 21, outside label 2's rectangle. The two interfaces therefore agree about membership without pretending the rectangles partition all pixels.
Handle missing entries before indexing
For this array, boxes[0] is None because label 1 is absent. Passing that None directly as a NumPy index does not produce an empty object crop. In the executed fixture, labels[None] has shape (1,4,6): it adds an axis to the whole array. Check for None before applying a purported region slice.
A practical iteration keeps the label number attached to each slot:
for label_id, box in enumerate(boxes, start=1):
if box is None:
continue
exact_membership = labels[box] == label_id
If an application first removes every None and then renumbers the shortened list from one, the two surviving boxes would be mislabeled as 1 and 2. The actual identities remain 2 and 7. Filtering missing entries should not silently rewrite the association between a slice and its label.
An enclosing box does not perform component analysis
As a separate perturbation, copy the label array and add one more label-2 pixel at coordinate (3,5). It is separated from the existing label-2 region, yet find_objects still returns one box for identifier 2. That box expands to rows 0:4 and columns 1:6, enclosing 20 cells while only five carry label 2.
The result groups occurrences by their existing identifier; it does not assign fresh connected-component identities to disconnected pieces. Whether those pieces represent one physical item, multiple items, or a labeling mistake requires information outside this slice calculation.
A useful crop record therefore retains the label ID, source label-array identity, slice tuple, exact membership mask, and the aligned value-array identity. Report rectangle dimensions separately from member-pixel counts and masked measurements. With those distinctions preserved, a crop can accelerate or simplify a review without changing which pixels the requested label actually owns.
Sources
[1] SciPy, find_objects: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.find_objects.html
[2] SciPy, sum_labels: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.sum_labels.html