释义:相似变换是旋转、平移、缩放变换的组合,包含四个参数:缩放因子s,旋转角度
θ
\theta
θ,x方向平移
t
x
\ t_x
tx, y方向平移
t
y
\ t_y
ty
变换矩阵为:
(
x
′
y
′
)
\begin{pmatrix} x' \\ y' \\ \end{pmatrix}
(x′y′) =
(
s
c
o
s
(
θ
)
s
s
i
n
(
θ
)
t
x
−
s
s
i
n
(
θ
)
s
c
o
s
(
θ
)
t
y
)
\begin{pmatrix} scos(\theta) &s sin(\theta) & t_x \\ -s sin(\theta) & scos(\theta) & t_y \\ \end{pmatrix}
(scos(θ)−ssin(θ)ssin(θ)scos(θ)txty)
(
x
y
1
)
\begin{pmatrix} x \\ y \\ 1 \\ \end{pmatrix}
⎝⎛xy1⎠⎞ (1)
令
m
1
=
s
c
o
s
(
θ
)
m_1=scos(\theta)
m1=scos(θ),
m
2
=
−
s
s
i
n
(
θ
)
m_2=-ssin(\theta)
m2=−ssin(θ),
m
3
=
t
x
m_3=t_x
m3=tx,
m
4
=
t
y
m_4=t_y
m4=ty
则
x
′
=
m
1
x
−
m
2
y
+
t
x
x'=m_1x-m_2y+t_x
x′=m1x−m2y+tx,
y
′
=
m
1
y
+
m
2
x
+
t
y
y'=m_1y+m_2x+t_y
y′=m1y+m2x+ty
相似变换矩阵有四个参数,实际上有两个点即可得出解,实际上可能由多个点,比如人脸关键点和标准关键点之间的相似变换矩阵求解,事实上,从原始点到目标点的变换矩阵为:
(
x
1
−
y
1
1
0
y
1
x
1
0
1
x
2
−
y
2
1
0
y
2
x
2
0
1
.
.
.
.
.
.
.
.
.
.
.
.
)
\begin{pmatrix} x_1 & -y_1 & 1 & 0 \\ y_1 & x_1 & 0 & 1 \\ x_2 & -y_2 & 1 & 0 \\ y_2 & x_2 & 0 & 1 \\ ... & ... & ... & ... \\\end{pmatrix}
⎝⎜⎜⎜⎜⎛x1y1x2y2...−y1x1−y2x2...1010...0101...⎠⎟⎟⎟⎟⎞
(
m
1
m
2
m
3
m
4
)
\begin{pmatrix} m_1 \\ m_2 \\ m_3 \\ m_4 \\ \end{pmatrix}
⎝⎜⎜⎛m1m2m3m4⎠⎟⎟⎞ =
(
x
1
′
y
1
′
x
2
′
y
2
′
.
.
.
)
\begin{pmatrix} x_1' \\ y_1' \\ x_2' \\ y_2' \\ ... \\ \end{pmatrix}
⎝⎜⎜⎜⎜⎛x1′y1′x2′y2′...⎠⎟⎟⎟⎟⎞ (2)
(2)式写成
A
M
=
B
AM=B
AM=B,可得
A
T
A
M
=
A
T
B
A^TAM=A^TB
ATAM=ATB,即可照函数icvGetRTMatrix的计算方法,转换为如下的形式:
(
Σ
x
i
2
+
y
i
2
0
Σ
x
i
Σ
y
i
0
Σ
x
i
2
+
y
i
2
−
Σ
y
i
Σ
x
i
Σ
x
i
−
Σ
y
i
1
0
Σ
y
i
Σ
x
i
0
1
)
\begin{pmatrix} \Sigma x_i^2+y_i^2 & 0 & \Sigma x_i & \Sigma y_i \\ 0 & \Sigma x_i^2+y_i^2 & -\Sigma y_i & \Sigma x_i \\ \Sigma x_i & -\Sigma y_i & 1 & 0 \\ \Sigma y_i & \Sigma x_i & 0 & 1 \\ \end{pmatrix}
⎝⎜⎜⎛Σxi2+yi20ΣxiΣyi0Σxi2+yi2−ΣyiΣxiΣxi−Σyi10ΣyiΣxi01⎠⎟⎟⎞
(
m
1
m
2
m
3
m
4
)
\begin{pmatrix} m_1 \\ m_2 \\ m_3 \\ m_4 \\ \end{pmatrix}
⎝⎜⎜⎛m1m2m3m4⎠⎟⎟⎞ =
(
Σ
x
i
x
i
′
+
y
i
y
i
′
Σ
x
i
y
i
′
−
x
i
′
y
i
Σ
x
i
′
Σ
y
i
′
)
\begin{pmatrix} \Sigma x_ix_i' + y_iy_i' \\ \Sigma x_iy_i'-x_i'y_i \\ \Sigma x_i' \\ \Sigma y_i' \\ \end{pmatrix}
⎝⎜⎜⎛Σxixi′+yiyi′Σxiyi′−xi′yiΣxi′Σyi′⎠⎟⎟⎞ (3)
其中第一行和第二行,分别是第三行和第四行乘以x或y,再求和所得
则相似变换矩阵的四个参数可如下计算,即
M
=
(
A
T
A
)
−
1
A
T
B
M=(A^TA)^{-1}A^TB
M=(ATA)−1ATB:
(
m
1
m
2
m
3
m
4
)
\begin{pmatrix} m_1 \\ m_2 \\ m_3 \\ m_4 \\ \end{pmatrix}
⎝⎜⎜⎛m1m2m3m4⎠⎟⎟⎞ =
(
Σ
x
i
2
+
y
i
2
0
Σ
x
i
Σ
y
i
0
Σ
x
i
2
+
y
i
2
−
Σ
y
i
Σ
x
i
Σ
x
i
−
Σ
y
i
1
0
Σ
y
i
Σ
x
i
0
1
)
−
1
\begin{pmatrix} \Sigma x_i^2+y_i^2 & 0 & \Sigma x_i & \Sigma y_i \\ 0 & \Sigma x_i^2+y_i^2 & -\Sigma y_i & \Sigma x_i \\ \Sigma x_i & -\Sigma y_i & 1 & 0 \\ \Sigma y_i & \Sigma x_i & 0 & 1 \\ \end{pmatrix}^{-1}
⎝⎜⎜⎛Σxi2+yi20ΣxiΣyi0Σxi2+yi2−ΣyiΣxiΣxi−Σyi10ΣyiΣxi01⎠⎟⎟⎞−1
(
Σ
x
i
x
i
′
+
y
i
y
i
′
Σ
x
i
y
i
′
−
x
i
′
y
i
Σ
x
i
′
Σ
y
i
′
)
\begin{pmatrix} \Sigma x_ix_i' + y_iy_i' \\ \Sigma x_iy_i'-x_i'y_i \\ \Sigma x_i' \\ \Sigma y_i' \\ \end{pmatrix}
⎝⎜⎜⎛Σxixi′+yiyi′Σxiyi′−xi′yiΣxi′Σyi′⎠⎟⎟⎞ (4)
公式(4)的中的逆矩阵可通过伴随矩阵方式计算,即
(
A
T
A
)
−
1
=
(
A
T
A
)
∗
∣
A
T
A
∣
(A^TA)^{-1}=\frac{(A^TA)^{*}}{|A^TA|}
(ATA)−1=∣ATA∣(ATA)∗
对相似变换后的像素坐标取整:
u
0
=
⌊
u
t
r
a
n
s
⌋
u_0=⌊u_{trans} ⌋
u0=⌊utrans⌋
v
0
=
⌊
v
t
r
a
n
s
⌋
v_0=⌊v_{trans} ⌋
v0=⌊vtrans⌋
u
1
=
u
0
+
1
u_1=u_0+1
u1=u0+1
v
1
=
v
0
+
1
v_1=v_0+1
v1=v0+1
d
x
=
u
t
r
a
n
s
−
u
0
,
d
y
=
v
t
r
a
n
s
−
v
0
d_x=u_{trans}-u_0,d_y=v_{trans}-v_0
dx=utrans−u0,dy=vtrans−v0
计算双线性插值的权值:
(
w
1
=
(
1
−
d
x
)
×
(
1
−
d
y
)
,
w
2
=
d
x
×
(
1
−
d
y
)
,
w
3
=
(
1
−
d
x
)
×
d
y
,
w
4
=
d
x
×
d
y
(w_1=(1-d_x )×(1-d_y ),w_2=d_x×(1-d_y ),w_3=(1-d_x )×d_y,w_4=d_x×d_y
(w1=(1−dx)×(1−dy),w2=dx×(1−dy),w3=(1−dx)×dy,w4=dx×dy
双线性插值计算pDst(u,v):
p
D
s
t
(
u
,
v
)
=
w
1
×
p
S
r
c
(
u
0
,
v
0
)
+
w
2
×
p
S
r
c
(
u
1
,
v
0
)
+
w
3
×
p
S
r
c
(
u
0
,
v
1
)
+
w
4
×
p
S
r
c
(
u
1
,
v
1
)
pDst(u,v)=w_1×pSrc(u_0,v_0 )+w_2×pSrc(u_1,v_0 )+w_3×pSrc(u_0,v_1 )+w_4×pSrc(u_1,v_1 )
pDst(u,v)=w1×pSrc(u0,v0)+w2×pSrc(u1,v0)+w3×pSrc(u0,v1)+w4×pSrc(u1,v1)
双线性插值的另外一种形式:
p
D
s
t
(
u
,
v
)
=
w
1
×
p
S
r
c
(
u
0
,
v
0
)
+
w
2
×
p
S
r
c
(
u
0
,
v
1
)
+
w
3
×
p
S
r
c
(
u
1
,
v
0
)
+
w
4
×
p
S
r
c
(
u
1
,
v
1
)
pDst(u,v)=w_1×pSrc(u_0,v_0 )+w_2×pSrc(u_0,v_1 )+w_3×pSrc(u_1,v_0 )+w_4×pSrc(u_1,v_1 )
pDst(u,v)=w1×pSrc(u0,v0)+w2×pSrc(u0,v1)+w3×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