phenotypic.refine.Thinning#

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

Bases: ObjectRefiner

Progressively thin object masks by iteratively removing outer pixels while preserving connectivity.

Strips away boundary pixels one layer at a time, gradually reducing object width toward single-pixel structures. Unlike skeletonization, thinning offers explicit iteration control, making it useful for gentle boundary cleanup (few iterations) or full skeleton extraction (convergence).

Parameters:

max_num_iter (int | None) – Maximum thinning iterations. None iterates until convergence (full skeleton). A small value (1–3) provides gentle boundary cleanup; a large value (10–50) thins aggressively. Default: None.

Returns:

Input image with objmask thinned by the specified number of iterations.

Return type:

Image

Raises:

ValueError – If max_num_iter is negative.

Best For:
  • Gradually separating touching or overlapping colonies via controlled pixel removal.

  • Clarifying diffuse colony boundaries before morphological measurements.

  • Preparing masks for graph-based analysis by converting to single-pixel skeletons.

  • De-noising colony edges while preserving filamentous structures.

Consider Also:
  • Skeletonize for direct medial-axis extraction without iterative control.

  • MaskEroder for uniform inward shrinking with a configurable structuring element.

  • SeparateObjects for watershed-based separation of touching colonies.

See also

How To: Refine Noisy Detection Boundaries for thinning-based boundary cleanup workflows. Refinement Strategies for a comparison of morphological refinement methods.

Methods

__init__

Initialize the thinner.

apply

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

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

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.