2

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

我尝试对每个像素进行简单的线性插值,但如果我将最终产品可视化,imshow我并没有真正在帧之间进行平滑过渡。

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

我知道这些scipy.interpolate功能,但它们似乎只是为了插入一组离散点而派上用场。

4

2 回答 2

4

由于原始数据已经网格化,您可以使用ndimage.map_coordinates插值:

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()

在此处输入图像描述

于 2013-09-06T13:04:18.113 回答
2

假设矩阵是 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 将是插值的时间值。所以这给出了m1fort_interp = t1m2for t_interp = t2

于 2013-09-06T09:51:32.523 回答