phenotypic.refine.Skeletonize#
- class phenotypic.refine.Skeletonize(method: Literal['zhang', 'lee'] | None = None)[source]
Bases:
ObjectRefinerReduce object masks to single-pixel-wide skeletons via medial axis thinning.
Compresses each object region to its medial axis (centerline), preserving topological connectivity while discarding boundary and interior pixels. Useful for distilling colony morphology to its core branching structure for filament or spreading phenotype analysis.
- Parameters:
method (Literal['zhang', 'lee'] | None) – Thinning algorithm.
"zhang"is fast and optimized for clean 2D masks."lee"is more robust to noise and works on 2D/3D.Noneauto-selects based on dimensionality. Default: None.- Returns:
Input image with
objmaskreplaced by the single-pixel-wide skeleton.- Return type:
Image
- Raises:
ValueError – If an invalid
methodis provided.
- Best For:
Extracting colony centerlines for elongation or orientation analysis.
Analyzing branching patterns in filamentous fungi or spreading bacterial phenotypes.
Simplifying masks for spatial graph analysis or hyphae tracking.
Reducing boundary noise before measuring advanced morphological features.
- Consider Also:
Thinningfor iterative boundary peeling with control over the number of iterations.MaskGradientwhen you need boundary outlines rather than medial axes.MaskEroderfor uniform inward shrinking that preserves filled regions.
See also
How To: Refine Noisy Detection Boundaries for skeleton-based analysis workflows. Refinement Strategies for a comparison of morphological refinement methods.
Methods
Initialize the skeletonizer.
Applies the operation to an image, either in-place or on a copy.
Return (and optionally display) the root widget.
- __init__(method: Literal['zhang', 'lee'] | None = None)[source]
Initialize the skeletonizer.
- Parameters:
method (Literal["zhang", "lee"] | None) –
Algorithm for skeletonization.
”zhang”: Optimized for 2D images; fast, produces thin skeletons. Best for well-defined colony boundaries.
”lee”: Works on 2D/3D; more robust to noisy or irregular boundaries. Slightly slower but preserves topology better on challenging images.
None: Automatically selects Zhang for 2D and Lee for 3D.
Choosing the right method depends on image quality: clean, binary masks benefit from Zhang; noisier masks or fungal hyphae benefit from Lee.
- __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.