插入 2d numpy 数组以改变形状

Interpolate 2d numpy array to change shape

我有一个形状为 (960, 2652) 的 numpy 数组,我想使用 linear/cubic 插值将其大小更改为 (1000, 1600)

>>> print(arr.shape)
(960, 2652)

我检查了 and answer, which recommends to use scipy.interpolate.interp2d,但我应该提供什么作为 xy

from scipy import interpolate
f = interpolate.interp2d(x, y, arr, kind='cubic')

旧域上的数据点,即

import numpy as np
from scipy import interpolate
from scipy import misc
import matplotlib.pyplot as plt

arr = misc.face(gray=True)

x = np.linspace(0, 1, arr.shape[0])
y = np.linspace(0, 1, arr.shape[1])
f = interpolate.interp2d(y, x, arr, kind='cubic')

x2 = np.linspace(0, 1, 1000)
y2 = np.linspace(0, 1, 1600)
arr2 = f(y2, x2)

arr.shape # (768, 1024)
arr2.shape # (1000, 1600)

plt.figure()
plt.imshow(arr)
plt.figure()
plt.imshow(arr2)

skimage.transform.resize 是一种非常方便的方法:

import numpy as np
from skimage.transform import resize
import matplotlib.pyplot as plt
from scipy import misc
    
arr = misc.face(gray=True)

dim1, dim2 = 1000, 1600
    
arr2= resize(arr,(dim1,dim2),order=3)  #order = 3 for cubic spline
    
print(arr2.shape) 

plt.figure()
plt.imshow(arr)
plt.figure()
plt.imshow(arr2)