我有一个具有以下形状的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
和时,请注意对坐标范围的-1调整y
。这样可以确保图像坐标从0到199(含)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句