[BUG] iterable sigma and multichannel

+ docstring improvement
This commit is contained in:
Emmanuelle Gouillart
2013-08-25 16:41:37 +02:00
parent 15b4a4d979
commit b4242ca3fb
2 changed files with 19 additions and 8 deletions
+12 -8
View File
@@ -1,3 +1,5 @@
import collections as coll
import numpy as np
from scipy import ndimage
from skimage.util import img_as_float
@@ -13,7 +15,8 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0,
----------
image : array-like
input image to filter
input image (grayscale or color) to filter. If color channels are
to be filtered separately, use ``multichannel=True``.
sigma : scalar or sequence of scalars
standard deviation for Gaussian kernel. The standard
deviations of the Gaussian filter are given for each axis as a
@@ -22,19 +25,18 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0,
output : array, optional
The ``output`` parameter passes an array in which to store the
filter output.
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
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'.
cval : scalar, optional
Value to fill past edges of input if ``mode`` is 'constant'. Default
Value to fill past edges of input if `mode` is 'constant'. Default
is 0.0
multichannel : bool, optional (default: False)
Whether the last axis of the image is to be interpreted as multiple
channels. If True, each channel is filtered separately (channels are
not mixed together).
Returns
-------
@@ -84,7 +86,9 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0,
"""
if multichannel:
# do not filter across channels
ndim = image.ndim
sigma = [sigma] * (ndim - 1) + [0]
if not isinstance(sigma, coll.Iterable):
sigma = [sigma] * (image.ndim - 1)
if len(sigma) != image.ndim:
sigma = np.concatenate((np.asarray(sigma), [0]))
image = img_as_float(image)
return ndimage.gaussian_filter(image, sigma, mode=mode, cval=cval)
+7
View File
@@ -20,6 +20,13 @@ def test_multichannel():
a[1, 1] = np.arange(1, 4)
gaussian_rgb_a = gaussian_filter(a, sigma=1, mode='reflect',
multichannel=True)
# Check that the mean value is conserved in each channel
# (color channels are not mixed together)
assert np.allclose([a[..., i].mean() for i in range(3)],
[gaussian_rgb_a[..., i].mean() for i in range(3)])
# Iterable sigma
gaussian_rgb_a = gaussian_filter(a, sigma=[1, 2], mode='reflect',
multichannel=True)
assert np.allclose([a[..., i].mean() for i in range(3)],
[gaussian_rgb_a[..., i].mean() for i in range(3)])