二维矩阵插值
1.1 使用库
使用scipy中的二维插值库
from scipy.interpolate import griddata
1.2 二维矩阵插值
def interpolation_2D(array, newSize):
rows, cols = array.shape
rowIndex = np.array([[i]*cols for i in np.linspace(0,100,rows)])
colIndex = np.array([list(np.linspace(0,100,cols))] * rows)
index = np.hstack([rowIndex.reshape(-1,1), colIndex.reshape(-1,1)])
newRows, newCols = newSize
newRowIndex = np.array([[i]*newCols for i in np.linspace(0,100,newRows)])
newColIndex = np.array([list(np.linspace(0,100,newCols))] * newRows)
newIndex = np.hstack([newRowIndex.reshape(-1,1), newColIndex.reshape(-1,1)])
res = griddata(index, array.reshape(-1,1), newIndex).reshape(newSize)
return res
1.3 结果展示
data = np.load('../data/test.npy')
res = interpolation_2D(data, (500,500))
fig, ax = plt.subplots(1,2,figsize=(12,6))
ax[0].imshow(data,cmap='jet',vmax=1.3)
ax[1].imshow(res,cmap='jet',vmax=1.3)