如何使用线性插值来插值一个numpy数组

5
我有一个形状如下的 numpy 数组: (1, 128, 160, 1),与此同时,我还有一张形状为 (200, 200) 的图片。现在,我需要做以下操作:
orig = np.random.rand(1, 128, 160, 1)
orig = np.squeeze(orig)

现在,我的目标是将原始数组插值为与输入图像相同的大小,即(200, 200),使用线性插值。我认为我必须指定应该评估numpy数组的网格,但我无法弄清楚如何做到这一点。

2
如果你的意思是双线性插值 - scipy.interpolate.interp2d - meowgoesthedog
@meowgoesthedog 啊,好的,那么采样网格可以简单地定义为一个网格或其他东西吗? - Luca
1个回答

6
你可以使用 scipy.interpolate.interp2d 来完成这个操作:
from scipy import interpolate

# Make a fake image - you can use yours.
image = np.ones((200,200))

# Make your orig array (skipping the extra dimensions).
orig = np.random.rand(128, 160)

# Make its coordinates; x is horizontal.
x = np.linspace(0, image.shape[1], orig.shape[1])
y = np.linspace(0, image.shape[0], orig.shape[0])

# Make the interpolator function.
f = interpolate.interp2d(x, y, orig, kind='linear')

# Construct the new coordinate arrays.
x_new = np.arange(0, image.shape[1])
y_new = np.arange(0, image.shape[0])

# Do the interpolation.
new_orig = f(x_new, y_new)

注意在形成 xy 时对坐标范围进行了 -1 的调整。这确保图像坐标从0到199(含)的范围内。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接