From 9795d3fad4bb4c039d1515bece5e85cc88d76999 Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Thu, 29 Aug 2013 23:14:30 +0200 Subject: [PATCH] Gaussian filter function: changed the default value of multichannel Now we try to guess automatically whether the image is grayscale or RGB --- skimage/filter/_gaussian.py | 23 +++++++++++++++++------ skimage/filter/tests/test_gaussian.py | 8 +++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/skimage/filter/_gaussian.py b/skimage/filter/_gaussian.py index 8cb2be1f..af63ba10 100644 --- a/skimage/filter/_gaussian.py +++ b/skimage/filter/_gaussian.py @@ -1,13 +1,16 @@ import collections as coll import numpy as np from scipy import ndimage -from skimage.util import img_as_float +import warnings + +from ..util import img_as_float +from ..color import guess_spatial_dimensions __all__ = ['gaussian_filter'] def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0, - multichannel=False): + multichannel=None): """ Multi-dimensional Gaussian filter @@ -15,8 +18,7 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0, ---------- image : array-like - input image (grayscale or color) to filter. If color channels are - to be filtered separately, use ``multichannel=True``. + input image (grayscale or color) to filter. 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 @@ -32,10 +34,12 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0, cval : scalar, optional Value to fill past edges of input if `mode` is 'constant'. Default is 0.0 - multichannel : bool, optional (default: False) + multichannel : bool, optional (default: None) 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). + not mixed together). Only 3 channels are supported. If `None`, + the function will attempt to guess this, and raise a warning if + ambiguous, when the array has shape (M, N, 3). Returns ------- @@ -84,6 +88,13 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0, >>> image = lena() >>> filtered_lena = gaussian_filter(image, sigma=1, multichannel=True) """ + spatial_dims = guess_spatial_dimensions(image) + if spatial_dims is None and multichannel is None: + msg = ("Images with dimensions (M, N, 3) are interpreted as 2D+RGB" + + " by default. Use `multichannel=False` to interpret as " + + " 3D image with last dimension of length 3.") + warnings.warn(RuntimeWarning(msg)) + multichannel = True if multichannel: # do not filter across channels if not isinstance(sigma, coll.Iterable): diff --git a/skimage/filter/tests/test_gaussian.py b/skimage/filter/tests/test_gaussian.py index 77c01a63..9118bfae 100644 --- a/skimage/filter/tests/test_gaussian.py +++ b/skimage/filter/tests/test_gaussian.py @@ -16,12 +16,18 @@ def test_energy_decrease(): def test_multichannel(): - a = np.zeros((3, 3, 3)) + a = np.zeros((5, 5, 3)) 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)]) + # Test multichannel = None + gaussian_rgb_a = gaussian_filter(a, sigma=1, mode='reflect') + # 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