根据您的描述,您想要
scipy.ndimage.zoom。
双线性插值将为
order=1,最接近为
order=0,三次为默认值(
order=3)。
zoom专门用于要重新采样为新分辨率的常规栅格数据。
作为一个简单的例子:
import numpy as npimport scipy.ndimagex = np.arange(9).reshape(3,3)print 'Original array:'print xprint 'Resampled by a factor of 2 with nearest interpolation:'print scipy.ndimage.zoom(x, 2, order=0)print 'Resampled by a factor of 2 with bilinear interpolation:'print scipy.ndimage.zoom(x, 2, order=1)print 'Resampled by a factor of 2 with cubic interpolation:'print scipy.ndimage.zoom(x, 2, order=3)
结果:
Original array:[[0 1 2] [3 4 5] [6 7 8]]Resampled by a factor of 2 with nearest interpolation:[[0 0 1 1 2 2] [0 0 1 1 2 2] [3 3 4 4 5 5] [3 3 4 4 5 5] [6 6 7 7 8 8] [6 6 7 7 8 8]]Resampled by a factor of 2 with bilinear interpolation:[[0 0 1 1 2 2] [1 2 2 2 3 3] [2 3 3 4 4 4] [4 4 4 5 5 6] [5 5 6 6 6 7] [6 6 7 7 8 8]]Resampled by a factor of 2 with cubic interpolation:[[0 0 1 1 2 2] [1 1 1 2 2 3] [2 2 3 3 4 4] [4 4 5 5 6 6] [5 6 6 7 7 7] [6 6 7 7 8 8]]
编辑: 正如Matt
S.所指出的,放大多波段图像有两个注意事项。我正在从以前的答案之一中几乎逐字地复制下面的部分:
缩放也适用于3D(和nD)阵列。但是请注意,例如,如果放大2倍,则会沿 所有 轴缩放。
data = np.arange(27).reshape(3,3,3)print 'Original:n', dataprint 'Zoomed by 2x gives an array of shape:', ndimage.zoom(data, 2).shape
这样产生:
Original:[[[ 0 1 2] [ 3 4 5] [ 6 7 8]] [[ 9 10 11] [12 13 14] [15 16 17]] [[18 19 20] [21 22 23] [24 25 26]]]Zoomed by 2x gives an array of shape: (6, 6, 6)
对于多波段图像,通常不希望沿“ z”轴进行插值,从而创建新波段。
如果您要缩放3波段RGB图像,可以通过指定一个元组序列作为缩放因子来实现:
print 'Zoomed by 2x along the last two axes:'print ndimage.zoom(data, (1, 2, 2))
这样产生:
Zoomed by 2x along the last two axes:[[[ 0 0 1 1 2 2] [ 1 1 1 2 2 3] [ 2 2 3 3 4 4] [ 4 4 5 5 6 6] [ 5 6 6 7 7 7] [ 6 6 7 7 8 8]] [[ 9 9 10 10 11 11] [10 10 10 11 11 12] [11 11 12 12 13 13] [13 13 14 14 15 15] [14 15 15 16 16 16] [15 15 16 16 17 17]] [[18 18 19 19 20 20] [19 19 19 20 20 21] [20 20 21 21 22 22] [22 22 23 23 24 24] [23 24 24 25 25 25] [24 24 25 25 26 26]]]



