现在的位置: 首页 > 综合 > 正文

三维透视变换

2018年04月10日 ⁄ 综合 ⁄ 共 652字 ⁄ 字号 评论关闭

From:维基百科。

透视投影的定义可以理解为透过摄像机取景器对于被投影物体进行观察。摄像机的位置、朝向和视野都将影响投影变换的结果。我们定义以下变量来对这一变换进行描述:

  • \mathbf{a}_{x,y,z}:将被投影的三维空间中的点。
  • \mathbf{c}_{x,y,z}:摄像机的位置。
  • \mathbf{\theta}_{x,y,z}:摄像机的旋转角度。(也可以理解为物体的旋转角度,但是方向相反,注意角度是绕x、y、z的旋转度数而不是与x、y、z的夹角!
  •  当 \mathbf{c}_{x,y,z}=<0,0,0>且 \mathbf{\theta}_{x,y,z}=<0,0,0>,
    三维向量<1,2,0>将被投影到二维向量<1,2>。
  • \mathbf{e}_{x,y,z}:观测者相对显示平面的位置。

最终结果为:

  • \mathbf{b}_{x,y}\mathbf{a}所产生的二维投影。

首先我们定义点\mathbf{d}_{x,y,z}作为\mathbf{a}向摄像机坐标系所作的变换,其中摄像机坐标系由摄像机的位置\mathbf{c}和旋转\mathbf{\theta}_{x,y,z}所决定。该过程为:先用\mathbf{a}减去\mathbf{c}(平移),然后使用由-\mathbf{\theta}旋转方向为逆时针,所以成像方向要加负号)产生的旋转矩阵乘上该结果。

该变换通常称为摄像机变换(注意该计算过程假设使用左手法则):

\begin{bmatrix}   \mathbf{d}_x \\   \mathbf{d}_y \\   \mathbf{d}_z \\\end{bmatrix}=\begin{bmatrix}   1 & 0 & 0  \\   0 & {\cos -\mathbf{\theta}_x } & {\sin -\mathbf{\theta}_x }  \\   0 & { - \sin -\mathbf{\theta}_x } & {\cos -\mathbf{\theta}_x }  \\\end{bmatrix}\begin{bmatrix}   {\cos -\mathbf{\theta}_y } & 0 & { - \sin -\mathbf{\theta}_y }  \\   0 & 1 & 0  \\   {\sin -\mathbf{\theta}_y } & 0 & {\cos -\mathbf{\theta}_y }  \\\end{bmatrix}\begin{bmatrix}   {\cos -\mathbf{\theta}_z } & {\sin -\mathbf{\theta}_z } & 0  \\   { - \sin -\mathbf{\theta}_z } & {\cos -\mathbf{\theta}_z } & 0  \\   0 & 0 & 1  \\\end{bmatrix}\left( {\begin{bmatrix}   \mathbf{a}_x  \\   \mathbf{a}_y  \\   \mathbf{a}_z  \\\end{bmatrix} - \begin{bmatrix}   \mathbf{c}_x  \\   \mathbf{c}_y  \\   \mathbf{c}_z  \\\end{bmatrix}} \right)

或者使用以下这种非矩阵表示的形式,其中角度的正负号与矩阵表示形式不同:

\begin{array}{lcl}d_x &= &\cos \theta_y\cdot(\sin \theta_z\cdot(a_y-c_y)+\cos \theta_z\cdot(a_x-c_x))-\sin \theta_y\cdot(a_z-c_z) \\d_y &= &\sin \theta_x\cdot(\cos \theta_y\cdot(a_z-c_z)+\sin \theta_y\cdot(\sin \theta_z\cdot(a_y-c_y)+\cos \theta_z\cdot(a_x-c_x)))+\cos \theta_x\cdot(\cos \theta_z\cdot(a_y-c_y)-\sin \theta_z\cdot(a_x-c_x)) \\d_z &= &\cos \theta_x\cdot(\cos \theta_y\cdot(a_z-c_z)+\sin \theta_y\cdot(\sin \theta_z\cdot(a_y-c_y)+\cos \theta_z\cdot(a_x-c_x)))-\sin \theta_x\cdot(\cos \theta_z\cdot(a_y-c_y)-\sin \theta_z\cdot(a_x-c_x)) \\\end{array}

然后将变换后的该点通过以下方程投影到二维平面(此处投影平面为x/y平面,有时也使用x/z):

假设摄像机和人眼同处一个位置,Ez/Dz = 1

\begin{array}{lcl} \mathbf{b}_x &= &(\mathbf{d}_x - \mathbf{e}_x) (\mathbf{e}_z / \mathbf{d}_z) \\ \mathbf{b}_y &= &(\mathbf{d}_y - \mathbf{e}_y) (\mathbf{e}_z / \mathbf{d}_z) \\\end{array}

或在齐次坐标系下可以表示为:

\begin{bmatrix}   \mathbf{f}_x \\   \mathbf{f}_y \\   \mathbf{f}_z \\   \mathbf{f}_w \\\end{bmatrix}=\begin{bmatrix}   1 & 0 & 0 & -\mathbf{e}_x \\   0 & 1 & 0 & -\mathbf{e}_y \\   0 & 0 & 1 & 0 \\   0 & 0 & 1/\mathbf{e}_z & 0 \\\end{bmatrix}\begin{bmatrix}   \mathbf{d}_x  \\   \mathbf{d}_y  \\   \mathbf{d}_z  \\   1 \\\end{bmatrix}

\begin{array}{lcl} \mathbf{b}_x &= &\mathbf{f}_x / \mathbf{f}_w \\ \mathbf{b}_y &= &\mathbf{f}_y / \mathbf{f}_w \\\end{array}

观测者到显示平面的距离,\mathbf{e}_z,直接关系到视野的大小。\alpha=2 \cdot \tan^{-1}(1/\mathbf{e}_z)为可视角度。(这里假设屏幕的两角为(-1,-1)和(1,1))

如果要在一些特定的显示设备上显示该二维平面,之后还要进行一些必要的剪裁和缩放操作。

图示

Projective Transform.svg

计算三维空间中位于Ax,Az的点在屏幕坐标x轴的位置:

screen\ x\ coordinate\ (Bx)\ =\ model\ x\ coordinate\ (Ax) \times \frac{distance\ from\ eye\ to\ screen\ (Bz)}{distance\ from\ eye\ to\ point\ (Az)}

对于y轴同样有:

screen\ y\ coordinate\ (By)\ =\ model\ y\ coordinate\ (Ay) \times \frac{distance\ from\ eye\ to\ screen\ (Bz)}{distance\ from\ eye\ to\ point\ (Az)}

(其中Ax和Ay是透视转换前物体在空间中的坐标)

抱歉!评论已关闭.