1 | function [phi,S] = tps(x1,x2,Y) |
---|
2 | % TPS Compute the thin-plate spline basis |
---|
3 | % PHI=TPS(X1,X2,Y) returns the basis PHI of a thin-plate spline |
---|
4 | % (TPS) defined on the domain X1,X2 with control points Y. |
---|
5 | % |
---|
6 | % X1 and X2 are MxN matrices specifying the grid vertices. When |
---|
7 | % warping images, these usually correspond to image pixels. |
---|
8 | % |
---|
9 | % Y is a 2xK matrix specifying the control points, one per |
---|
10 | % column. Ofthen Y is a subset of the domain X1,X2, but this is not |
---|
11 | % required. |
---|
12 | % |
---|
13 | % PHI is a (K+3)xNxM matrix, with one layer per basis element. Each |
---|
14 | % basis element is a function of the domain X1,X2. |
---|
15 | % |
---|
16 | % [PHI,S] = TPS(X1,X2,Y) additionally returns the stiffness matrix S |
---|
17 | % of the TPS. |
---|
18 | % |
---|
19 | % See also WTPS. |
---|
20 | |
---|
21 | % AUTORIGHTS |
---|
22 | % Copyright (C) 2006 Andrea Vedaldi |
---|
23 | % |
---|
24 | % This file is part of VLUtil. |
---|
25 | % |
---|
26 | % VLUtil is free software; you can redistribute it and/or modify |
---|
27 | % it under the terms of the GNU General Public License as published by |
---|
28 | % the Free Software Foundation; either version 2, or (at your option) |
---|
29 | % any later version. |
---|
30 | % |
---|
31 | % This program is distributed in the hope that it will be useful, |
---|
32 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
33 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
34 | % GNU General Public License for more details. |
---|
35 | % |
---|
36 | % You should have received a copy of the GNU General Public License |
---|
37 | % along with this program; if not, write to the Free Software Foundation, |
---|
38 | % Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
39 | |
---|
40 | X = [x1(:)';x2(:)'] ; |
---|
41 | |
---|
42 | K = size(Y,2) ; |
---|
43 | Q = size(X,2) ; |
---|
44 | U = tpsu(Y,Y) ; |
---|
45 | L = [[ones(1,K); Y], zeros(3) ; U, ones(K,1), Y'] ; |
---|
46 | invL = inv(L) ; |
---|
47 | |
---|
48 | tmp = tpsu(Y,X) ; |
---|
49 | phi = invL * [ ones(1,Q) ; X(1,:) ; X(2,:) ; tmp ] ; |
---|
50 | |
---|
51 | [M,N] = size(x1) ; |
---|
52 | phi = reshape(phi,K+3,M,N) ; |
---|
53 | |
---|
54 | if nargout > 1 |
---|
55 | % See Bookstein; note that here the terms are re-arranged a bit |
---|
56 | invLn = invL(1:K, end-K+1:end) ; |
---|
57 | S = invLn * U * invLn ; |
---|
58 | end |
---|