In [ ]:
import numpy as np
from menpo.transform.piecewiseaffine import (
    DiscreteAffinePWATransform, CachedPWATransform, 
    TriangleContainmentError, PiecewiseAffineTransform)

We build a PiecewiseAffineTransform by supplying two sets of points and a shared triangle list


In [ ]:
from menpo.shape import TriMesh, PointCloud
a = np.array([[0, 0], [1, 0], [0, 1], [1, 1],
              [-0.5, -0.7], [0.8, -0.4], [0.9, -2.1]])
b = np.array([[0,0], [2, 0], [-1, 3], [2, 6],
              [-1.0, -0.01], [1.0, -0.4], [0.8, -1.6]])
tl = np.array([[0,2,1], [1,3,2]])

src = TriMesh(a, tl)
src_points = PointCloud(a)
tgt = PointCloud(b)

# broken until Affine is updated
#slow_pwa = DiscreteAffinePWATransform(src, tgt)
fast_pwa = CachedPWATransform(src, tgt)
# pwa is just a CachedPWATransform alias
# a PointCloud source results in Delaunay being used.
pwa = PiecewiseAffineTransform(src_points, tgt)

Lets make a random 5000 point PointCloud in the unit square and view it


In [ ]:
%matplotlib inline
points_s = PointCloud(np.random.rand(10000).reshape([-1,2]))
points_f = PointCloud(np.random.rand(10000).reshape([-1,2]))
points_f.view()

Now lets see the effect having warped


In [ ]:
#slow_pwa.apply_inplace(points_s);
fast_pwa.apply_inplace(points_f);
points_f.view()

In [ ]:
test = np.array([[0.1,0.1], [0.7, 0.9], 
                 [0.2,0.3], [0.5, 0.6]])

In [ ]:
#slow_pwa.index_alpha_beta(test)
fast_pwa.index_alpha_beta(test)

In [ ]:
#slow_pwa.apply_inplace(test)
fast_pwa.apply_inplace(test)
print test