phenotypic.enhance.OpeningSubtractBg#

class phenotypic.enhance.OpeningSubtractBg(shape: Literal['square', 'diamond', 'disk'] = 'disk', width: int = 51, n_iter: int = 1)[source]

Bases: ImageEnhancer, FootprintMixin

Subtract background from detect_mat via OpenCV-accelerated morphological opening.

Computes the white top-hat transform (original minus morphological opening) using OpenCV’s C++/SIMD backend, isolating bright foreground structures smaller than the structuring element while removing slow-varying background intensity. Significantly faster than scikit-image equivalents for high-throughput workflows.

For algorithm details, see What Enhancement Actually Does.

Parameters:
  • shape (Literal['square', 'diamond', 'disk']) – Structuring element geometry. 'disk' (default) gives isotropic removal suited to round colonies; 'square' is fastest; 'diamond' is a compromise.

  • width (int) – Diameter of the structuring element in pixels. Must be larger than colony diameter to avoid subtracting colony signal. Typical range: 31–101. Default: 51.

  • n_iter (int) – Number of morphological iterations. Higher values intensify background removal. Default: 1.

Returns:

Input image with detect_mat containing only foreground structures smaller than the structuring element. rgb and gray are unchanged.

Return type:

Image

Best For:
  • Fast background subtraction for high-throughput plate screening.

  • Removing uneven illumination gradients and agar shading before colony detection.

  • Pipelines where speed matters (large batches, parameter sweeps).

  • Drop-in performance upgrade over SubtractRollingBall when a flat structuring element is acceptable.

Consider Also:
  • SubtractRollingBall for parabolic background estimation that handles gradual intensity ramps more accurately.

  • SubtractGaussian for Gaussian-based background subtraction with continuous control over the background scale.

  • WhiteTophatEnhance when you want to keep only the extracted small bright structures.

See also

Tutorial 3: Enhancing Before Detection for a visual walkthrough of background subtraction on plate images. What Enhancement Actually Does for background on morphological background removal strategies.

Methods

__init__

apply

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

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)

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.