morphops.tps

Provides thin-plate splines related operations and algorithms.

Given two sets of points, the thin-plate spline can interpolate from one to the other in a manner that minimizes the “integral bending norm”[bookstein89].

Importantly, it has a remarkable connection to Kendall’s shape space in the following way: The non-zero eigenvectors of the bending energy matrix form an orthonormal basis in the tangent space of shape coordinates [bookstein96].

References

[bookstein89]Bookstein, F.L., 1989. Principal warps: Thin-plate splines and the decomposition of deformations. IEEE Transactions on pattern analysis and machine intelligence, 11(6), pp.567-585.
[bookstein96]Bookstein, F.L., 1996. Biometrics, biomathematics and the morphometric synthesis. Bulletin of mathematical biology, 58(2), p.313.
morphops.tps.K_matrix(X, Y=None)[source]

Calculates the upper-right (p,p) submatrix of the (p+k+1,p+k+1)-shaped L matrix.

Parameters:
  • X ((p,2) or (p,3) shaped array-like) – A (p,k) array of p points in k=2 or k=3 dimensions.
  • Y ((m,2) or (m,3) shaped array-like, optional) –

    A (m,k) array of p points in k=2 or k=3 dimensions. Y must have the same k as X.

    If Y is None, it is just set to X.

Returns:

K – A (p,p) array where the element at [i,j] is \(U(\|X_i - Y_j\|)\). The definition of U depends on k.

In particular, if k = 2, then \(U(r) = r^2 \log(r^2)\), else \(U(r) = r\).

Note: Using \(\alpha U(r)\) instead of \(U(r)\) for some \(\alpha \in \mathbb{R}\) will not change the calculated spline. Simple block matrix inverse formulae show that when calculating \(L^{-1}\) for the spline using \(\alpha U(r)\), the non-uniform coefficients multiplied to the \(U\) terms will be scaled by \(\frac{1}{\alpha}\) while the uniform coefficients will stay the same.

Return type:

np.ndarray

morphops.tps.L_matrix(X)[source]

Makes the (p+k+1,p+k+1)-shaped L matrix that gets inverted when calculating the thin-plate spline “from” X.

Parameters:X ((p,2) or (p,3) shaped array-like) – A (p,k) array of p landmarks in k=2 or k=3 dimensions for one specimen.
Returns:L – A (p+k+1,p+k+1) array of the form [[K | P][P.T | 0]].
Return type:np.ndarray
morphops.tps.P_matrix(X)[source]

Makes the minor diagonal submatrix P of the (p+k+1,p+k+1)-shaped L matrix.

Basically just stacks a column of 1s before the coordinate columns in X.

Parameters:X ((p,2) or (p,3) shaped array-like) – A (p,k) array of p points in k=2 or k=3 dimensions.
Returns:P – A (p,k+1) array, which is 1 in the first column, and exactly X in the remaining columns.
Return type:np.ndarray
morphops.tps.bending_energy_matrix(X)[source]

Returns the upper right (pxp) submatrix of L^(-1).

Parameters:X ((p,2) or (p,3) shaped array-like) – A (p,k) array of p landmarks in k=2 or k=3 dimensions for one specimen.
Returns:L_inv – The upper right (p,p) submatrix of the inverse of the L_matrix of X.
Return type:np.ndarray
morphops.tps.tps_coefs(X, Y)[source]

Finds the thin-plate spline coefficients for the thin-plate spline function that interpolates from X to Y.

Parameters:
  • X ((p,2) or (p,3) shaped array-like) – A (p,k) array of p points in k=2 or k=3 dimensions.
  • Y ((p,2) or (p,3) shaped array-like) – A (p,k) array of p points in k=2 or k=3 dimensions. Y must have the same shape as X.
Returns:

  • W (np.ndarray) – A (p,k) array of weights for the non-affine part of the spline.
  • A (np.ndarray) – A (k+1,k) array of weights for the affine part of the spline.

morphops.tps.tps_warp(X, Y, pts)[source]

Maps points pts to their image under the thin-plate spline function generated by tps_coefs() of X and Y.

Parameters:
  • X ((p,2) or (p,3) shaped array-like) – A (p,k) array of p points in k=2 or k=3 dimensions.
  • Y ((p,2) or (p,3) shaped array-like) – A (p,k) array of p points in k=2 or k=3 dimensions. Y must have the same shape as X.
  • pts ((m,2) or (m,3) shaped array-like, optional) – A (m,k) array of m points in k=2 or k=3 dimensions. pts must have the same coordinate dimensions k as X.
Returns:

warped_pts – A (m,k) array of points corresponding to the image of pts under the thin-plate spline produced by X, Y.

Return type:

(m,2) or (m,3) shaped array-like, optional