phenotypic.detect.MadHysteresisDetector#

class phenotypic.detect.MadHysteresisDetector(k_high: float = 5.0, k_low: float = 2.5, min_size: int = 20, connectivity: int = 2, ignore_zeros: bool = False, ignore_borders: bool = True)[source]

Bases: ThresholdDetector

Detect colonies by MAD-based noise estimation and hysteresis thresholding.

Estimate the background noise floor using the Median Absolute Deviation (MAD), then apply hysteresis thresholding with thresholds set as multiples of the estimated noise standard deviation. Designed for filter response maps (CED, Hessian, LoG, Frangi) where the noise structure is approximately Gaussian and histogram-based methods produce unstable thresholds. For a full comparison see Detection Strategies Compared.

Parameters:
  • k_high (float) – High-threshold multiplier. The high threshold is k_high * sigma_noise; pixels above this seed connected regions. Higher values are more conservative. Typical range: 3.0–8.0. Default: 5.0.

  • k_low (float) – Low-threshold multiplier. The low threshold is k_low * sigma_noise; pixels above this are included if connected to a high-threshold seed. Must be less than k_high. Typical range: 1.5–4.0. Default: 2.5.

  • min_size (int) – Minimum colony area in pixels. Connected components smaller than this are removed as noise. Default: 20.

  • connectivity (int) – Pixel connectivity for labelling connected components. 1 for 4-connectivity, 2 for 8-connectivity. Default: 2.

  • ignore_zeros (bool) – Exclude zero-intensity pixels from MAD computation. Enable for plates with black borders or masked regions. Default: True.

  • ignore_borders (bool) – Remove colonies touching image edges via clear_border(). Recommended for grid-based colony counting to eliminate partial colonies at plate boundaries. Default: True.

Returns:

Input image with objmask set to binary mask and objmap set to labeled connected components.

Return type:

Image

Raises:

ValueError – If k_low >= k_high.

Best For:
  • Filter response maps (CED, Hessian, LoG, Frangi) where the noise floor is approximately Gaussian.

  • Low-contrast colonies where signal is faint relative to background texture and MAD provides a stable noise estimate.

  • Standardised pipelines where multiplier-based thresholds generalise across plates with varying colony density.

Consider Also:
  • HysteresisDetector when thresholds should be derived from the intensity histogram rather than noise statistics.

  • OtsuDetector when the image is a raw intensity plate with a bimodal histogram.

  • ChanVeseDetector when colonies have diffuse edges and region-based segmentation is more appropriate.

See also

Tutorial 2: Detecting Colonies

Step-by-step tutorial for basic colony detection.

How To: Choose a Detection Algorithm

Guide for selecting the right detector for your plate images.

Detection Strategies Compared

In-depth comparison of all detection strategies.

Methods

__init__

apply

Detect colonies using sinusoidal cross-correlation grid estimation.

widget

Return (and optionally display) the root widget.

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

Detect colonies using sinusoidal cross-correlation grid estimation.

This method performs the core detection workflow: 1. Extract grid dimensions (if GridImage) 2. Threshold the detection matrix with adaptive kernel sizing 3. Remove noise if requested 4. Label connected components 5. Determine or estimate grid edges (via sinusoidal cross-correlation) 6. Assign dominant colonies to grid cells 7. Create final object map

Parameters:

image – Image object to process. Can be a regular Image or GridImage.

Returns:

The processed image with updated objmask and objmap.

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.