phenotypic.refine.MaskDilator#

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

Bases: ObjectRefiner, FootprintMixin

Expand colony masks outward using morphological dilation.

Adds pixels around object boundaries, bridging small gaps between nearby fragments and recovering faint halos excluded by strict thresholding. Dilation inflates area; follow with erosion (closing) if area accuracy is critical.

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

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

  • n_iter (int)

Returns:

Input image with objmask and objmap dilated.

Return type:

Image

Best For:
  • Bridging thin gaps between fragments of the same colony.

  • Recovering faint colony halos near detection boundaries.

  • Preprocessing before merge-based refinement operations.

Consider Also:
  • MaskCloser for dilation-then-erosion that bridges gaps without inflating colony size.

  • MaskEroder for the opposite effect — shrinking masks to remove thin protrusions.

See also

Refinement Strategies for the recommended refinement sequence.

Methods

__init__

Initialize the dilator.

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'] | np.ndarray[int] | None = None, width: int = 3, n_iter: int = 1)[source]

Initialize the dilator.

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

    Structuring element for dilation. 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 expand objects more and bridge wider gaps, but risk merging distinct colonies and inflating size measurements beyond recovery.

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

  • n_iter (int) – Number of times to apply dilation. Repeated dilation 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.