/
_median.py
82 lines (70 loc) · 2.89 KB
/
_median.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from warnings import warn
import numpy as np
from scipy import ndimage as ndi
from .rank import generic
def median(
image, footprint=None, out=None, mode='nearest', cval=0.0, behavior='ndimage'
):
"""Return local median of an image.
Parameters
----------
image : array-like
Input image.
footprint : ndarray, optional
If ``behavior=='rank'``, ``footprint`` is a 2-D array of 1's and 0's.
If ``behavior=='ndimage'``, ``footprint`` is a N-D array of 1's and 0's
with the same number of dimension than ``image``.
If None, ``footprint`` will be a N-D array with 3 elements for each
dimension (e.g., vector, square, cube, etc.)
out : ndarray, (same dtype as image), optional
If None, a new array is allocated.
mode : {'reflect', 'constant', 'nearest', 'mirror','‘wrap'}, optional
The mode parameter determines how the array borders are handled, where
``cval`` is the value when mode is equal to 'constant'.
Default is 'nearest'.
.. versionadded:: 0.15
``mode`` is used when ``behavior='ndimage'``.
cval : scalar, optional
Value to fill past edges of input if mode is 'constant'. Default is 0.0
.. versionadded:: 0.15
``cval`` was added in 0.15 is used when ``behavior='ndimage'``.
behavior : {'ndimage', 'rank'}, optional
Either to use the old behavior (i.e., < 0.15) or the new behavior.
The old behavior will call the :func:`skimage.filters.rank.median`.
The new behavior will call the :func:`scipy.ndimage.median_filter`.
Default is 'ndimage'.
.. versionadded:: 0.15
``behavior`` is introduced in 0.15
.. versionchanged:: 0.16
Default ``behavior`` has been changed from 'rank' to 'ndimage'
Returns
-------
out : 2-D array (same dtype as input image)
Output image.
See also
--------
skimage.filters.rank.median : Rank-based implementation of the median
filtering offering more flexibility with additional parameters but
dedicated for unsigned integer images.
Examples
--------
>>> from skimage import data
>>> from skimage.morphology import disk
>>> from skimage.filters import median
>>> img = data.camera()
>>> med = median(img, disk(5))
"""
if behavior == 'rank':
if mode != 'nearest' or not np.isclose(cval, 0.0):
warn(
"Change 'behavior' to 'ndimage' if you want to use the "
"parameters 'mode' or 'cval'. They will be discarded "
"otherwise.",
stacklevel=2,
)
return generic.median(image, footprint=footprint, out=out)
if footprint is None:
footprint = ndi.generate_binary_structure(image.ndim, image.ndim)
return ndi.median_filter(
image, footprint=footprint, output=out, mode=mode, cval=cval
)