Skip to content

Commit

Permalink
remove cython gradient implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jabooth authored and patricksnape committed Jul 12, 2019
1 parent 846bc4f commit 53f489d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 89 deletions.
1 change: 0 additions & 1 deletion menpo/feature/.gitignore
@@ -1,2 +1 @@
windowiterator.cpp
_gradient.cpp
33 changes: 0 additions & 33 deletions menpo/feature/_gradient.pyx

This file was deleted.

52 changes: 18 additions & 34 deletions menpo/feature/features.py
Expand Up @@ -5,39 +5,9 @@
scipy_gaussian_filter = None # expensive

from .base import ndfeature, winitfeature, imgfeature
from ._gradient import gradient_cython
from .windowiterator import WindowIterator, WindowIteratorResult


def _np_gradient(pixels):
"""
This method is used in the case of multi-channel images (not 2D images).
The output ordering is identical to the gradient() method, returning
a 2 * n_channels image with gradients in order of the first axis derivative
over all the channels, then the second etc. For example, in the case of
a 3D image with 2 channels, the ordering would be:
I[:, 0, 0, 0] = [A_0, B_0, A_1, B_1, A_2, B_2]
where A and B are the 'channel' labels (synonymous with RGB for a colour
image) and 0,1,2 are the axis labels (synonymous with y,x for a 2D image).
"""
n_dims = pixels.ndim - 1
grad_per_dim_per_channel = [np.gradient(g, edge_order=1)
for g in pixels]
# Flatten out the separate dims
grad_per_channel = list(itertools.chain.from_iterable(
grad_per_dim_per_channel))
# Add a channel axis for broadcasting
grad_per_channel = [g[None, ...] for g in grad_per_channel]

# Permute the list so it is first axis, second axis, etc
grad_per_channel = [grad_per_channel[i::n_dims]
for i in range(n_dims)]
grad_per_channel = list(itertools.chain.from_iterable(grad_per_channel))

# Concatenate gradient list into an array (the new_image)
return np.concatenate(grad_per_channel, axis=0)


@ndfeature
def gradient(pixels):
r"""
Expand Down Expand Up @@ -69,10 +39,24 @@ def gradient(pixels):
all the ``y``-gradients are returned over each channel, then all
the ``x``-gradients.
"""
if (pixels.ndim - 1) == 2: # 2D Image
return gradient_cython(pixels)
else:
return _np_gradient(pixels)
if pixels.dtype == np.uint8:
raise TypeError("Attempting to take the gradient on a uint8 image.")
n_dims = pixels.ndim - 1
grad_per_dim_per_channel = [np.gradient(g, edge_order=1)
for g in pixels]
# Flatten out the separate dims
grad_per_channel = list(itertools.chain.from_iterable(
grad_per_dim_per_channel))
# Add a channel axis for broadcasting
grad_per_channel = [g[None, ...] for g in grad_per_channel]

# Permute the list so it is first axis, second axis, etc
grad_per_channel = [grad_per_channel[i::n_dims]
for i in range(n_dims)]
grad_per_channel = list(itertools.chain.from_iterable(grad_per_channel))

# Concatenate gradient list into an array (the new_image)
return np.concatenate(grad_per_channel, axis=0)


@ndfeature
Expand Down
21 changes: 0 additions & 21 deletions menpo/feature/test/test_gradient.py
Expand Up @@ -4,7 +4,6 @@
from menpo.image import Image
from menpo.feature import gradient

from menpo.feature.features import _np_gradient
import menpo.io as mio


Expand All @@ -26,17 +25,6 @@ def test_gradient_float():
assert_allclose(grad_image.pixels[1], np_grad[1])


def test_gradient_takeo_float32():
dtype = np.float32
t = takeo.copy()
t.pixels = t.pixels.astype(dtype)
grad_image = gradient(t)
_check_assertions(grad_image, t.shape, t.n_channels * 2,
dtype)
np_grad = _np_gradient(t.pixels)
assert_allclose(grad_image.pixels, np_grad)


def test_gradient_double():
dtype = np.float64
p = example_image.astype(dtype)
Expand All @@ -49,15 +37,6 @@ def test_gradient_double():
assert_allclose(grad_image.pixels[1], np_grad[1])


def test_gradient_takeo_double():
t = takeo.copy()
t.pixels = t.pixels.astype(np.float64)
grad_image = gradient(t)

np_grad = _np_gradient(t.pixels)
assert_allclose(grad_image.pixels, np_grad)


def test_gradient_uint8_exception():
image = Image(example_image.astype(np.uint8))
with raises(TypeError):
Expand Down

0 comments on commit 53f489d

Please sign in to comment.