python - 如何使用线性插值法对 numpy 数组进行插值

标签 python arrays numpy

我有一个具有以下形状的 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)

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

关于python - 如何使用线性插值法对 numpy 数组进行插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55373964/

相关文章:

python - 使矩阵乘法运算符@适用于 numpy 中的标量

python - 如何更改Python中有序字典中的特定值

python - 使用流水线 hgetall 进行 Redis 慢速查询

Java 泛型可变参数方法参数

javascript - 第二次单击时 jQuery 数组不会被删除

python - 根据颜色值将多维 Numpy 数组转换为二维数组

python - 将字符串转换为二维 numpy 数组

python - 如何通过路径访问元素?

python - 4 个嵌套循环的时间复杂度是多少,每个循环都取决于父循环

ios - C 与 Swift 的互操作性