phenotypic.refine.MaskCloser#
- class phenotypic.refine.MaskCloser(shape: Literal['auto', 'square', 'diamond', 'disk'] | numpy.ndarray | None = None, width: int = 5, n_iter: int = 1)[source]
Bases:
ObjectRefiner,FootprintMixinBridge small gaps and fill holes in colony masks using morphological closing.
Applies binary closing (dilation then erosion) to reconnect nearby fragments of the same colony separated by thin background channels. Preserves overall colony shape and size while reducing fragmentation.
- Parameters:
shape (Literal['auto', 'square', 'diamond', 'disk'] | np.ndarray | None) – Structuring element.
'auto','disk','square','diamond', or custom ndarray. Default:None.width (int) – Footprint width in pixels. Larger values bridge wider gaps but risk merging distinct colonies. Typical range: 3–9. Default: 5.
n_iter (int) – Number of closing iterations. Default: 1.
- Returns:
Input image with
objmaskandobjmapmorphologically closed.- Return type:
Image
- Best For:
Colonies fragmented by uneven pigmentation or shadow effects.
Small internal holes from condensation or glare.
Reconnecting nearby fragments before measurement.
- Consider Also:
MaskFillfor filling enclosed holes without bridging separate objects.MaskOpenerfor the opposite effect — breaking thin connections between distinct colonies.NearestNeighborMergerfor merging distant fragments based on proximity.
See also
How To: Merge Fragmented Detections for fragment merging strategies.
Methods
Initialize the closer.
Applies the operation to an image, either in-place or on a copy.
Return (and optionally display) the root widget.
- __init__(shape: Literal['auto', 'square', 'diamond', 'disk'] | numpy.ndarray | None = None, width: int = 5, n_iter: int = 1)[source]
Initialize the closer.
- Parameters:
shape (Literal["auto", "square", "diamond", "disk"] | np.ndarray | None) –
Structuring element for closing. Use:
”auto” to select a disk shape scaled to image size (larger plates → slightly larger width),
a NumPy array to pass a custom shape,
one of the named shapes (“disk”, “square”, “diamond”) with a specified width,
or
Noneto use the library default.
Larger widths fill wider gaps and smoother colony boundaries, but risk merging adjacent colonies and losing edge sharpness.
width (int) – Footprint width in pixels when using named shapes or auto-scaling. Default: 5 pixels (moderate gap-filling).
n_iter (int) – Number of times to apply closing. Repeated closing with a small element produces smoother results than a single pass with a larger element. Default: 1.
- __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.