数组进行上采样和插值

数组进行上采样和插值

本文介绍了对 NumPy 数组进行上采样和插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,例如:

array = np.arange(0,4,1).reshape(2,2)

> [[0 1
    2 3]]

我想对这个数组进行上采样并插入结果值.我知道对数组进行上采样的一个好方法是使用:

I want to both upsample this array as well as interpolate the resulting values. I know that a good way to upsample an array is by using:

array = eratemp[0].repeat(2, axis = 0).repeat(2, axis = 1)
[[0 0 1 1]
 [0 0 1 1]
 [2 2 3 3]
 [2 2 3 3]]

但我无法找到一种方法来插入值以消除数组的每个 2x2 部分之间的块状"性质.

but I cannot figure out a way to interpolate the values to remove the 'blocky' nature between each 2x2 section of the array.

我想要这样的东西:

[[0 0.4 1 1.1]
 [1 0.8 1 2.1]
 [2 2.3 3 3.1]
 [2.1 2.3 3.1 3.2]]

类似的东西(注意:这些不是确切的数字).我知道可能无法对这个特定的 2D 网格进行插值,但是在我的答案中使用第一个网格,在上采样过程中应该可以进行插值,因为您正在增加像素数,因此可以填补空白"'.

Something like this (NOTE: these will not be the exact numbers). I understand that it may not be possible to interpolate this particular 2D grid, but using the first grid in my answer, an interpolation should be possible during the upsampling process as you are increasing the number of pixels, and can therefore 'fill in the gaps'.

我对插值的类型不太感兴趣,只要最终输出是平滑的表面!我曾尝试使用 scipy.interp2d 方法但无济于事,如果有人能分享他们的智慧,将不胜感激!

I am not too fussed on the type of interpolation, providing the final output is a smoothed surface! I have tried to use the scipy.interp2d method but to no avail, would be grateful if someone could share their wisdom!

推荐答案

可以使用 SciPy interp2d 进行插值,可以找到文档 这里.

You can use SciPy interp2d for the interpolation, you can find the documentation here.

我稍微修改了文档中的示例:

I've modified the example from the documentation a bit:

from scipy import interpolate
x = np.array(range(2))
y = np.array(range(2))
a = np.array([[0, 1], [2, 3]])
xx, yy = np.meshgrid(x, y)
f = interpolate.interp2d(x, y, a, kind='linear')

xnew = np.linspace(0, 2, 4)
ynew = np.linspace(0, 2, 4)
znew = f(xnew, ynew)

如果你打印znew,它应该是这样的:

If you print znew it should look like this:

array([[ 0.        ,  0.66666667,  1.        ,  1.        ],
       [ 1.33333333,  2.        ,  2.33333333,  2.33333333],
       [ 2.        ,  2.66666667,  3.        ,  3.        ],
       [ 2.        ,  2.66666667,  3.        ,  3.        ]])

这篇关于对 NumPy 数组进行上采样和插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 23:21