phenotypic.refine.Thinning#

class phenotypic.refine.Thinning(max_num_iter: int | None = None)[source]#

Bases: ObjectRefiner

Progressively thin object masks to single-pixel-wide structures via morphological erosion.

Intuition:

Thinning iteratively removes outer pixels while preserving connectivity and 2x2 blocks, gradually reducing object width to 1 pixel. Unlike skeletonization, thinning does not require solving a medial axis problem, making it simpler and more predictable. On agar plates, thinning can clarify colony boundaries, separate overlapping regions, or prepare masks for filament analysis by stripping away outer noise layers step-by-step.

Why this is useful for agar plates:

Colonies touching or with diffuse edges can be gradually separated by removing outer pixels without aggressive erosion. Thinning is gentler than opening and can preserve thin filamentous structures better than binary erosion, making it ideal for multi-pass boundary cleaning before advanced morphological measurements.

Use cases:
  • Gradually separating touching or overlapping colonies via controlled pixel removal.

  • Clarifying colony boundaries by removing diffuse outer layers.

  • Preparing masks for graph-based analysis (converting to 1-pixel skeletons).

  • De-noising edges while preserving internal structure (e.g., before branching analysis).

Caveats:
  • Each iteration removes pixels; too many iterations will obliterate small features or break thin filaments.

  • Thinning alone does not always separate touching colonies; combine with opening or seed-based separation for better results.

  • The algorithm preserves 2x2 blocks, which can leave small “square” artifacts in very compact colonies.

  • On highly fragmented or noise-laden masks, iterations may not converge smoothly; pre-clean with SmallObjectRemover.

max_num_iter#

Maximum number of thinning iterations. Each iteration removes outer-layer pixels while maintaining topology. - None (default): Iterate until convergence (no changes detected). - Positive int: Stop after N iterations regardless of convergence.

Larger max_num_iter values progressively thin more aggressively. For separating touching colonies, try 1-3 iterations; for full skeletonization, set max_num_iter higher (10-50 depending on colony size).

Type:

int | None

Examples

Gradually thin colonies to clarify boundaries
>>> from phenotypic.refine import Thinning
>>> op = Thinning(max_num_iter=2)
>>> image = op.apply(image, inplace=True)  
Thin to convergence (full skeleton)
>>> from phenotypic.refine import Thinning
>>> op = Thinning()  # Iterate until no change
>>> image = op.apply(image, inplace=True)  
Raises:

ValueError – If max_num_iter is negative (checked during operation).

Parameters:

max_num_iter (int | None)

Methods

__init__

Initialize the thinner.

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__(max_num_iter: int | None = None)[source]#

Initialize the thinner.

Parameters:

max_num_iter (int | None) –

Upper limit on iterations. Use: - None (default) to iterate until convergence, yielding a full skeleton. - A small int (e.g., 1-3) for gentle boundary cleanup while preserving

colony bulk.

  • A large int (e.g., 10-50) for aggressive thinning to single-pixel structures.

Choosing max_num_iter is a trade-off: few iterations preserve colony size/robustness but may leave overlaps; many iterations separate more aggressively but risk removing small filaments or creating fragmentation.

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