相机旋转矩阵

时间:2017-05-15 16:50:09

标签: python graphics camera linear-algebra raytracing

我正在尝试光线跟踪视频。就此而言,我需要每个帧的相机旋转矩阵,在世界坐标中。相机位于原点。没有翻译。

我将相机的轨迹视为每帧的旋转变化。 因此,对于每一帧,我都有三个值(滚动,偏航,俯仰),用于描述相机应该从该帧旋转到下一帧的程度。这些旋转应在摄像机坐标系中理解。

如何计算帧的世界坐标旋转矩阵?

我尝试了什么:

def rot_x(angle):
    cosa = np.cos(angle)
    sina = np.sin(angle)
    return np.array([[1,0,0], [0, cosa, -sina], [0, sina, cosa]])

def rot_y(angle):
    cosa = np.cos(angle)
    sina = np.sin(angle)
    return np.array([[cosa, 0, sina], [0,1,0], [-sina, 0, cosa]])

def rot_z(angle):
    cosa = np.cos(angle)
    sina = np.sin(angle)
    return np.array([[cosa, -sina, 0], [sina, cosa, 0], [0,0,1]])

matrices = [initial_rot]
for pitch, yaw, roll in frames_data:
    rx = rot_x(pitch)
    ry = rot_y(yaw)
    rz = rot_z(roll)
    last_matrix = matrices[-1]
    matrices.append(last_matrix.T.dot(rx).dot(ry).dot(rz))

(因为last_matrix应该是正交的,所以它的倒数应该是转置)。

但是,有些东西是非常错误的,渲染的视频只是在y维度上闪烁。我确信这里的数学有问题..

1 个答案:

答案 0 :(得分:2)

  1. 矩阵乘法的顺序很重要。应用另一个旋转应该通过左乘(假设标准惯例)来完成。

  2. 由于这只是合成多次旋转,因此不需要反转最后一次旋转。

  3. 应为帧N计算的完整旋转是:

    R_n = R(yaw_n, pitch_n, roll_n) R_{n - 1} R_{n - 2} ... R_1 R_0
    

    使用:

    R_0: the initial rotation (i.e. initial_rot)
    R_n: the complete rotation for the frame N
    R(yaw_n, pitch_n, roll_n): the rotation derived from the yaw / pitch / roll values applied between frame N - 1 and N (i.e. rx.dot(ry).dot(rz))
    

    因此,代码摘录中的最后一行应改为:

    rotation = rx.dot(ry).dot(rz)
    matrices.append(rotation.dot(last_matrix))