phenotypic.enhance.SubtractWhiteTophat#

class phenotypic.enhance.SubtractWhiteTophat(shape: str = 'diamond', width: int | None = None)[source]

Bases: ImageEnhancer

Suppress small bright artifacts in detect_mat by subtracting the white top-hat.

Computes the white top-hat (original minus morphological opening) and subtracts it from the image, removing small bright blobs such as dust specks, glare highlights, and condensation artifacts while preserving larger colony structures.

For algorithm details, see What Enhancement Actually Does.

Parameters:
  • shape (str) – Footprint geometry. 'diamond' (default) or 'disk' provide isotropic behavior; 'square' can align with sensor grid artifacts.

  • width (int) – Maximum bright-object size (pixels) targeted for removal. Set slightly smaller than the smallest colonies to preserve them. None (default) derives a small value from image dimensions.

Returns:

Input image with detect_mat smoothed by subtracting the white top-hat. rgb and gray are unchanged.

Return type:

Image

Best For:
  • Removing small bright artifacts that could be mistaken for tiny colonies.

  • Reducing glare highlights on shiny plates before thresholding.

  • Cleaning up dust and condensation artifacts that confuse detection.

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

  • GrayOpening for morphological smoothing that removes small bright features without explicit subtraction.

  • RankMedianEnhancer for impulsive noise removal via median filtering.

See also

Tutorial 3: Enhancing Before Detection for a visual walkthrough of artifact removal on plate images.

Methods

__init__

apply

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

widget

Return (and optionally display) the root widget.

__init__(shape: str = 'diamond', width: int | None = None)[source]
Parameters:
  • shape (str) – Footprint geometry controlling which bright features are removed. ‘diamond’ or ‘disk’ provide isotropic behavior on plates; ‘square’ can align with sensor grid artifacts. Advanced: ‘sphere’ or ‘cube’ for volumetric data.

  • width (int | None) – Maximum bright-object width (in pixels) targeted for removal. Set slightly smaller than the smallest colonies to avoid suppressing real colonies. None picks a small default based on image dimensions.

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