1、对于旋转插值,使用scipy.spatial.transform.Slerp
from scipy.spatial.transform import Rotation as R
from scipy.spatial.transform import Slerp
key_rots = R.random(5, random_state=2342345)
key_times = [0, 1, 2, 3, 4]
构建对象,确定对应的时间和旋转量,Create the interpolator object:
slerp = Slerp(key_times, key_rots)
对于新的时间进行插值,Interpolate the rotations at the given times:
times = [0, 0.5, 0.25, 1, 1.5, 2, 2.75, 3, 3.25, 3.60, 4]
interp_rots = slerp(times)
需要给出插值前的旋转:The keyframe rotations expressed as Euler angles:
key_rots.as_euler('xyz', degrees=True)
array([[ 14.31443779, -27.50095894, -3.7275787 ],
[ -1.79924227, -24.69421529, 164.57701743],
[146.15020772, 43.22849451, -31.34891088],
[ 46.39959442, 11.62126073, -45.99719267],
[-88.94647804, -49.64400082, -65.80546984]])
得到最后的结果:The interpolated rotations expressed as Euler angles. These agree with the keyframe rotations at both endpoints of the range of keyframe times.
interp_rots.as_euler('xyz', degrees=True)
array([[ 14.31443779, -27.50095894, -3.7275787 ],
[ 4.74588574, -32.44683966, 81.25139984],
[ 10.71094749, -31.56690154, 38.06896408],
[ -1.79924227, -24.69421529, 164.57701743],
[ 11.72796022, 51.64207311, -171.7374683 ],
[ 146.15020772, 43.22849451, -31.34891088],
[ 68.10921869, 20.67625074, -48.74886034],
[ 46.39959442, 11.62126073, -45.99719267],
[ 12.35552615, 4.21525086, -64.89288124],
[ -30.08117143, -19.90769513, -78.98121326],
[ -88.94647804, -49.64400082, -65.80546984]])
2、对于其他数值,scipy.interpolate.interp1d
class scipy.interpolate.interp1d(x, y, kind=‘linear’, axis=- 1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)
axis(int, optional)指的是对哪一维进行插值
Specifies the axis of y along which to interpolate. Interpolation defaults to the last axis of y.
import matplotlib.pyplot as plt
from scipy import interpolate
x = np.arange(0, 10)
y = np.exp(-x/3.0)
f = interpolate.interp1d(x, y)
xnew = np.arange(0, 9, 0.1)
ynew = f(xnew) # use interpolation function returned by `interp1d`
plt.plot(x, y, 'o', xnew, ynew, '-')
plt.show()
或者可以直接:
import matplotlib.pyplot as plt
from scipy import interpolate
x = np.arange(0, 10)
y = np.exp(-x/3.0)
xnew = np.arange(0, 9, 0.1)
ynew = interpolate.interp1d(x, y, axis=0)(xnew)
plt.plot(x, y, 'o', xnew, ynew, '-')
plt.show()