矩阵到四元数和后退不会给出相同的结果

时间:2017-10-16 04:21:44

标签: python quaternions

我正在将矩阵(M)转换为四元数,以便我可以例如两个不同的变换矩阵之间的lerp,使我需要自己制作视频帧的图像的平滑动画。

当我从四元数转换回矩阵作为测试时,这个新矩阵与成为quat的矩阵相差甚远。

import numpy as np
from transforms3d import quaternions

M = np.array([[  0.757403109,  -0.186744161,   145.541734],
 [ -0.154492906,   0.626185286,   100.878814],
 [ -0.000294826495,  -0.000344726091,   1.00000000]])


quat = quaternions.mat2quat(M)


testM = quaternions.quat2mat(quat)
print("TEST: M original")
print(M)
print("TEST: quat back to mat (testM)")
print(testM)
print("Why not the same")
print ("quat")
print(quat)
print("quat of testM")
print(quaternions.mat2quat(testM))

#Scaling gives same result, scale M to be -1. to 1
mmax = np.amax(M)
scaleTestM = M / mmax
print("M Scaled")
print(scaleTestM)
quatOfScaled = quaternions.mat2quat(scaleTestM)
print("Quat of scaled")
print(quaternions.quat2mat(quatOfScaled))

我是否会遗漏四元数实际可以表示的内容或代码是错误的? 如果这不起作用,那么有关如何在两个转换矩阵之间平滑移动的其他建议表示赞赏。

Python 3.6

控制台输出是:

    TEST: M original
   [[  7.57403109e-01  -1.86744161e-01   1.45541734e+02]
    [ -1.54492906e-01   6.26185286e-01   1.00878814e+02]
     [ -2.94826495e-04  -3.44726091e-04   1.00000000e+00]]
    TEST: quat back to mat (testM)
    [[ 0.38627453 -0.42005089  0.8211877 ]
     [-0.54462197  0.61466344  0.57059247]
     [-0.74443193 -0.6676422   0.00865989]]
    Why not the same
    quat
    [ 0.70880143 -0.43673539  0.55220671 -0.04393723]
    quat of testM
    [ 0.70880143 -0.43673539  0.55220671 -0.04393723]
    M Scaled
    [[  5.20402697e-03  -1.28309699e-03   1.00000000e+00]
     [ -1.06150244e-03   4.30244486e-03   6.93126372e-01]
     [ -2.02571789e-06  -2.36857210e-06   6.87088145e-03]]
    Quat of scaled
    [[ 0.38627453 -0.42005089  0.8211877 ]
     [-0.54462197  0.61466344  0.57059247]
     [-0.74443193 -0.6676422   0.00865989]]

1 个答案:

答案 0 :(得分:2)

对于给定的四元数,有多个矩阵表示。将矩阵转换为四元数时,有关您最初使用这些表示的信息将丢失。

参见例如https://en.wikipedia.org/wiki/Quaternion

中的矩阵表示