🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.ndimage.median_filter.html
median_filter — SciPy v1.17.0 Manual
For 2-dimensional images with uint8, ... function scipy.signal.medfilt2d may be faster. It is however limited to constant mode with cval=0. The filter always returns the argument that would appear at index n // 2 in a sorted array, where n is the number of elements in the footprint of the filter. Note that this differs from the conventional definition of the median when n is ...
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.signal.medfilt.html
medfilt — SciPy v1.17.0 Manual
Apply a median filter to the input array using a local window-size given by kernel_size.
🌐
TutorialsPoint
tutorialspoint.com › scipy › scipy_median_filter.htm
SciPy - Median Filter
The scipy.ndimage module of scipy library have a function namely, median_filter() to apply the median filter on the given images to remove salt-and-pepper noise by preserving the details of the edges.
🌐
SciPy
docs.scipy.org › doc › scipy-1.15.2 › reference › generated › scipy.ndimage.median_filter.html
median_filter — SciPy v1.15.2 Manual
>>> from scipy import ndimage, datasets >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> plt.gray() # show the filtered result in grayscale >>> ax1 = fig.add_subplot(121) # left side >>> ax2 = fig.add_subplot(122) # right side >>> ascent = datasets.ascent() >>> result = ndimage.median_filter(ascent, size=20) >>> ax1.imshow(ascent) >>> ax2.imshow(result) >>> plt.show()
🌐
CuPy
docs.cupy.dev › en › stable › reference › generated › cupyx.scipy.ndimage.median_filter.html
cupyx.scipy.ndimage.median_filter — CuPy 14.1.1 documentation
cupyx.scipy.ndimage.median_filter(input, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0, axes=None)[source]# Multi-dimensional median filter. Parameters: input (cupy.ndarray) – The input array. size (int or sequence of int) – One of size or footprint must be provided.
🌐
SciPy Lecture Notes
scipy-lectures.org › advanced › image_processing › auto_examples › plot_denoising.html
2.6.8.15. Denoising an image with the median filter — Scipy lecture notes
This example shows the original image, the noisy image, the denoised one (with the median filter) and the difference between the two.
🌐
SciPy
docs.scipy.org › doc › scipy-1.15.3 › reference › generated › scipy.ndimage.median_filter.html
median_filter — SciPy v1.15.3 Manual
>>> from scipy import ndimage, datasets >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> plt.gray() # show the filtered result in grayscale >>> ax1 = fig.add_subplot(121) # left side >>> ax2 = fig.add_subplot(122) # right side >>> ascent = datasets.ascent() >>> result = ndimage.median_filter(ascent, size=20) >>> ax1.imshow(ascent) >>> ax2.imshow(result) >>> plt.show()
🌐
SciPy
docs.scipy.org › doc › scipy-1.11.4 › reference › generated › scipy.ndimage.median_filter.html
scipy.ndimage.median_filter — SciPy v1.11.4 Manual
Controls the placement of the filter on the input array’s pixels. A value of 0 (the default) centers the filter over the pixel, with positive values shifting the filter to the left, and negative ones to the right.
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › scipy-1.16.0 › reference › generated › scipy.ndimage.median_filter.html
median_filter — SciPy v1.16.0 Manual
Also, this function does not support the float16 dtype, behavior in the presence of NaNs is undefined, and memory consumption scales with n**4. For float16 support, greater control over the definition of the filter, and to limit memory usage, consider using vectorized_filter with NumPy functions np.median or np.nanmedian. ... Try it in your browser! >>> from scipy import ndimage, datasets >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> plt.gray() # show the filtered result in grayscale >>> ax1 = fig.add_subplot(121) # left side >>> ax2 = fig.add_subplot(122) # right side >>> ascent = datasets.ascent() >>> result = ndimage.median_filter(ascent, size=20) >>> ax1.imshow(ascent) >>> ax2.imshow(result) >>> plt.show()
🌐
SciPy
docs.scipy.org › doc › scipy-1.7.1 › reference › generated › scipy.ndimage.median_filter.html
scipy.ndimage.median_filter — SciPy v1.7.1 Manual
scipy.ndimage.median_filter(input, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0)[source]¶
🌐
SciPy
docs.scipy.org › doc › scipy-0.14.0 › reference › generated › scipy.ndimage.filters.median_filter.html
scipy.ndimage.filters.median_filter — SciPy v0.14.0 Reference Guide
scipy.ndimage.filters.median_filter(input, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0)[source]¶
🌐
SciPy
scipy.github.io › devdocs › reference › generated › scipy.ndimage.median_filter.html
median_filter — SciPy v1.18.0.dev Manual
For 2-dimensional images with uint8, ... function scipy.signal.medfilt2d may be faster. It is however limited to constant mode with cval=0. The filter always returns the argument that would appear at index n // 2 in a sorted array, where n is the number of elements in the footprint of the filter. Note that this differs from the conventional definition of the median when n is ...
🌐
SciPy
docs.scipy.org › doc › scipy-0.16.1 › reference › generated › scipy.ndimage.filters.median_filter.html
scipy.ndimage.filters.median_filter — SciPy v0.16.1 Reference Guide
October 24, 2015 - scipy.ndimage.filters.median_filter(input, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0)[source]¶
🌐
machinelearning1
machinelearning1.wordpress.com › 2014 › 07 › 13 › signal-processing-apply-median-filters-python
Signal Processing – Apply Median Filters (Python) | machinelearning1
July 13, 2014 - import numpy as np import scipy as sp import matplotlib.pyplot as plt from scipy import signal t = np.linspace(0,10,200) # create a time signal x1 = np.sin(t) # create a simple sine wave x2 = x1 + np.random.rand(200) # add noise to the signal y1 = sp.signal.medfilt(x2,21) # add noise to the signal # plot the results plt.subplot(2,1,1) plt.plot(t,x2,'yo-') plt.title('input wave') plt.xlabel('time') plt.subplot(2,1,2) plt.plot(range(200),y1,'yo-') plt.title('filtered wave') plt.xlabel('time') plt.show()
Top answer
1 of 2
9

Based on this post, we could create sliding windows to get a 2D array of such windows being set as rows in it. These windows would merely be views into the data array, so no memory consumption and thus would be pretty efficient. Then, we would simply use those ufuncs along each row axis=1.

Thus, for example sliding-median` could be computed like so -

np.median(strided_app(data, window_len,1),axis=1)

For the other ufuncs, just use the respective ufunc names there : np.min, np.max & np.mean. Please note this is meant to give a generic solution to use ufunc supported functionality.

For the best performance, one must still look into specific functions that are built for those purposes. For the four requested functions, we have the builtins, like so -

Median : scipy.signal.medfilt.

Max : scipy.ndimage.filters.maximum_filter1d.

Min : scipy.ndimage.filters.minimum_filter1d.

Mean : scipy.ndimage.filters.uniform_filter1d

2 of 2
1

The fact that applying of a median filter with the window size 1 will not change the array gives us a freedom to apply the median filter row-wise or column-wise.

For example, this code

from scipy.ndimage import median_filter
import numpy as np

arr = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])
median_filter(arr, size=3, cval=0, mode='constant')
#with cval=0, mode='constant' we set that input array is extended with zeros 
#when window overlaps edges, just for visibility and ease of calculation

outputs an expected filtered with window (3, 3) array

array([[0., 2., 0.],
       [2., 5., 3.],
       [0., 5., 0.]])

because median_filter automatically extends the size to all dimensions, so the same effect we can get with:

median_filter(arr, size=(3, 3), cval=0, mode='constant')

Now, we can also apply median_filter row-wise with setting 1 to the first element of size

median_filter(arr, size=(1, 3), cval=0, mode='constant')

Output:

array([[1., 2., 2.],
       [4., 5., 5.],
       [7., 8., 8.]])

And column-wise with the same logic

median_filter(arr, size=(3, 1), cval=0, mode='constant')

Output:

array([[1., 2., 3.],
       [4., 5., 6.],
       [4., 5., 6.]])
🌐
Gael-varoquaux
gael-varoquaux.info › scipy-lecture-notes › intro › scipy › auto_examples › plot_filters.html
1.5.12.5. Explore signal filtering with scipy.signal — Scipy lecture notes
Apply a variety of turn-key filters to it, and plot the result · from scipy import signal · from matplotlib import pyplot as plt · plt.figure(figsize=(7, 4)) plt.plot(x, label='Original signal') plt.plot(signal.medfilt(x), label='medfilt: median filter') plt.plot(signal.wiener(x), label='wiener: wiener filter') plt.legend(loc='best') plt.show() Total running time of the script: ( 0 minutes 0.039 seconds) Download Python source code: plot_filters.py ·
🌐
SciPy
docs.scipy.org › doc › scipy-1.16.2 › reference › generated › scipy.signal.medfilt.html
medfilt — SciPy v1.16.2 Manual
The more general function scipy.ndimage.median_filter has a more efficient implementation of a median filter and therefore runs much faster.