replace _discard_edges with skimage.util.arraypad.crop

This commit is contained in:
Gregory R. Lee
2015-05-15 10:32:56 -04:00
parent 3bf6cc106e
commit 86e07447d1
2 changed files with 4 additions and 55 deletions
+3 -35
View File
@@ -6,6 +6,7 @@ import numpy as np
from scipy.ndimage.filters import uniform_filter, convolve1d
from ..util.dtype import dtype_range
from ..util.arraypad import crop
def gaussian_filter2(X, sigma=1.5, size=11):
@@ -41,39 +42,6 @@ def gaussian_filter2(X, sigma=1.5, size=11):
return X
def _discard_edges(X, pad):
""" Remove border of width pad from ndarray X.
Parameters
----------
X : ndarray
image
pad : int or sequence of ints
border width to remove. Can be a list of values corresponding to each
axis. If pad is an integer, the same width is removed from all axes.
Returns
-------
Y : nadarray
image with edges removed
"""
X = np.asanyarray(X)
if pad == 0:
return X
if isinstance(pad, int):
slice_array = [slice(pad, -pad), ] * X.ndim
else:
if len(pad) != X.ndim:
raise ValueError("pad array must match number of X dimensions")
slice_array = []
for d in range(X.ndim):
slice_array.append(slice(pad[d], -pad[d]))
return X[slice_array]
def structural_similarity(X, Y, win_size=None, gradient=False,
dynamic_range=None, multichannel=None,
gaussian_weights=False, full=False,
@@ -278,9 +246,9 @@ def structural_similarity(X, Y, win_size=None, gradient=False,
# weight with Eq. 7 of Wang and Simoncelli 2006.
W = np.log((1 + vx / C2) * (1 + vy / C2))
W /= W.sum()
mssim = _discard_edges(S * W, pad).sum()
mssim = crop(S * W, pad).sum()
else:
mssim = _discard_edges(S, pad).mean()
mssim = crop(S, pad).mean()
if gradient:
# The following is Eqs. 7-8 of Avanaki 2009.
@@ -5,8 +5,7 @@ from numpy.testing import (assert_equal, assert_raises, assert_almost_equal,
assert_array_almost_equal)
from skimage.measure import structural_similarity as ssim
from skimage.measure._structural_similarity import (gaussian_filter2,
_discard_edges)
from skimage.measure._structural_similarity import (gaussian_filter2)
import skimage.data
from skimage.io import imread
from skimage import data_dir
@@ -232,23 +231,5 @@ def test_gaussian_filter2():
assert np.all(xf[:, :3] == 0)
def test_discard_edges():
x = np.zeros((11, 11))
x[3:8, 3:8] = 1.0
xd = _discard_edges(x, 3)
assert xd.shape == (5, 5)
assert np.all(xd == 1.0)
# non-uniform edge case
x = np.zeros((11, 11))
x[3:8, 1:10] = 1.0
xd = _discard_edges(x, [3, 1])
assert xd.shape == (5, 9)
assert np.all(xd == 1.0)
assert_raises(ValueError, _discard_edges, x, [3, 3, 3])
assert_raises(TypeError, _discard_edges, x, 3.5)
if __name__ == "__main__":
np.testing.run_module_suite()