phenotypic.enhance.GrayOpening#

class phenotypic.enhance.GrayOpening(shape: Literal['square', 'diamond', 'disk'] = 'square', width: int = 5, n_iter: int = 1)[source]

Bases: ImageEnhancer, FootprintMixin

Remove small bright artifacts from detect_mat via morphological opening.

Applies erosion followed by dilation with a structuring element, removing bright features smaller than the element while preserving the shape of larger structures. Effectively suppresses dust particles, small noise speckles, and tiny satellite colonies.

For algorithm details, see What Enhancement Actually Does.

Parameters:
  • shape (Literal['square', 'diamond', 'disk']) – Structuring element geometry. 'square' (default) preserves edges; 'diamond' is more rounded at diagonals; 'disk' provides uniform circular operations.

  • width (int) – Diameter of the structuring element in pixels. Larger values remove larger features. Typical range: 3–15. Default: 5.

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

Returns:

Input image with detect_mat morphologically opened. rgb and gray are unchanged.

Return type:

Image

Best For:
  • Removing dust particles and small bright noise from plate scans.

  • Suppressing tiny satellite colonies that interfere with detection of larger colonies.

  • Smoothing the detection surface before background subtraction.

Consider Also:
  • WhiteTophatEnhance when you want to isolate (not remove) small bright structures.

  • SubtractWhiteTophat for subtracting small bright artifacts while retaining the background.

  • BilateralDenoise for noise reduction that preserves edges without morphological assumptions.

See also

Tutorial 3: Enhancing Before Detection for a visual walkthrough of enhancement pipelines on plate images.

Methods

__init__

A kernel configuration class for image processing tasks, particularly suited for applications such as analyzing and processing images of microbe colonies on solid media agar.

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

A kernel configuration class for image processing tasks, particularly suited for applications such as analyzing and processing images of microbe colonies on solid media agar. This class enables the definition of a kernel shape and size, which significantly impacts the morphological operations applied to the image (e.g., filtering, dilation, erosion). Adjusting these parameters can enhance or hinder the detection and analysis of colony boundaries, shapes, and distribution.

Parameters:
  • shape (Literal['square', 'diamond', 'disk'])

  • width (int)

  • n_iter (int)

shape

The geometric shape of the kernel. This attribute governs the pattern and extent of neighboring pixels involved in the processing operation. Choosing “square” results in a uniform rectangular influence, which may be suitable for isotropic features but could introduce angular artifacts in circular features like microbe colonies. The “diamond” shape provides a more angular neighborhood pattern that helps preserve diagonal structures. On the other hand, “disk” introduces a circular pattern that can align well with colony boundaries and reduce distortions in rounded features.

Type:

Literal[“square”, “diamond”, “disk”]

width

The size (diameter) of the kernel in pixels. A larger width increases the area of influence during image processing, which can smooth out smaller features like noise but potentially merge closely spaced microbe colonies into larger regions. Smaller values offer finer detail and greater distinction between colonies but may leave noise unprocessed or small artifacts unchanged.

Type:

int

n_iter

Number of times to apply the opening operation. Repeated opening with a small element produces smoother results than a single pass with a larger element. Default: 1.

Type:

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.