我有一个具有以下形状的 numpy 数组:(1, 128, 160, 1)。
现在,我有一个形状为 (200, 200) 的图像。
因此,我执行以下操作:
orig = np.random.rand(1, 128, 160, 1)
orig = np.squeeze(orig)
现在,我想要做的是采用我的原始数组并将其插值到与输入图像相同的大小,即 (200, 200)
使用线性插值。我想我必须指定应该在其上评估 numpy 数组的网格,但我无法弄清楚如何去做。
最佳答案
你可以像这样用 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)
注意形成x
和y
时对坐标范围的-1调整。这确保图像坐标从 0 到 199(含)。
关于python - 如何使用线性插值法对 numpy 数组进行插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55373964/