Python 两个矩阵之间的插值

Python 两个矩阵之间的插值,python,matrix,interpolation,Python,Matrix,Interpolation,我有两个矩阵,它们代表网格上不同时间的量值。 我想在中间时间创建第三个矩阵,其像素在两个矩阵之间插值 我尝试对每个像素进行简单的线性插值,但如果我用imshow可视化最终产品,帧之间就不会有平滑的过渡 我不能提供一个直接的例子,因为我正在处理一个巨大的数据集,但我想知道是否有人遇到过类似的问题 我知道scipy.interpolate函数,但它们似乎只在插值一组离散点时才派上用场。假设矩阵是numpy数组,并且由于只有两个点,您可以自己实现(线性)插值: def interp(m1, t1, m

我有两个矩阵,它们代表网格上不同时间的量值。 我想在中间时间创建第三个矩阵,其像素在两个矩阵之间插值

我尝试对每个像素进行简单的线性插值,但如果我用
imshow
可视化最终产品,帧之间就不会有平滑的过渡

我不能提供一个直接的例子,因为我正在处理一个巨大的数据集,但我想知道是否有人遇到过类似的问题


我知道scipy.interpolate函数,但它们似乎只在插值一组离散点时才派上用场。

假设矩阵是numpy数组,并且由于只有两个点,您可以自己实现(线性)插值:

def interp(m1, t1, m2, t2, t_interp):
    return m1 + (m2-m1) / (t2-t1) * (t_interp-t1)
其中,
m1
/
m2
可以是任意形状的numpy数组和
t1
/
t2
它们对应的时间值。t_interp是要插值的时间值。
因此,对于
t\u interp=t1
t\u interp=t2
m1
m2
由于原始数据已经网格化,您可以使用
ndimage.map\u坐标来插值:

import numpy as np
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt

# given 2 arrays arr1, arr2
arr1 = np.linspace(0, 1, 100).reshape(10,10)
arr2 = np.linspace(1, 0, 100).reshape(10,10)

# rejoin arr1, arr2 into a single array of shape (2, 10, 10)
arr = np.r_['0,3', arr1, arr2]

# define the grid coordinates where you want to interpolate
X, Y = np.meshgrid(np.arange(10), np.arange(10))
# 0.5 corresponds to half way between arr1 and arr2
coordinates = np.ones((10,10))*0.5, X, Y

# given arr interpolate at coordinates
newarr = ndimage.map_coordinates(arr, coordinates, order=2).T
fig, ax = plt.subplots(ncols=3)
cmap = plt.get_cmap('Greys')

vmin = np.min([arr1.min(), newarr.min(), arr2.min()])
vmax = np.max([arr1.max(), newarr.max(), arr2.max()])
ax[0].imshow(arr1, interpolation='nearest', cmap=cmap, vmin=vmin, vmax=vmax)
ax[1].imshow(newarr, interpolation='nearest', cmap=cmap, vmin=vmin, vmax=vmax)
ax[2].imshow(arr2, interpolation='nearest', cmap=cmap, vmin=vmin, vmax=vmax)
ax[0].set_xlabel('arr1')
ax[1].set_xlabel('interpolated')
ax[2].set_xlabel('arr2')
plt.show()

那怎么办?它需要“一个大小的数组(N,ndim),或者一个ndim数组的元组”。谢谢你的回答,但正如我所说的,这个问题是我以前一直在尝试的。有没有更复杂的方法?