Thin Plate Spline

給定兩張圖片中一些相互對應的關鍵點,如何能夠將其中一張圖片形變到另外一張圖片上使得這些關鍵點都對應重合?這就是TPS方法所要解決的問題,TPS可以對表面進行柔性的變形。

Total TPS spline surfaces for the geometric transformation between Acaste (red) and Calymene (green). (A) Basal (non-deformed) grid for Acaste landmark configuration. (B) Basal (non-deformed) grid for Calymeme landmark configuration. (C) Thin plate spline surface for the Calymeme-Acaste transformation. (D) Thin plate spline surface for the Acaste-Calymeme transformation.

Thin Plate Spline(TPS,薄板樣條)插值是常用的2D插值方法。它的物理意義是:假設在原形狀中有N個點A_n,這N個點在形變之后新坐標之下對應新的N個點B_n。用一個薄鋼板的形變來模擬2D形變,確保這N個點能夠正確匹配,那么怎樣的形變,可以使鋼板的彎曲能量最小?TPS插值是這個問題的數值解法。

幾乎所有的生物有關的形變都是可以用TPS來近似,Bookstein本人就是生物形態計量的大師。

樣條曲線插值

線性插值對每個區間[x_k,x_{k+1}]使用線性函數。 樣條插值在每個間隔中使用低階多項式,并選擇多項式以使它們平滑地吻合在一起。 結果函數被稱為樣條曲線。 例如,三次樣條是分片段立方,兩次連續可微。此外,它的二階導數在終點為零。

薄板樣條

薄板樣條插值使薄板的彎曲能量最小,不過把2D圖像看成薄板沒錯,但是彎曲的能量并不是N個點形變對應位置所產生在薄板內的“彎曲”。實際上,這個樣條函數是對每一個維度的坐標分別進行插值,在后面也會看到代碼會對坐標進行一個展平的操作。每一維的形變施加于垂直于薄板的方向,簡單地說就是針對二維xy平面在它的z方向上進行變換,讓在這個維度上的薄板往上凸或者往下凹從而完成這個薄板的形變。

考慮這樣一個插值問題:自變量 \mathbf{x} 是2維空間中的一點,函數值 \mathbf{y} 也是2維空間中的一點,并且都在笛卡爾坐標系下表示。給定 N 個自變量 \mathbf{x}_k 和對應的函數值 \mathbf{y}_k ,求插值函數。[1]

已知K個控制點 \{c_i, i=1, \dots, K\} 用徑向基函數[2]可以將原來的坐標變換到另一個坐標去:

\Phi(x)=\sum^K_{i=1}w_i \sigma(||x-c_i||)

其中 \sigma( r )=r^2\log r 是徑向基函數核。也可以看到每個新的值都是會受到所有其他非一一對應控制點的影響。

\Phi(\mathbf{x})=\begin{bmatrix}\Phi_1(\mathbf{x})\\\Phi_2(\mathbf{x})\end{bmatrix}

這里是2維空間,所以是兩個插值函數,如果是D維那就是求D個插值函數,可以寫成向量形式:

\mathbf{y}_k = \Phi(\mathbf{x}_k).

其中插值函數\Phi_1可以寫成:

\Phi_1(\mathbf{x})=\mathbf{c}+\mathbf{a}^T \mathbf{x}+\mathbf{w}^T \mathbf{s}(\mathbf{x})

其中\mathbf{c}是標量,\mathbf{a}\in \mathbb{R}^{2\times 1}因為例子中維度為2, \mathbf{w}\in \mathbb{R}^{N\times1},函數向量

\mathbf{s}(\mathbf{x})=(\sigma(||\mathbf{x}-\mathbf{x}_1||),\sigma(||\mathbf{x}-\mathbf{x}_2||),\dots,\sigma(||\mathbf{x}-\mathbf{x}_N||))^T

在文獻[3]已經給出證明,這種形式的插值函數是使彎曲能量最小的

J(\Phi)=\sum^2_{j=1} \iint_{\mathbb{R}^2} \left((\frac{\partial^2 \Phi}{\partial x^2}) ^{2} + (\frac{\partial^2 \Phi}{\partial x \partial y}) ^{2} + (\frac{\partial^2 \Phi}{\partial y^2}) ^{2} \right) dxdy

在TPS插值函數\Phi中有1+D+N個參數,其中D在這里為2,所以再加上維度的約束:

\sum^N_{k=1} w_k = 0

\sum^N_{k=1} x^x_k w_k = 0

\sum^N_{k=1} x^y_k w_k = 0

\mathbf{x}^x_k\mathbf{x}^y_k 分別表示點 \mathbf{x}x 坐標值和 y 坐標值,可以將內容簡寫成:

\begin{bmatrix}1_N & X & S \\ 0 & 0 & 1^T_N \\ 0 & 0 & X^T\end{bmatrix} \begin{bmatrix} c \\ \mathbf{a} \\ \mathbf{w}\end{bmatrix} = \begin{bmatrix}Y^x \\ 0 \\ 0\end{bmatrix}

其中 (S)_{ij}=\sigma({\bf x}_i-\bf x_j)1_N 表示值全為1的 N 維列向量

X = \left[\begin{matrix} {\bf x}_1^x & {\bf x}_1^y\\ {\bf x}_2^x & {\bf x}_2^y \\ \cdots & \cdots \\ {\bf x}_N^x & {\bf x}_N^y \end{matrix}\right]

Y^x = \left[\begin{matrix} {\bf y}_1^x \\ {\bf y}_2^x \\ \cdots \\ {\bf y}_N^x \end{matrix}\right]

把矩陣簡化表示,令

\Gamma =\left[\begin{matrix}1_N & X & S \\ 0 & 0 & 1^T_N \\ 0 & 0 & X^T\end{matrix}\right]

如果 S 是非奇異矩陣,則 \Gamma 也是非奇異矩陣,可以解得參數為:

\begin{bmatrix} c \\ \mathbf{a} \\ \mathbf{w}\end{bmatrix}= \Gamma^{-1}\left[\begin{matrix} Y^x \\ 0 \\ 0 \end{matrix}\right]

然后把各個維度的 \Phi 函數的參數都計算出來則有

\left[\begin{matrix} {\bf w}^x & {\bf w}^y\\ c^x &c^y\\ {\bf a}^x & {\bf a}^y \end{matrix}\right]= \Gamma^{-1}\left[\begin{matrix} Y^x & Y^y\\ 0 & 0\\ 0 &0 \end{matrix}\right]

\Gamma^{-1} 寫成下面的形式有

\Gamma^{-1}=\left[ \begin{matrix} \Gamma^{11} & \Gamma^{12}\\ \Gamma^{21} & \Gamma^{22} \end{matrix}\right]

矩陣 \Gamma^{12} 為彎曲能量矩陣,秩為 N-3\Gamma^{11} 作為仿射矩陣,實現平移和旋轉。

將這些形變應用到所有其他點 \{\mathbf{x}_i, i=1,\dots, M\} 上需要和用于計算形變參數的控制點 \{\mathbf{x}^c_i, i=1, \dots, N\} 一起構造對應的計算矩陣:

\left[ \begin{matrix} 1_N & X & S \end{matrix}\right]

其中 S_{ij}=\sigma(\mathbf{x}_i - \mathbf{x}_j) 維度為 M\times N 得到結果為:

Y =\left[ \begin{matrix} 1_N & X & S \end{matrix}\right] \begin{bmatrix} c \\ \mathbf{a} \\ \mathbf{w}\end{bmatrix}

Numpy 實現

首先構造一個上文中的 \Gamma 矩陣:

def makeT(cp):
    # cp: [K x 2] control points
    # T: [(K+3) x (K+3)]
    K = cp.shape[0]
    T = np.zeros((K+3, K+3))
    T[:K, 0] = 1
    T[:K, 1:3] = cp
    T[K, 3:] = 1
    T[K+1:, 3:] = cp.T
    # compute every point pair of points
    R = squareform(pdist(cp, metric='euclidean'))
    R = R * R
    R[R == 0] = 1 # a trick to make R ln(R) 0
    R = R * np.log(R)
    np.fill_diagonal(R, 0)
    T[:K, 3:] = R
    return T

然后構造一個和 \Gamma 進行計算的矩陣,將待轉換的點對構造成矩陣形式

def liftPts(p, cp):
    # p: [N x 2], input points
    # cp: [K x 2], control points
    # pLift: [N x (3+K)], lifted input points
    N, K = p.shape[0], cp.shape[0]
    pLift = np.zeros((N, K+3))
    pLift[:,0] = 1
    pLift[:,1:3] = p
    R = cdist(p, cp, 'euclidean')
    R = R * R
    R[R == 0] = 1
    R = R * np.log(R)
    pLift[:, 3:] = R
    return pLift

對TPS矩陣進行求解,分別得到x和y維度的形變矩陣

def tps_transform(gallery, probe):
    """
    Compute the new points coordination with Thin-Plate-Spline algorithm
    """
    src_pt_xs = probe[:, 0]
    src_pt_ys = probe[:, 1]
    cps = np.vstack([src_pt_xs, src_pt_ys]).T
    # construct T
    T = makeT(cps)
    # solve cx, cy (coefficients for x and y)
    tar_pt_xt = gallery[:, 0]
    tar_pt_yt = gallery[:, 1]
    xtAug = np.concatenate([tar_pt_xt, np.zeros(3)])
    ytAug = np.concatenate([tar_pt_yt, np.zeros(3)])
    cx = np.linalg.solve(T, xtAug)  # [K+3]
    cy = np.linalg.solve(T, ytAug)
    return cx, cy

  1. https://blog.csdn.net/VictoriaW/article/details/70161180 ?

  2. https://en.wikipedia.org/wiki/Radial_basis_function ?

  3. Kent, J. T. and Mardia, K. V. (1994a). The link between kriging and thin-plate splines. In: Probability, Statistics and Optimization: a Tribute to Peter Whittle (ed. F. P. Kelly), pp 325–339. John Wiley & Sons, Ltd, Chichester. page 282, 287, 311 ?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容