图像变换

图像变换

  • 包含内容:等距变换、相似变换、仿射变换、投影变换
  • 相关链接:
    • https://blog.csdn.net/raby_gyl/article/details/52780644
    • https://blog.csdn.net/KinboSong/article/details/64923831
    • https://blog.csdn.net/aiwoshan0908/article/details/80047529
    • https://blog.csdn.net/u014096352/article/details/53526747
    • 基于Landmark的人脸对齐以及裁剪方法
      • https://www.cnblogs.com/cv-pr/articles/5438351.html
    • 平均脸
      • https://blog.csdn.net/dzJx2EOtaA24Adr/article/details/78610166?utm_source=blogxgwz4
  • 求解相似变换矩阵、仿射变换矩阵的注释
    • icvGetRTMatrix函数详细注释:https://blog.csdn.net/xdfyoga1/article/details/44263271

相似变换原理及推导

  • 释义:相似变换是旋转、平移、缩放变换的组合,包含四个参数:缩放因子s,旋转角度θ\theta,x方向平移 tx\ t_x, y方向平移 ty\ t_y
    • 变换矩阵为:
      (xy)\begin{pmatrix} x' \\ y' \\ \end{pmatrix} = (scos(θ)ssin(θ)txssin(θ)scos(θ)ty)\begin{pmatrix} scos(\theta) &s sin(\theta) & t_x \\ -s sin(\theta) & scos(\theta) & t_y \\ \end{pmatrix} (xy1)\begin{pmatrix} x \\ y \\ 1 \\ \end{pmatrix} (1)
    • m1=scos(θ)m_1=scos(\theta)m2=ssin(θ)m_2=-ssin(\theta)m3=txm_3=t_xm4=tym_4=t_y
    • x=m1xm2y+txx'=m_1x-m_2y+t_x, y=m1y+m2x+tyy'=m_1y+m_2x+t_y
    • 相似变换矩阵有四个参数,实际上有两个点即可得出解,实际上可能由多个点,比如人脸关键点和标准关键点之间的相似变换矩阵求解,事实上,从原始点到目标点的变换矩阵为:
      (x1y110y1x101x2y210y2x201............)\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} (m1m2m3m4)\begin{pmatrix} m_1 \\ m_2 \\ m_3 \\ m_4 \\ \end{pmatrix} = (x1y1x2y2...)\begin{pmatrix} x_1' \\ y_1' \\ x_2' \\ y_2' \\ ... \\ \end{pmatrix} (2)
    • (2)式写成AM=BAM=B,可得ATAM=ATBA^TAM=A^TB,即可照函数icvGetRTMatrix的计算方法,转换为如下的形式:
      (Σxi2+yi20ΣxiΣyi0Σxi2+yi2ΣyiΣxiΣxiΣyi10ΣyiΣxi01)\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} (m1m2m3m4)\begin{pmatrix} m_1 \\ m_2 \\ m_3 \\ m_4 \\ \end{pmatrix} = (Σxixi+yiyiΣxiyixiyiΣxiΣyi)\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} (3)
    • 其中第一行和第二行,分别是第三行和第四行乘以x或y,再求和所得
    • 则相似变换矩阵的四个参数可如下计算,即M=(ATA)1ATBM=(A^TA)^{-1}A^TB
      (m1m2m3m4)\begin{pmatrix} m_1 \\ m_2 \\ m_3 \\ m_4 \\ \end{pmatrix} = (Σxi2+yi20ΣxiΣyi0Σxi2+yi2ΣyiΣxiΣxiΣyi10ΣyiΣxi01)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} (Σxixi+yiyiΣxiyixiyiΣxiΣyi)\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} (4)
    • 公式(4)的中的逆矩阵可通过伴随矩阵方式计算,即(ATA)1=(ATA)ATA(A^TA)^{-1}=\frac{(A^TA)^{*}}{|A^TA|}

双线性插值推导

  • 假设相似变换前像素坐标(u,v)处的像素值为pSrc(u,v), 相似变换后的像素值为pDst(u,v),双线性插值计算pDst(u,v)的步骤如下:
  • 对相似变换后的像素坐标取整:
    u0=utransu_0=⌊u_{trans} ⌋
    v0=vtransv_0=⌊v_{trans} ⌋
    u1=u0+1u_1=u_0+1
    v1=v0+1v_1=v_0+1
    dx=utransu0,dy=vtransv0d_x=u_{trans}-u_0,d_y=v_{trans}-v_0
  • 计算双线性插值的权值:
    (w1=(1dx)×(1dy),w2=dx×(1dy),w3=(1dx)×dy,w4=dx×dy(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
  • 双线性插值计算pDst(u,v):
    pDst(u,v)=w1×pSrc(u0,v0)+w2×pSrc(u1,v0)+w3×pSrc(u0,v1)+w4×pSrc(u1,v1)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(u0,v1)+w3×pSrc(u1,v0)+w4×pSrc(u1,v1)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 )
    =(1dx)×(1dy)×pSrc(u0,v0)+dx×(1dy)×pSrc(u0,v1)+(1dx)×dy×pSrc(u1,v0)+dx×dy×pSrc(u1,v1)(1-d_x )×(1-d_y )×pSrc(u_0,v_0 )+d_x×(1-d_y )×pSrc(u_0,v_1 )+(1-d_x )×d_y×pSrc(u_1,v_0 )+d_x×d_y×pSrc(u_1,v_1 )
    =(1dy)×[(1dx)×pSrc(u0,v0)+dx×pSrc(u0,v1)]+dy×[(1dx)×pSrc(u1,v0)+dx×pSrc(u1,v1)](1-d_y )×[(1-d_x )×pSrc(u_0,v_0 )+d_x×pSrc(u_0,v_1 )]+d_y×[(1-d_x )×pSrc(u_1,v_0 )+d_x×pSrc(u_1,v_1 )]
    =(1dy)×[pSrc(u0,v0)+dx×(pSrc(u0,v1)pSrc(u0,v1))]+dy×[pSrc(u1,v0)+dx×(pSrc(u1,v1)pSrc(u1,v0))](1-d_y )×[pSrc(u_0,v_0 )+d_x×(pSrc(u_0,v_1 )-pSrc(u_0,v_1 ))]+d_y×[pSrc(u_1,v_0 )+d_x×(pSrc(u_1,v_1 )-pSrc(u_1,v_0 ))]
    • mt=pSrc(u0,v0)+dx×(pSrc(u0,v1)pSrc(u0,v1))m_t=pSrc(u_0,v_0 )+d_x×(pSrc(u_0,v_1 )-pSrc(u_0,v_1 ))mb=pSrc(u1,v0)+dx×(pSrc(u1,v1)pSrc(u1,v0))m_b=pSrc(u_1,v_0 )+d_x×(pSrc(u_1,v_1 )-pSrc(u_1,v_0 ))
    • pDst(u,v)=mt+(mb+mt)dypDst(u,v)=m_t+(m_b+m_t)d_y
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值