MedianBlur#
- class deeptrack.optical.math.MedianBlur(ksize: int | Callable[[...], int] = 3, channel_axis: int | None = -1, **kwargs: Any)#
Bases:
BlurApply a median filter over spatial dimensions.
Each pixel is replaced by the median of its neighborhood defined by ksize. Median filtering is effective at removing impulsive noise (e.g., salt-and-pepper) while preserving edges.
If channel_axis is specified, the filter is applied independently per channel. Otherwise, all dimensions (including channels, if present) are treated as spatial and the filter is applied across them.
NumPy backend uses scipy.ndimage.median_filter. Torch backend uses explicit unfolding and is significantly slower. Median filtering is not differentiable.
Parameters#
- ksize: int
Size of the median filter window (must be odd).
- channel_axis: int or None, default=-1
Axis corresponding to channels. Set to None to treat all dimensions as spatial.
Methods#
- get(image, ksize, channel_axis, **kwargs) –> array | tensor
Applies the median filter to the input image using the selected backend.
Examples#
>>> import deeptrack as dt
Create an input image:
>>> import numpy as np >>> >>> input_image = np.random.rand(32, 32)
Define a median blur feature:
>>> median_blur = dt.MedianBlur(ksize=3, channel_axis=None) >>> output_image = median_blur(input_image) >>> print(output_image.shape) (32, 32)
Visualize the input and output images:
>>> import matplotlib.pyplot as plt >>> >>> plt.figure(figsize=(8, 4)) >>> plt.subplot(1, 2, 1) >>> plt.imshow(input_image, cmap='gray') >>> plt.subplot(1, 2, 2) >>> plt.imshow(output_image, cmap='gray') >>> plt.show()