phenotypic.refine.WhiteTophatModifier#
- class phenotypic.refine.WhiteTophatModifier(footprint_shape='disk', footprint_radius: int | None = None)[source]#
Bases:
ObjectRefinerSuppress small bright structures in the mask using white tophat.
- Intuition:
White tophat highlights small, bright features relative to their local background. On agar plates, glare, dust, or bright halos can create thin connections or speckles that pollute colony masks. This modifier detects those bright micro-structures and subtracts them from the binary mask to improve separation and mask quality.
- Why this is useful for agar plates:
Bright artifacts can bridge adjacent colonies or inflate perimeters. Removing those tiny bright elements yields cleaner, more compact masks that better match colony boundaries under uneven illumination.
- Use cases:
Reducing glare-induced bridges between neighboring colonies.
Removing bright speckles/dust that become embedded in masks after thresholding.
- Caveats:
Large footprints may remove real bright edges of colonies (e.g., highly reflective rims), slightly eroding edge sharpness.
If the footprint is too small, bright artifacts may remain.
- Parameters:
footprint_radius (int)
- footprint_shape#
Shape for the footprint used in the tophat transform. Supported: ‘disk’, ‘square’. Disk tends to preserve round features, while square can be more aggressive along axes.
- Type:
- footprint_radius#
Radius of the footprint. Larger values remove broader bright features but risk shrinking thin colony appendages.
Noneauto-scales with image size.- Type:
int | None
Examples
Suppress small bright structures in the mask using white tophat
>>> from phenotypic.refine import WhiteTophatModifier >>> op = WhiteTophatModifier(shape='disk', radius=5) >>> image = op.apply(image, inplace=True)
Methods
Initialize the modifier.
Applies the operation to an image, either in-place or on a copy.
Drop references to the UI widgets.
Push internal state into widgets.
Return (and optionally display) the root widget.
- __init__(footprint_shape='disk', footprint_radius: int | None = None)[source]#
Initialize the modifier.
- Parameters:
footprint_shape (str) –
Footprint geometry for white tophat. - ‘disk’: Balanced in all directions; gentle on round colonies. - ‘square’: Slightly stronger along rows/columns; may remove
more rectilinear glare or sensor artifacts.
footprint_radius (int | None) – Radius in pixels. Increasing removes larger bright structures and can improve background suppression, but may thin colony edges.
Noneauto-selects ~0.4% of the smaller image dimension.
- Raises:
ValueError – If
shapeis not one of the supported values (raised during operation).
- __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.
- widget(image: Image | None = None, show: bool = False) Widget#
Return (and optionally display) the root widget.
- Parameters:
- Returns:
The root widget.
- Return type:
ipywidgets.Widget
- Raises:
ImportError – If ipywidgets or IPython are not installed.