A robot vision pipeline may convert an image into a binary mask before measuring a part. The mask marks the detected part as foreground and everything else as background. Gaps inside a detected silhouette can come from glare or segmentation errors, but they can also represent features that a downstream inspection needs to measure. The way an image is cropped can change which of those gaps a hole-filling operation sees as enclosed.
SciPy's binary_fill_holes operation works from the boundary of the array it receives. In effect, it marks background that can be reached from the outside without crossing foreground under the selected neighborhood. Background left unmarked is enclosed and becomes foreground. Changing the boundary or the neighborhood can change the result even when every retained pixel is identical.
Create a ring with a known interior
The following fixture uses a nine by nine Boolean array with a five by five foreground square. Its central three by three region is background. The foreground ring fully encloses those nine interior pixels. The crop begins at row three, so its first row crosses the central region and removes the top portion of the foreground ring. Python slices exclude their upper endpoints, which is why each slice can be read directly against the defined coordinates.
import numpy as np
from scipy.ndimage import binary_fill_holes, generate_binary_structure
mask = np.zeros((9, 9), dtype=bool)
mask[2:7, 2:7] = True
mask[3:6, 3:6] = False
crop = np.s_[3:8, 1:8]
filled_whole = binary_fill_holes(mask)
fill_then_crop = filled_whole[crop]
crop_then_fill = binary_fill_holes(mask[crop])
assert np.count_nonzero(filled_whole & ~mask) == 9
assert np.array_equal(crop_then_fill, mask[crop])
assert np.count_nonzero(fill_then_crop & ~crop_then_fill) == 9
With SciPy 1.18.1 and NumPy 2.5.2, processing the full image filled nine pixels. Filling after cropping added zero. Every pixel in the original three by three opening remained inside the crop, yet the opening now touched the top edge. From that edge the background traversal reached the entire region. Taking the crop after the full-image fill retained the nine newly filled pixels.
Checking the whole resulting array is useful here. A measurement that reports only apparent area might expose a difference without showing where it came from. Saving a mask of changed pixels can show that the difference is concentrated at a crop boundary. When the reported area feeds another rule, carry that location information back to the associated image and part record.
Change the background neighborhood
The same ring also gives a small test of connectivity. Clear the foreground pixel at row two and column two. That corner touches the inner background diagonally. The adjacent pixels to its right and below still belong to the foreground ring.
diagonal_opening = mask.copy()
diagonal_opening[2, 2] = False
four_neighbors = generate_binary_structure(2, 1)
eight_neighbors = generate_binary_structure(2, 2)
fill_four = binary_fill_holes(diagonal_opening, structure=four_neighbors)
fill_eight = binary_fill_holes(diagonal_opening, structure=eight_neighbors)
assert np.count_nonzero(fill_four & ~diagonal_opening) == 9
assert np.array_equal(fill_eight, diagonal_opening)
The four-neighbor traversal reaches background through shared edges. It cannot pass from the removed corner to the interior, so the nine pixels fill. The eight-neighbor traversal can pass through a shared corner, reach the interior, and leave it as background. Saving the chosen connectivity with the result makes this difference visible to anyone reproducing the measurement. An operation that fills regions according to their pixel area introduces a separate size rule, as the scikit-image morphology documentation illustrates.
Choose where the domain should end
If the crop exists for display or a later detector, processing the full mask first can retain the connectivity of the original image. If the crop defines the physical region used by the measurement, its boundary may be the intended one. Save the crop origin, extent, and operation order so those choices can be reconstructed. When working from tiles, a larger tile context can preserve some surrounding connectivity, but the result still depends on the boundary of the larger context. A path to the original image edge may travel beyond a fixed margin.
Keep separate records for the original binary mask, the filled mask, and the pixels that changed when those distinctions matter for review. In a pipeline that measures machined apertures, the foreground surface and the openings can both be meaningful. A fill can be suitable for an outer-silhouette measurement while a subsequent aperture analysis still needs the original interior background. Linking both outputs to one source frame avoids reconstructing a feature after it has been overwritten.
A small regression set can include an enclosed opening, an edge-connected opening, and a diagonal connection. For each example, verify the processed pixel locations as well as their count. Repeat those checks with the same crop offsets and connectivity used by the measurement application. If a preprocessing stage resizes or rotates the mask, run the regression after that stage as well, because interpolation and thresholding can change the pixel paths being tested.
When comparing a deployed mask with a stored reference, align both arrays in original image coordinates before taking the difference. A raw comparison between two equal-sized crops can attribute a shifted crop to a change in the detector. Record how the crop was chosen as well as its coordinates. A moving crop centered on a detected part may cut through an opening on some frames and surround it on others. A targeted replay can move that boundary across one fixed synthetic opening and check the result at each step.
The AI-generated concept image shows a crop boundary crossing a square opening in an inspection plate. The exact pixel counts and connectivity results in this article come from the synthetic Boolean arrays.
Sources checked November 28, 2026. SciPy binary_fill_holes documentation at https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.binary_fill_holes.html and scikit-image morphology documentation at https://scikit-image.org/docs/stable/api/skimage.morphology.html.