Skip to content

Commit

Permalink
Merge pull request #441 from jabooth/booleanconstrain
Browse files Browse the repository at this point in the history
adds constrain_to_landmarks on BooleanImage
  • Loading branch information
jabooth committed Sep 11, 2014
2 parents a34a9e0 + a27af56 commit 6008825
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
61 changes: 61 additions & 0 deletions menpo/image/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,64 @@ def _build_warped_image(self, template_mask, sampled_pixel_values,
# more manually than other image classes.
warped_image.pixels[warped_image.mask] = sampled_pixel_values
return warped_image

def constrain_to_landmarks(self, group=None, label=None, trilist=None):
r"""
Restricts this mask to be equal to the convex hull around the
landmarks chosen.
Parameters
----------
group : string, Optional
The key of the landmark set that should be used. If None,
and if there is only one set of landmarks, this set will be used.
Default: None
label: string, Optional
The label of of the landmark manager that you wish to use. If no
label is passed, the convex hull of all landmarks is used.
Default: None
trilist: (t, 3) ndarray, Optional
Triangle list to be used on the landmarked points in selecting
the mask region. If None defaults to performing Delaunay
triangulation on the points.
Default: None
"""
self.constrain_to_pointcloud(self.landmarks[group][label],
trilist=trilist)

def constrain_to_pointcloud(self, pointcloud, trilist=None):
r"""
Restricts this mask to be equal to the convex hull around a point cloud
Parameters
----------
pointcloud : :map:`PointCloud`
The pointcloud of points that should be constrained to
trilist: (t, 3) ndarray, Optional
Triangle list to be used on the points in selecting
the mask region. If None defaults to performing Delaunay
triangulation on the points.
Default: None
"""
from menpo.transform.piecewiseaffine import PiecewiseAffine
from menpo.transform.piecewiseaffine import TriangleContainmentError

if self.n_dims != 2:
raise ValueError("can only constrain mask on 2D images.")

if trilist is not None:
from menpo.shape import TriMesh
pointcloud = TriMesh(pointcloud.points, trilist)

pwa = PiecewiseAffine(pointcloud, pointcloud)
try:
pwa.apply(self.indices)
except TriangleContainmentError as e:
self.from_vector_inplace(~e.points_outside_source_domain)
22 changes: 2 additions & 20 deletions menpo/image/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ def gradient(self, nullify_values_at_mask_boundaries=False):
grad_image.landmarks = self.landmarks
return grad_image

# TODO maybe we should be stricter about the trilist here, feels flakey
def constrain_mask_to_landmarks(self, group=None, label=None,
trilist=None):
r"""
Expand Down Expand Up @@ -621,25 +620,8 @@ def constrain_mask_to_landmarks(self, group=None, label=None,
Default: None
"""
from menpo.transform.piecewiseaffine import PiecewiseAffine
from menpo.transform.piecewiseaffine import TriangleContainmentError

if self.n_dims != 2:
raise ValueError("can only constrain mask on 2D images.")

pc = self.landmarks[group][label]
if trilist is not None:
from menpo.shape import TriMesh

pc = TriMesh(pc.points, trilist)

pwa = PiecewiseAffine(pc, pc)
try:
# Call the superclass indices property because we actually want
# ALL the indices, not just the true ones.
pwa.apply(Image.indices.fget(self))
except TriangleContainmentError as e:
self.mask.from_vector_inplace(~e.points_outside_source_domain)
self.mask.constrain_to_pointcloud(self.landmarks[group][label],
trilist=trilist)

def rescale(self, scale, interpolator='scipy', round='ceil', **kwargs):
r"""A copy of this MaskedImage, rescaled by a given factor.
Expand Down

0 comments on commit 6008825

Please sign in to comment.