phenotypic.enhance.NonLocalMeansDenoiser#

class phenotypic.enhance.NonLocalMeansDenoiser(patch_size: int = 5, search_dist: int = 11, h: float = 0.5, *, fast_mode: bool = False, sigma: float = 0.0)[source]

Bases: ImageEnhancer

Denoise detect_mat with non-local means patch-based filtering.

Compares patches across the image to identify similar structures and averages them, preserving thin colony boundaries and internal texture better than simple Gaussian or median filtering. Particularly effective at removing Gaussian noise and agar granularity.

For algorithm details, see What Enhancement Actually Does.

Parameters:
  • patch_size (int) – Size of patches used for comparison in pixels. Larger patches capture more structure but are slower. Typical range: 5–15. Default: 5.

  • search_dist (int) – Maximum search distance for similar patches in pixels. Larger values find more candidates at higher cost. Typical range: 5–21. Default: 11.

  • h (float) – Cut-off distance controlling smoothness. Rule of thumb: h ~= noise level (sigma). Higher values produce more smoothing. Default: 0.5.

  • fast_mode (bool) – If True, use faster variant with uniform spatial weighting. If False (default), use original algorithm with Gaussian spatial weighting.

  • sigma (float) – Expected noise standard deviation. Values > 0 improve patch weighting by accounting for expected noise variance. Default: 0.0 (disabled).

Returns:

Input image with detect_mat denoised via non-local means filtering. rgb and gray are unchanged.

Return type:

Image

Best For:
  • Scanner noise and agar granularity where colony edges must stay sharp.

  • Low-contrast or faint colonies where Gaussian blur would cause loss of detail.

  • Preserving colony texture and morphology during speckle and dust removal.

  • Pre-filtering before edge detection to avoid amplifying noise.

Consider Also:
  • BM3DDenoiser for state-of-the-art structured noise removal at higher computational cost.

  • BilateralDenoise for faster edge-preserving denoising without patch comparison.

  • BayesShrinkEnhancer for adaptive wavelet denoising with spatially varying thresholds.

See also

Tutorial 3: Enhancing Before Detection for a visual walkthrough of denoising pipelines on plate images. How To: Denoise Low-Light Images for non-local means and other denoising strategies on low-light plate images.

Methods

__init__

apply

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

widget

Return (and optionally display) the root widget.

__init__(patch_size: int = 5, search_dist: int = 11, h: float = 0.5, *, fast_mode: bool = False, sigma: float = 0.0)[source]
Parameters:
  • patch_size (int) – Size of patches used for comparison. Larger patches capture more structure but are slower. Start with 5-7 for agar plates; increase to 11-15 for heavily noisy images. Default: 7.

  • search_dist (int) – Maximal distance in pixels to search for similar patches. Larger values find more candidates at higher cost. Default: 11.

  • h (float) – Cut-off distance controlling smoothness. Typical rule of thumb: h ≈ sigma (noise level). Increase to ~1.5*sigma for more smoothing. Default: 0.1.

  • fast_mode (bool) – If True, use faster variant with uniform spatial weighting. If False, use original algorithm with Gaussian spatial weighting (slower but potentially better quality). Default: False.

  • sigma (float) – Noise standard deviation. If provided (> 0), improves patch weighting by accounting for expected noise variance. Start with estimate_sigma() output. Default: 0.0 (disabled).

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