phenotypic.refine.MaskOpener#

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

Bases: ObjectRefiner, FootprintMixin

Smooth mask boundaries and break thin bridges between touching colonies.

Applies binary opening (erosion then dilation) to remove small isolated pixels and narrow connections. Colonies linked by faint film or agar artifacts become separated without significantly shrinking well-formed colony masks.

Parameters:
  • shape (Literal['auto'] | FootprintShape | np.ndarray | None) – Structuring element. 'auto' selects based on detected objects. 'diamond', 'square', 'disk', or a custom ndarray. Default: None.

  • width (int) – Size of the structuring element in pixels. Larger values smooth more aggressively. Typical range: 3–9. Default: 5.

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

Returns:

Input image with objmask and objmap morphologically opened.

Return type:

Image

Best For:
  • Splitting colonies connected by 1–2 pixel bridges after thresholding.

  • Removing tiny noise specks from the detection mask.

  • Smoothing jagged mask edges before measurement.

Consider Also:
  • MaskCloser for the opposite effect — filling small gaps and bridging fragments.

  • SmallObjectRemover for removing small objects by area rather than morphology.

  • SeparateObjects for splitting merged colonies using watershed-based separation.

See also

How To: Refine Noisy Detection Boundaries for a walkthrough of refinement operations. Refinement Strategies for the recommended refinement sequence.

Methods

__init__

Initialize the opener.

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'] | Literal['disk', 'square', 'diamond'] | numpy.ndarray | None = None, width: int = 5, n_iter: int = 1)[source]

Initialize the opener.

Parameters:
  • shape (Literal["auto"] | np.ndarray | int | None) –

    Structuring element for opening. Use:

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

    • a NumPy array to pass a custom shape,

    • an int width to build a diamond shape of that size,

    • or None to use the library default.

    Larger widths disconnect wider bridges and suppress more speckles, but erode edges and can remove small colonies.

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

  • width (int)

__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.