我使用以下 Python 函数将四元数转换为欧拉角:
import math
def quaternion_to_euler_angle(w, x, y, z):
ysqr = y * y
t0 = +2.0 * (w * x + y * z)
t1 = +1.0 - 2.0 * (x * x + ysqr)
X = math.degrees(math.atan2(t0, t1))
t2 = +2.0 * (w * y - z * x)
t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
Y = math.degrees(math.asin(t2))
t3 = +2.0 * (w * z + x * y)
t4 = +1.0 - 2.0 * (ysqr + z * z)
Z = math.degrees(math.atan2(t3, t4))
return X, Y, Z
我想将具有“w”、“quat_x”、“quat_y”和“quat_z”列的 Pandas DataFrame 转换为欧拉角。目前,我正在使用 for 循环迭代 DataFrame 的每一行并调用quaternion_to_euler_angle()
每行上的函数。这非常慢,因为我有超过 400'000 行。
有更有效的方法吗?例如,我可以将 DataFrame (或单个系列)传递给quaternion_to_euler_angle()
但问题是要改变quaternion_to_euler_angle()
这样它就可以处理 DataFrame 而不是整数。
更紧凑的方法是使用Rotation
from scipy.spatial.transform
:
import pandas as pd
from scipy.spatial.transform import Rotation
rot = Rotation.from_quat(quat_df)
rot_euler = rot.as_euler('xyz', degrees=True)
euler_df = pd.DataFrame(data=rot_euler, columns=['x', 'y', 'z'])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)