phenotypic.enhance.WhiteTophatEnhancer#

class phenotypic.enhance.WhiteTophatEnhancer(shape: str = 'diamond', radius: int | None = None)[source]#

Bases: ImageEnhancer

White top-hat transform to suppress small bright structures.

Computes the white top-hat (original minus opening) using a structuring element, then subtracts it from the image here (i.e., remove small bright blobs). In agar plate colony images, this helps reduce bright specks from dust, glare, or condensation, making colonies stand out against a smoother background.

Use cases (agar plates): - Remove small bright artifacts that can be mistaken for tiny colonies. - Reduce glare highlights on shiny plates before thresholding.

Tuning and effects: - shape: The morphology footprint geometry. ‘diamond’ or ‘disk’ are good

isotropic choices on plates; ‘square’ can align with pixel grids.

  • radius: Sets the maximum size of bright features to remove. Choose slightly smaller than the minimum colony radius so real colonies are preserved.

Caveats: - If radius is too large, real small colonies will be attenuated. - Operates on bright features; for dark colonies on bright agar, it primarily

removes bright noise rather than enhancing the colonies themselves.

Parameters:
shape#

Footprint shape: ‘diamond’, ‘disk’, ‘square’, ‘sphere’, ‘cube’.

Type:

str

radius#

Footprint radius in pixels; if None, a small default is derived from the image size.

Type:

int | None

Methods

__init__

apply

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

dispose_widgets

Drop references to the UI widgets.

sync_widgets_from_state

Push internal state into widgets.

widget

Return (and optionally display) the root widget.

__init__(shape: str = 'diamond', radius: 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.

  • radius (int | None) – Maximum bright-object size (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

dispose_widgets() None#

Drop references to the UI widgets.

Return type:

None

sync_widgets_from_state() None#

Push internal state into widgets.

Return type:

None

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.