phenotypic.enhance.MedianFilter#
- class phenotypic.enhance.MedianFilter(mode: Literal['nearest', 'reflect', 'constant', 'mirror', 'wrap'] = 'nearest', shape: Literal['disk', 'square', 'diamond'] | None = None, width: int = 5, cval: float = 0.0)[source]
Bases:
ImageEnhancerRemove impulsive noise from detect_mat while preserving colony edges.
Replaces each pixel with the median of its local neighborhood, making it robust to outlier pixels (condensation droplets, dust specks, sensor noise). Preserves colony boundaries better than Gaussian smoothing because it does not average across edges.
- Parameters:
mode (Literal['nearest', 'reflect', 'constant', 'mirror', 'wrap']) – Boundary handling. Accepted values:
'nearest','reflect','constant','mirror','wrap'. Default:'nearest'.shape (Literal['disk', 'square', 'diamond'] | None) – Structuring element shape. Accepted values:
'disk','square','diamond', orNonefor library default. Default:None.width (int) – Size of the structuring element in pixels. Larger values smooth more aggressively. Typical range: 3–9. Default: 5.
cval (float) – Fill value when
mode='constant'. Default: 0.0.
- Returns:
Input image with
detect_matfiltered.rgbandgrayare unchanged.- Return type:
Image
- Best For:
Plates with salt-and-pepper noise or bright/dark speckle artifacts.
Preserving sharp colony edges during denoising.
Pre-filtering before edge-based detection (Canny, Sobel).
- Consider Also:
GaussianBlurfor faster, simpler smoothing when edge preservation is less critical.BilateralDenoisefor edge-preserving smoothing with continuous intensity gradients.RankMedianEnhancerfor configurable rank-based filtering with explicit footprint control.
See also
How To: Denoise Low-Light Images for a comparison of denoising methods on low-light plates. What Enhancement Actually Does for how enhancement fits into the pipeline model.
Methods
This class is designed to facilitate image processing tasks, particularly for analyzing microbe colonies on solid media agar.
Applies the operation to an image, either in-place or on a copy.
Return (and optionally display) the root widget.
- __init__(mode: Literal['nearest', 'reflect', 'constant', 'mirror', 'wrap'] = 'nearest', shape: Literal['disk', 'square', 'diamond'] | None = None, width: int = 5, cval: float = 0.0)[source]
This class is designed to facilitate image processing tasks, particularly for analyzing microbe colonies on solid media agar. By adjusting the mode, shape, width, and cval attributes, users can modify the processing behavior and results to suit their specific requirements for studying spatial arrangements, colony boundaries, and other morphological features.
- Parameters:
- mode
Determines how boundaries of the image are handled during processing. For instance, “reflect” can help minimize edge artifacts when analyzing colonies near the edge of the image by mirroring boundary pixels, while “constant” fills with a value (cval), which might highlight isolated colonies. Adjusting this can significantly affect how edge regions are interpreted.
- Type:
Literal[“nearest”, “reflect”, “constant”, “mirror”, “wrap”]
- shape
Specifies the shape of the structuring element used in morphological operations. For instance, “disk” simulates circular neighborhood which works well for circular colonies, whereas “square” gives a grid-like neighborhood. This can directly impact how structures are identified or segmented.
- Type:
Literal[“disk”, “square”, “diamond”] | None
- width
Size of the structuring element. Larger widths result in broader neighborhoods being considered, which may smooth or connect distant colonies, while smaller widths preserve finer details but may miss larger structural relationships. Only if shape is not None.
- Type:
- cval
Value used to fill borders when mode is set to “constant”. This directly affects colony recognition at the edges; for example, setting a high cval compared to colony intensity might obscure colonies near the borders.
- Type:
- __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.