phenotypic.detect.LiDetector#

class phenotypic.detect.LiDetector(ignore_zeros: bool = False, ignore_borders: bool = True)[source]

Bases: ThresholdDetector

Detect colonies by minimising cross-entropy between the original and thresholded image.

Iteratively refine a threshold that minimises the information loss (cross-entropy) between the original intensity distribution and the binarised result. Performs well on low-contrast or noisy plates where the histogram is not clearly bimodal and Otsu’s variance-based assumption does not hold. For a full comparison see Detection Strategies Compared.

Parameters:
  • ignore_zeros (bool) – Exclude zero-intensity pixels from threshold computation. Enable for plates with black borders or masked regions; disable only when zero is a meaningful intensity value. 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 threshold computation fails (e.g., degenerate histogram with insufficient intensity variation).

Best For:
  • Low-contrast plates where colony and background intensities overlap significantly.

  • Noisy or textured agar backgrounds that create histogram irregularities breaking bimodal assumptions.

  • Images where the intensity distribution is unimodal or only weakly bimodal.

Consider Also:
  • OtsuDetector when the histogram is clearly bimodal and a fast single-pass threshold suffices.

  • YenDetector for a correlation-based alternative that handles skewed histograms.

  • HysteresisDetector when colony brightness varies across the plate and a single threshold under-segments faint regions.

References

[1] C. H. Li and C. K. Lee, “Minimum cross entropy thresholding,” Pattern Recognit., vol. 26, no. 4, pp. 617–625, 1993.

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.