{ } Raw JSON

bundles / scipy latest / scipy / signal / _signaltools / medfilt2d

function

scipy.signal._signaltools:medfilt2d

source: /scipy/signal/_signaltools.py :1932

Signature

def   medfilt2d ( input kernel_size = 3 )

Summary

Median filter a 2-dimensional array.

Extended Summary

Apply a median filter to the input array using a local window-size given by kernel_size (must be odd). The array is zero-padded automatically.

Parameters

input : array_like

A 2-dimensional input array.

kernel_size : array_like, optional

A scalar or a list of length 2, giving the size of the median filter window in each dimension. Elements of kernel_size should be odd. If kernel_size is a scalar, then this scalar is used as the size in each dimension. Default is a kernel of size (3, 3).

Returns

out : ndarray

An array the same size as input containing the median filtered result.

Notes

This is faster than medfilt when the input dtype is uint8, float32, or float64; for other types, this falls back to medfilt. In some situations, scipy.ndimage.median_filter may be faster than this function.

Array API Standard Support

medfilt2d has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ✅                   
PyTorch               ✅                     ⛔                   
JAX                   ⚠️ no JIT
Dask                  ⚠️ computes graph     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

import numpy as np
from scipy import signal
x = np.arange(25).reshape(5, 5)
x
# Replaces i,j with the median out of 5*5 window
signal.medfilt2d(x, kernel_size=5)
# Replaces i,j with the median out of default 3*3 window
signal.medfilt2d(x)
# Replaces i,j with the median out of default 5*3 window
signal.medfilt2d(x, kernel_size=[5,3])
# Replaces i,j with the median out of default 3*5 window
signal.medfilt2d(x, kernel_size=[3,5])
# As seen in the examples, # kernel numbers must be odd and not exceed original array dim

See also

scipy.ndimage.median_filter

Aliases

  • scipy.signal.medfilt2d

Referenced by