Merge pull request #1734 from sumitbinnani/RenamingFilter

Refactoring gaussian_filter and gabor_filter (Ref: #1647)
This commit is contained in:
Steven Silvester
2015-10-20 12:56:02 -05:00
10 changed files with 81 additions and 71 deletions
+2
View File
@@ -6,6 +6,8 @@ Version 0.14
------------
* Remove deprecated ``ntiles_*` kwargs in ``equalize_adapthist``.
* Remove deprecated ``skimage.restoration.nl_means_denoising``.
* Remove deprecated ``skimage.filters.gaussian_filter``.
* Remove deprecated ``skimage.filters.gabor_filter``.
Version 0.13
+3 -3
View File
@@ -12,7 +12,7 @@ COLOR_IMAGE = data.astronaut()[::5, ::5]
GRAY_IMAGE = data.camera()[::5, ::5]
SIGMA = 3
smooth = partial(filters.gaussian_filter, sigma=SIGMA)
smooth = partial(filters.gaussian, sigma=SIGMA)
assert_allclose = partial(np.testing.assert_allclose, atol=1e-8)
@@ -23,7 +23,7 @@ def edges_each(image):
@adapt_rgb(each_channel)
def smooth_each(image, sigma):
return filters.gaussian_filter(image, sigma)
return filters.gaussian(image, sigma)
@adapt_rgb(hsv_value)
@@ -33,7 +33,7 @@ def edges_hsv(image):
@adapt_rgb(hsv_value)
def smooth_hsv(image, sigma):
return filters.gaussian_filter(image, sigma)
return filters.gaussian(image, sigma)
@adapt_rgb(hsv_value)
+2 -2
View File
@@ -1,5 +1,5 @@
import numpy as np
from ..filters import gaussian_filter
from ..filters import gaussian
def binary_blobs(length=512, blob_size_fraction=0.1, n_dim=2,
@@ -48,6 +48,6 @@ def binary_blobs(length=512, blob_size_fraction=0.1, n_dim=2,
n_pts = max(int(1. / blob_size_fraction) ** n_dim, 1)
points = (length * rs.rand(n_dim, n_pts)).astype(np.int)
mask[[indices for indices in points]] = 1
mask = gaussian_filter(mask, sigma=0.25 * length * blob_size_fraction)
mask = gaussian(mask, sigma=0.25 * length * blob_size_fraction)
threshold = np.percentile(mask, 100 * (1 - volume_fraction))
return np.logical_not(mask < threshold)
+15 -13
View File
@@ -12,30 +12,32 @@ del warn
del skimage_deprecation
from ..filters.lpi_filter import inverse, wiener, LPIFilter2D
from ..filters._gaussian import gaussian_filter
from ..filters._gaussian import gaussian
from ..filters.edges import (sobel, hsobel, vsobel, sobel_h, sobel_v,
scharr, hscharr, vscharr, scharr_h, scharr_v,
prewitt, hprewitt, vprewitt, prewitt_h, prewitt_v,
roberts, roberts_positive_diagonal,
roberts_negative_diagonal, roberts_pos_diag,
roberts_neg_diag)
scharr, hscharr, vscharr, scharr_h, scharr_v,
prewitt, hprewitt, vprewitt, prewitt_h, prewitt_v,
roberts, roberts_positive_diagonal,
roberts_negative_diagonal, roberts_pos_diag,
roberts_neg_diag)
from ..filters._rank_order import rank_order
from ..filters._gabor import gabor_kernel, gabor_filter
from ..filters._gabor import gabor_kernel, gabor
from ..filters.thresholding import (threshold_adaptive, threshold_otsu, threshold_yen,
threshold_isodata)
threshold_isodata)
from ..filters import rank
from ..filters.rank import median
from .._shared.utils import deprecated
from .. import restoration
denoise_bilateral = deprecated('skimage.restoration.denoise_bilateral')\
(restoration.denoise_bilateral)
(restoration.denoise_bilateral)
denoise_tv_bregman = deprecated('skimage.restoration.denoise_tv_bregman')\
(restoration.denoise_tv_bregman)
(restoration.denoise_tv_bregman)
denoise_tv_chambolle = deprecated('skimage.restoration.denoise_tv_chambolle')\
(restoration.denoise_tv_chambolle)
(restoration.denoise_tv_chambolle)
# Backward compatibility v<0.11
@deprecated('skimage.feature.canny')
def canny(*args, **kwargs):
# Hack to avoid circular import
@@ -46,7 +48,7 @@ def canny(*args, **kwargs):
__all__ = ['inverse',
'wiener',
'LPIFilter2D',
'gaussian_filter',
'gaussian',
'median',
'canny',
'sobel',
@@ -74,7 +76,7 @@ __all__ = ['inverse',
'denoise_tv_bregman',
'rank_order',
'gabor_kernel',
'gabor_filter',
'gabor',
'threshold_adaptive',
'threshold_otsu',
'threshold_yen',
+15 -9
View File
@@ -1,5 +1,5 @@
from .lpi_filter import inverse, wiener, LPIFilter2D
from ._gaussian import gaussian_filter
from ._gaussian import gaussian
from .edges import (sobel, hsobel, vsobel, sobel_h, sobel_v,
scharr, hscharr, vscharr, scharr_h, scharr_v,
prewitt, hprewitt, vprewitt, prewitt_h, prewitt_v,
@@ -7,22 +7,28 @@ from .edges import (sobel, hsobel, vsobel, sobel_h, sobel_v,
roberts_negative_diagonal, roberts_pos_diag,
roberts_neg_diag)
from ._rank_order import rank_order
from ._gabor import gabor_kernel, gabor_filter
from ._gabor import gabor_kernel, gabor
from .thresholding import (threshold_adaptive, threshold_otsu, threshold_yen,
threshold_isodata, threshold_li)
from . import rank
from .rank import median
from .._shared.utils import deprecated
from .._shared.utils import deprecated, copy_func
from .. import restoration
denoise_bilateral = deprecated('skimage.restoration.denoise_bilateral')\
(restoration.denoise_bilateral)
(restoration.denoise_bilateral)
denoise_tv_bregman = deprecated('skimage.restoration.denoise_tv_bregman')\
(restoration.denoise_tv_bregman)
(restoration.denoise_tv_bregman)
denoise_tv_chambolle = deprecated('skimage.restoration.denoise_tv_chambolle')\
(restoration.denoise_tv_chambolle)
(restoration.denoise_tv_chambolle)
gaussian_filter = copy_func(gaussian, name='gaussian_filter')
gaussian_filter = deprecated('skimage.filters.gaussian')(gaussian_filter)
gabor_filter = copy_func(gabor, name='gabor_filter')
gabor_filter = deprecated('skimage.filters.gabor')(gabor_filter)
# Backward compatibility v<0.11
@deprecated('skimage.feature.canny')
def canny(*args, **kwargs):
# Hack to avoid circular import
@@ -33,7 +39,7 @@ def canny(*args, **kwargs):
__all__ = ['inverse',
'wiener',
'LPIFilter2D',
'gaussian_filter',
'gaussian',
'median',
'canny',
'sobel',
@@ -61,10 +67,10 @@ __all__ = ['inverse',
'denoise_tv_bregman',
'rank_order',
'gabor_kernel',
'gabor_filter',
'gabor',
'threshold_adaptive',
'threshold_otsu',
'threshold_yen',
'threshold_isodata',
'threshold_li',
'threshold_li',
'rank']
+6 -6
View File
@@ -3,7 +3,7 @@ from scipy import ndimage as ndi
from .._shared.utils import assert_nD
__all__ = ['gabor_kernel', 'gabor_filter']
__all__ = ['gabor_kernel', 'gabor']
def _sigma_prefactor(bandwidth):
@@ -94,8 +94,8 @@ def gabor_kernel(frequency, theta=0, bandwidth=1, sigma_x=None, sigma_y=None,
return g
def gabor_filter(image, frequency, theta=0, bandwidth=1, sigma_x=None,
sigma_y=None, n_stds=3, offset=0, mode='reflect', cval=0):
def gabor(image, frequency, theta=0, bandwidth=1, sigma_x=None,
sigma_y=None, n_stds=3, offset=0, mode='reflect', cval=0):
"""Return real and imaginary responses to Gabor filter.
The real and imaginary parts of the Gabor filter kernel are applied to the
@@ -148,19 +148,19 @@ def gabor_filter(image, frequency, theta=0, bandwidth=1, sigma_x=None,
Examples
--------
>>> from skimage.filter import gabor_filter
>>> from skimage.filter import gabor
>>> from skimage import data, io
>>> from matplotlib import pyplot as plt # doctest: +SKIP
>>> image = data.coins()
>>> # detecting edges in a coin image
>>> filt_real, filt_imag = gabor_filter(image, frequency=0.6)
>>> filt_real, filt_imag = gabor(image, frequency=0.6)
>>> plt.figure() # doctest: +SKIP
>>> io.imshow(filt_real) # doctest: +SKIP
>>> io.show() # doctest: +SKIP
>>> # less sensitivity to finer details with the lower frequency kernel
>>> filt_real, filt_imag = gabor_filter(image, frequency=0.1)
>>> filt_real, filt_imag = gabor(image, frequency=0.1)
>>> plt.figure() # doctest: +SKIP
>>> io.imshow(filt_real) # doctest: +SKIP
>>> io.show() # doctest: +SKIP
+7 -7
View File
@@ -6,11 +6,11 @@ import warnings
from ..util import img_as_float
from ..color import guess_spatial_dimensions
__all__ = ['gaussian_filter']
__all__ = ['gaussian']
def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0,
multichannel=None):
def gaussian(image, sigma, output=None, mode='nearest', cval=0,
multichannel=None):
"""Multi-dimensional Gaussian filter
Parameters
@@ -66,23 +66,23 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0,
array([[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 0.]])
>>> gaussian_filter(a, sigma=0.4) # mild smoothing
>>> gaussian(a, sigma=0.4) # mild smoothing
array([[ 0.00163116, 0.03712502, 0.00163116],
[ 0.03712502, 0.84496158, 0.03712502],
[ 0.00163116, 0.03712502, 0.00163116]])
>>> gaussian_filter(a, sigma=1) # more smooting
>>> gaussian(a, sigma=1) # more smooting
array([[ 0.05855018, 0.09653293, 0.05855018],
[ 0.09653293, 0.15915589, 0.09653293],
[ 0.05855018, 0.09653293, 0.05855018]])
>>> # Several modes are possible for handling boundaries
>>> gaussian_filter(a, sigma=1, mode='reflect')
>>> gaussian(a, sigma=1, mode='reflect')
array([[ 0.08767308, 0.12075024, 0.08767308],
[ 0.12075024, 0.16630671, 0.12075024],
[ 0.08767308, 0.12075024, 0.08767308]])
>>> # For RGB images, each is filtered separately
>>> from skimage.data import astronaut
>>> image = astronaut()
>>> filtered_img = gaussian_filter(image, sigma=1, multichannel=True)
>>> filtered_img = gaussian(image, sigma=1, multichannel=True)
"""
+7 -7
View File
@@ -2,7 +2,7 @@ import numpy as np
from numpy.testing import (assert_equal, assert_almost_equal,
assert_array_almost_equal)
from skimage.filters._gabor import gabor_kernel, gabor_filter, _sigma_prefactor
from skimage.filters._gabor import gabor_kernel, gabor, _sigma_prefactor
def test_gabor_kernel_size():
@@ -15,7 +15,7 @@ def test_gabor_kernel_size():
kernel = gabor_kernel(0, theta=0, sigma_x=sigma_x, sigma_y=sigma_y)
assert_equal(kernel.shape, (size_y, size_x))
kernel = gabor_kernel(0, theta=np.pi/2, sigma_x=sigma_x, sigma_y=sigma_y)
kernel = gabor_kernel(0, theta=np.pi / 2, sigma_x=sigma_x, sigma_y=sigma_y)
assert_equal(kernel.shape, (size_x, size_y))
@@ -39,7 +39,7 @@ def test_gabor_kernel_sum():
for sigma_x in range(1, 10, 2):
for sigma_y in range(1, 10, 2):
for frequency in range(0, 10, 2):
kernel = gabor_kernel(frequency+0.1, theta=0,
kernel = gabor_kernel(frequency + 0.1, theta=0,
sigma_x=sigma_x, sigma_y=sigma_y)
# make sure gaussian distribution is covered nearly 100%
assert_almost_equal(np.abs(kernel).sum(), 1, 2)
@@ -50,22 +50,22 @@ def test_gabor_kernel_theta():
for sigma_y in range(1, 10, 2):
for frequency in range(0, 10, 2):
for theta in range(0, 10, 2):
kernel0 = gabor_kernel(frequency+0.1, theta=theta,
kernel0 = gabor_kernel(frequency + 0.1, theta=theta,
sigma_x=sigma_x, sigma_y=sigma_y)
kernel180 = gabor_kernel(frequency, theta=theta+np.pi,
kernel180 = gabor_kernel(frequency, theta=theta + np.pi,
sigma_x=sigma_x, sigma_y=sigma_y)
assert_array_almost_equal(np.abs(kernel0),
np.abs(kernel180))
def test_gabor_filter():
def test_gabor():
Y, X = np.mgrid[:40, :40]
frequencies = (0.1, 0.3)
wave_images = [np.sin(2 * np.pi * X * f) for f in frequencies]
def match_score(image, frequency):
gabor_responses = gabor_filter(image, frequency)
gabor_responses = gabor(image, frequency)
return np.mean(np.hypot(*gabor_responses))
# Gabor scores: diagonals are frequency-matched, off-diagonals are not.
+14 -14
View File
@@ -1,52 +1,52 @@
import numpy as np
from numpy.testing import assert_raises
from skimage.filters._gaussian import gaussian_filter
from skimage.filters._gaussian import gaussian
from skimage._shared._warnings import expected_warnings
def test_negative_sigma():
a = np.zeros((3, 3))
a[1, 1] = 1.
assert_raises(ValueError, gaussian_filter, a, sigma=-1.0)
assert_raises(ValueError, gaussian_filter, a, sigma=[-1.0, 1.0])
assert_raises(ValueError, gaussian_filter, a,
assert_raises(ValueError, gaussian, a, sigma=-1.0)
assert_raises(ValueError, gaussian, a, sigma=[-1.0, 1.0])
assert_raises(ValueError, gaussian, a,
sigma=np.asarray([-1.0, 1.0]))
def test_null_sigma():
a = np.zeros((3, 3))
a[1, 1] = 1.
assert np.all(gaussian_filter(a, 0) == a)
assert np.all(gaussian(a, 0) == a)
def test_energy_decrease():
a = np.zeros((3, 3))
a[1, 1] = 1.
gaussian_a = gaussian_filter(a, sigma=1, mode='reflect')
gaussian_a = gaussian(a, sigma=1, mode='reflect')
assert gaussian_a.std() < a.std()
def test_multichannel():
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)
gaussian_rgb_a = gaussian(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)])
[gaussian_rgb_a[..., i].mean() for i in range(3)])
# Test multichannel = None
with expected_warnings(['multichannel']):
gaussian_rgb_a = gaussian_filter(a, sigma=1, mode='reflect')
gaussian_rgb_a = gaussian(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)])
[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)
gaussian_rgb_a = gaussian(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)])
[gaussian_rgb_a[..., i].mean() for i in range(3)])
if __name__ == "__main__":
+10 -10
View File
@@ -3,7 +3,7 @@ from __future__ import absolute_import
import numpy as np
from numpy.testing import assert_array_almost_equal
from skimage.filters import threshold_adaptive, gaussian_filter
from skimage.filters import threshold_adaptive, gaussian
from skimage.util.apply_parallel import apply_parallel
@@ -14,15 +14,15 @@ def test_apply_parallel():
# apply the filter
expected1 = threshold_adaptive(a, 3)
result1 = apply_parallel(threshold_adaptive, a, chunks=(6, 6), depth=5,
extra_arguments=(3,),
extra_keywords={'mode': 'reflect'})
extra_arguments=(3,),
extra_keywords={'mode': 'reflect'})
assert_array_almost_equal(result1, expected1)
def wrapped_gauss(arr):
return gaussian_filter(arr, 1, mode='reflect')
return gaussian(arr, 1, mode='reflect')
expected2 = gaussian_filter(a, 1, mode='reflect')
expected2 = gaussian(a, 1, mode='reflect')
result2 = apply_parallel(wrapped_gauss, a, chunks=(6, 6), depth=5)
assert_array_almost_equal(result2, expected2)
@@ -42,9 +42,9 @@ def test_no_chunks():
def test_apply_parallel_wrap():
def wrapped(arr):
return gaussian_filter(arr, 1, mode='wrap')
return gaussian(arr, 1, mode='wrap')
a = np.arange(144).reshape(12, 12).astype(float)
expected = gaussian_filter(a, 1, mode='wrap')
expected = gaussian(a, 1, mode='wrap')
result = apply_parallel(wrapped, a, chunks=(6, 6), depth=5, mode='wrap')
assert_array_almost_equal(result, expected)
@@ -52,10 +52,10 @@ def test_apply_parallel_wrap():
def test_apply_parallel_nearest():
def wrapped(arr):
return gaussian_filter(arr, 1, mode='nearest')
return gaussian(arr, 1, mode='nearest')
a = np.arange(144).reshape(12, 12).astype(float)
expected = gaussian_filter(a, 1, mode='nearest')
expected = gaussian(a, 1, mode='nearest')
result = apply_parallel(wrapped, a, chunks=(6, 6), depth={0: 5, 1: 5},
mode='nearest')
mode='nearest')
assert_array_almost_equal(result, expected)