Skip to content

Commit

Permalink
Merge pull request #750 from jabooth/copy_landmarks_and_path
Browse files Browse the repository at this point in the history
Copy landmarks and path
  • Loading branch information
Patrick Snape committed Nov 18, 2016
2 parents e8a7382 + bea5a83 commit 23a439c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
26 changes: 26 additions & 0 deletions menpo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,29 @@ def partial_doc(func, *args, **kwargs):
p = partial(func, *args, **kwargs)
p.__doc__ = func.__doc__
return p


def copy_landmarks_and_path(source, target):
r"""
Transfers over the landmarks and path, if any, from one object to another.
This should be called in conversion and copy functions.
See `.as_masked()` on :map:`Image` as an example of usage.
Parameters
----------
source : :map:`Landmarkable`
The object who's landmarks and path, if any, will be copied
target : :map:`Landmarkable`
The object who will have landmarks and path set on
Returns
-------
target : :map:`Landmarkable`
The updated target.
"""
if source.has_landmarks:
target.landmarks = source.landmarks
if hasattr(source, 'path'):
target.path = source.path
return target
11 changes: 5 additions & 6 deletions menpo/image/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import PIL.Image as PILImage

from menpo.compatibility import basestring
from menpo.base import Vectorizable, MenpoDeprecationWarning
from menpo.base import (Vectorizable, MenpoDeprecationWarning,
copy_landmarks_and_path)
from menpo.shape import PointCloud, bounding_box
from menpo.landmark import Landmarkable
from menpo.transform import (Translation, NonUniformScale, Rotation,
Expand Down Expand Up @@ -428,11 +429,9 @@ def as_masked(self, mask=None, copy=True):
a mask.
"""
from menpo.image import MaskedImage
img = MaskedImage(self.pixels, mask=mask, copy=copy)
img.landmarks = self.landmarks
if hasattr(self, 'path'):
img.path = self.path
return img
return copy_landmarks_and_path(self,
MaskedImage(self.pixels,
mask=mask, copy=copy))

@property
def n_dims(self):
Expand Down
9 changes: 2 additions & 7 deletions menpo/image/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
binary_erosion = None # expensive, from scipy.ndimage
binary_dilation = None # expensive, from scipy.ndimage

from menpo.base import MenpoDeprecationWarning
from menpo.base import MenpoDeprecationWarning, copy_landmarks_and_path
from menpo.transform import Translation
from menpo.visualize.base import ImageViewer

Expand Down Expand Up @@ -245,12 +245,7 @@ def as_unmasked(self, copy=True, fill=None):
if not np.isscalar(fill):
fill = np.array(fill).reshape(self.n_channels, -1)
img.pixels[..., ~self.mask.mask] = fill

if self.has_landmarks:
img.landmarks = self.landmarks
if hasattr(self, 'path'):
img.path = self.path
return img
return copy_landmarks_and_path(self, img)

def n_true_pixels(self):
r"""
Expand Down
30 changes: 30 additions & 0 deletions menpo/test/copy_landmarks_and_path_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import menpo.io as mio
from menpo.base import copy_landmarks_and_path
from menpo.image import Image


def test_copy_landmarks_and_path():
img = mio.import_builtin_asset.lenna_png()
new_img = Image.init_blank(img.shape)
copy_landmarks_and_path(img, new_img)

assert new_img.path == img.path
assert new_img.landmarks.keys() == img.landmarks.keys()
assert new_img.landmarks is not img.landmarks


def test_copy_landmarks_and_path_returns_target():
img = mio.import_builtin_asset.lenna_png()
new_img = Image.init_blank(img.shape)
new_img_ret = copy_landmarks_and_path(img, new_img)
assert new_img_ret is new_img


def test_copy_landmarks_and_path_with_no_lms_path():
img = Image.init_blank((5, 5))
new_img = Image.init_blank((5, 5))
copy_landmarks_and_path(img, new_img)
assert not hasattr(img, 'path')
assert not hasattr(new_img, 'path')
assert not img.has_landmarks
assert not new_img.has_landmarks

0 comments on commit 23a439c

Please sign in to comment.