An inspection camera can return a correct class mask while the reject logic still counts the wrong number of defect regions. One way this happens is when the mask stores a separate value for each defect class, but the component-labeling step treats every nonzero pixel as the same foreground. A scratch and a residue patch that touch then become one region. An area filter downstream may act on their combined footprint even though the inspection specification assigns a separate threshold to each class.

The same mask can represent two legitimate questions. A maintenance report might track the total footprint of anything that needs attention. A reject rule may instead ask how many pixels belong to each connected scratch, applying a different threshold to residue. Choose the question before creating regions. An overlay that displays each source class in a different color does not guarantee that the component routine kept those classes separate.

Hold connectivity constant

SciPy's ndimage.label treats zero as background and every nonzero input as foreground. Its default two-dimensional structure connects pixels through a shared edge. The scikit-image measure.label function labels connected pixels that share an input value. scikit-image uses full connectivity by default, so specify connectivity=1 to compare the two libraries using shared edges in two dimensions. Holding adjacency constant isolates the effect of class values.

Here the first row contains two scratch pixels and one residue pixel. The lower row contains a second, isolated residue pixel. Class IDs 1 and 2 have the meanings defined by this fixture. An area threshold of three pixels is deliberately chosen to separate the combined area from both per-class areas.

import numpy as np

from scipy import ndimage

from skimage import measure

mask = np.array([[1, 1, 2, 0],

[0, 0, 0, 0],

[0, 0, 2, 0]])

structure = ndimage.generate_binary_structure(2, 1)

foreground, n_foreground = ndimage.label(mask, structure=structure)

by_value, n_by_value = measure.label(

mask, background=0, return_num=True, connectivity=1

)

def region_sizes(regions):

return sorted(np.bincount(regions.ravel())[1:].tolist())

assert (n_foreground, region_sizes(foreground)) == (2, [1, 3])

assert (n_by_value, region_sizes(by_value)) == (3, [1, 1, 2])

scratch, n_scratch = ndimage.label(mask == 1, structure=structure)

residue, n_residue = ndimage.label(mask == 2, structure=structure)

assert (n_scratch, region_sizes(scratch)) == (1, [2])

assert (n_residue, region_sizes(residue)) == (2, [1, 1])

An area gate of at least three pixels would flag the top foreground region. None of the individual class regions meets that same threshold. In a production review, the correct verdict depends on the agreed acceptance rule and class-specific limits. This small fixture exposes a disagreement in how pixels reach that rule. It says nothing about whether the camera detected the underlying marks correctly.

Carry the class into the report

Labeling an integer mask by value creates new component identifiers. Those identifiers enumerate regions and can differ from the semantic class IDs in the source mask. Keep both fields. For a class-aware labeled region, recover its class from the original mask at pixels belonging to that region and check that all those pixels agree. For per-class labeling, attach the class ID used to create each Boolean input before combining region records. Region number one from a scratch pass and region number one from a residue pass are separate records.

Review the declared background and any reserved values. A pipeline can have an unclassified or ignored value as well as background. In a foreground routine, a nonzero ignore value would join the foreground if the program passed it through. Exclude ignored pixels explicitly and keep a count if operators need to understand which portions of a frame were evaluated. If a processing step converts a multiclass mask to a Boolean array, preserve the original class map for any subsequent class-aware operations.

Check the effect on measurements

A merged region changes more than count. Its bounding box spans both marks, and a region centroid can fall in a location that represents neither defect well enough for a close-up image. If inspection points are converted to robot coordinates for review, retain the class and the pixel geometry from the actual region that triggered the rule. Apply physical area thresholds only after deciding how image pixels map to the surface and what calibration applies to that view.

Test the postprocessing with both synthetic masks and saved frames. Put the hand-built mask into a regression test and record the expected region counts, classes and areas. Add a pair of same-class pixels that meet only at a corner to make the chosen adjacency visible in the tests. Add an ignored pixel between otherwise separate regions and check the intended treatment. On an actual review frame, compare an overlay for the final labeled regions with the table of region IDs, class IDs and area measurements. This will reveal a change in postprocessing even when the upstream model and its prediction image are identical.

Sources

SciPy API reference, scipy.ndimage.label

https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html

scikit-image API reference, skimage.measure.label

https://scikit-image.org/docs/stable/api/skimage.measure.html#skimage.measure.label