问题描述
我有一个numpy数组,其形状如下:(1、128、160、1).
I have a numpy array which has the following shape: (1, 128, 160, 1).
现在,我有一个图像,其形状为(200,200).
Now, I have an image which has the shape: (200, 200).
因此,我执行以下操作:
So, I do the following:
orig = np.random.rand(1, 128, 160, 1)
orig = np.squeeze(orig)
现在,我要做的是获取原始数组,并使用线性插值将其插值为与输入图像相同的大小,即(200, 200)
.我想我必须指定应在其上评估numpy数组的网格,但我无法弄清楚如何做到这一点.
Now, what I want to do is take my original array and interpolate it to be of the same size as the input image i.e. (200, 200)
using linear interpolation. I think I have to specify the grid on which the numpy array should be evaluated but I am unable to figure out how to do it.
推荐答案
您可以使用scipy.interpolate.interp2d
来做到这一点:
You can do it with scipy.interpolate.interp2d
like this:
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(包括0和199).
Note the -1 adjustment to the coordinate range when forming x
and y
. This ensures that the image coordinates go from 0 to 199 inclusive.
这篇关于如何用线性插值插值一个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!