Piecewise Affine Transforms


In [1]:
import numpy as np
from menpo.transform import PiecewiseAffine

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


In [2]:
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)

pwa = PiecewiseAffine(src_points, tgt)

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


In [3]:
%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()


Out[3]:
<menpo.visualize.viewmatplotlib.MatplotlibPointGraphViewer2d at 0x10d235748>

Now lets see the effect having warped


In [4]:
t_points_f = pwa.apply(points_f);
t_points_f.view()


Out[4]:
<menpo.visualize.viewmatplotlib.MatplotlibPointGraphViewer2d at 0x10def5b00>

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

In [6]:
pwa.index_alpha_beta(test)


Out[6]:
(array([6, 5, 5, 5], dtype=uint32),
 array([ 0.9,  0.7,  0.2,  0.5]),
 array([ 0. ,  0.2,  0.1,  0.1]))