phenotypic.refine.MaskEroder#

class phenotypic.refine.MaskEroder(shape: Literal['auto', 'square', 'diamond', 'disk'] | numpy.ndarray | None = None, width: int = 3, n_iter: int = 1)[source]

Bases: ObjectRefiner, FootprintMixin

Shrink colony masks inward to remove thin protrusions and noise pixels.

Removes outer boundary pixels from all objects, eliminating thin whiskers, isolated specks, and uncertain boundary pixels from soft edges. Leaves the core colony structure intact.

Parameters:
  • shape (Literal['auto', 'square', 'diamond', 'disk'] | np.ndarray | None) – Structuring element. 'auto', 'disk', 'square', 'diamond', or custom ndarray. Default: None.

  • width (int) – Footprint width in pixels. Default: 3.

  • n_iter (int) – Number of erosion iterations. Default: 1.

Returns:

Input image with objmask and objmap eroded.

Return type:

Image

Best For:
  • Removing thin protrusions or whiskers from colony edges.

  • Eliminating noise specks that survived previous cleanup.

  • Excluding uncertain boundary pixels for higher-precision measurements.

Consider Also:
  • MaskDilator for the opposite effect — expanding masks outward.

  • MaskOpener for erosion-then-dilation that removes thin features without permanently shrinking colonies.

  • SmallObjectRemover for removing small objects by area rather than shrinking all objects.

See also

Refinement Strategies for the recommended refinement sequence.

Methods

__init__

Initialize the eroder.

apply

Applies the operation to an image, either in-place or on a copy.

widget

Return (and optionally display) the root widget.

__init__(shape: Literal['auto', 'square', 'diamond', 'disk'] | numpy.ndarray | None = None, width: int = 3, n_iter: int = 1)[source]

Initialize the eroder.

Parameters:
  • shape (Literal["auto", "square", "diamond", "disk"] | np.ndarray | None) –

    Structuring element for erosion. Use:

    • ”auto” to select a disk shape scaled to image size (larger plates → slightly larger width),

    • a NumPy array to pass a custom shape,

    • one of the named shapes (“disk”, “square”, “diamond”) with a specified width,

    • or None to use the library default.

    Larger widths erode more aggressively, removing larger features but risking elimination of small colonies and over-shrinkage of area measurements.

  • width (int) – Footprint width in pixels when using named shapes or auto-scaling. Default: 3 pixels (moderate erosion).

  • n_iter (int) – Number of times to apply erosion. Repeated erosion with a small element produces smoother results than a single pass with a larger element. Default: 1.

__del__()

Automatically stop tracemalloc when the object is deleted.

__getstate__()

Prepare the object for pickling by disposing of any widgets.

This ensures that UI components (which may contain unpickleable objects like input functions or thread locks) are cleaned up before serialization.

Note

This method modifies the object state by calling dispose_widgets(). Any active widgets will be detached from the object.

apply(image, inplace=False)

Applies the operation to an image, either in-place or on a copy.

Parameters:
  • image (Image) – The arr image to apply the operation on.

  • inplace (bool) – If True, modifies the image in place; otherwise, operates on a copy of the image.

Returns:

The modified image after applying the operation.

Return type:

Image

widget(image: Image | None = None, show: bool = False) Widget

Return (and optionally display) the root widget.

Parameters:
  • image (Image | None) – Optional image to visualize. If provided, visualization controls will be added to the widget.

  • show (bool) – Whether to display the widget immediately. Defaults to False.

Returns:

The root widget.

Return type:

ipywidgets.Widget

Raises:

ImportError – If ipywidgets or IPython are not installed.