phenotypic.refine.MaskGradient#

class phenotypic.refine.MaskGradient(shape: Literal['auto', 'square', 'diamond', 'disk'] | numpy.ndarray | None = None, width: int = 1)[source]

Bases: ObjectRefiner, FootprintMixin

Extract object boundaries via morphological gradient (dilation minus erosion).

Computes the difference between dilation and erosion of the binary mask, producing a thin outline of each object’s boundary pixels. Interior and exterior pixels are removed, leaving only the colony perimeter for edge-focused analysis or visualization.

Parameters:
  • shape (Literal['auto', 'square', 'diamond', 'disk'] | np.ndarray | None) – Structuring element for gradient computation. "auto" selects a disk scaled to image size, "disk", "square", or "diamond" use a named shape at the given width, a NumPy array provides a custom element, and None uses the library default. Default: None.

  • width (int) – Footprint width in pixels when using named shapes or auto-scaling. Larger values produce thicker boundaries. Typical range: 1–5. Default: 1.

Returns:

Input image with objmask replaced by the gradient boundary mask.

Return type:

Image

Raises:

AttributeError – If an invalid shape type is provided.

Best For:
  • Extracting colony perimeters for boundary roughness or circularity measurements.

  • Creating boundary masks for edge-specific color or texture analysis.

  • Visualizing colony contours as QC overlays on raw images.

  • Detecting spreading or filamentous edges extending from colony cores.

Consider Also:
  • Skeletonize when you need medial-axis topology rather than boundary outlines.

  • MaskEroder for uniform inward shrinking without extracting boundaries.

  • Thinning for iterative boundary peeling that preserves connectivity.

See also

How To: Refine Noisy Detection Boundaries for boundary extraction workflows. Refinement Strategies for a comparison of morphological refinement methods.

Methods

__init__

Initialize the gradient extractor.

apply

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

widget

Return (and optionally display) the root widget.

__init__(shape: Literal['auto', 'square', 'diamond', 'disk'] | numpy.ndarray | None = None, width: int = 1)[source]

Initialize the gradient extractor.

Parameters:
  • shape (Literal["auto", "square", "diamond", "disk"] | np.ndarray | None) –

    Structuring element for gradient computation. Use:

    • ”auto” to select a disk shape scaled to image size,

    • a NumPy array to pass a custom shape,

    • one of the named shapes (“disk”, “square”, “diamond”) with a specified width,

    • or None to use the library default.

    Larger widths produce thicker boundaries with less precision but more robustness to noise.

  • width (int) – Footprint width in pixels when using named shapes or auto-scaling. Default: 1 pixel (thin, precise boundaries).

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