ndfilters is a library of n-dimensional image filters similar to those in
scipy.ndimage,
but accelerated and parallelized using
Numba.
Compared to their scipy.ndimage equivalents, the filters in this library
offer some additional capabilities:
- Axis selection. Every filter accepts an
axisargument, so the kernel can be applied to any subset of the array's axes while the remaining axes act as batch dimensions. - Masking. A boolean
wheremask excludes selected elements of the input array from the calculation. - Physical units. Inputs can be either
numpy.ndarrayorastropy.units.Quantityinstances. - Varying kernels. The convolution kernel is allowed to change along axes orthogonal to the convolution axes.
Where a filter in this library has a scipy.ndimage counterpart, the two
agree except in the following cases.
- Boundary modes. Only
"mirror","nearest", and"wrap"are supported, plus"truncate", which has noscipy.ndimageequivalent and simply drops the parts of the kernel that fall outside the array. SciPy's"reflect","constant", and"grid-*"modes raise aValueErrorhere. - Integer input. Integer arrays are promoted to floating point, so the
result is a float and is not truncated.
scipy.ndimagereturns the dtype of the input, and for the separable filters it truncates its intermediates as well. The promotion is what lets awheremask that excludes an entire kernel footprint returnNaN. - Even-sized median footprints.
ndfilters.median_filteraverages the two middle elements, likenumpy.median, whilescipy.ndimage.median_filterselects the element of ranksize // 2, the larger of the two. SciPy's convention keeps the result in the dtype of the input and never introduces a value that was not already in the footprint, but it is a biased estimator: on unit-variance noise asize=2filter shifts the signal by roughly0.57. The two conventions agree exactly for odd-sized footprints.
The full documentation is hosted on Read the Docs.
ndfilters is published on PyPI and can be installed using pip.
pip install ndfiltersEvery filter takes an array and the shape of the kernel, and returns the filtered array.
import scipy.datasets
import ndfilters
img = scipy.datasets.ascent()
img_filtered = ndfilters.median_filter(img, size=21)The mean filter calculates a multidimensional rolling mean for the given kernel shape.
The trimmed mean filter is like the mean filter except it ignores a given portion of the dataset before calculating the mean at each pixel.
The median filter calculates a multidimensional rolling median for the given kernel shape.
The variance filter calculates the rolling variance for the given kernel shape.
The generic filter applies an arbitrary compiled function to each kernel footprint. It is the engine behind the other rolling filters in this library, and it can be used directly to build custom filters.
ndfilters.convolve()
convolves an array with a given kernel.
Unlike scipy.ndimage.convolve() or astropy.convolution.convolve(),
the kernel is allowed to vary along axes orthogonal to the convolution axes.





