如何对任意维度的 Numpy 数组重新采样?
How to resample a Numpy array of arbitrary dimensions?
有 scipy.misc.imresize
用于对 3D 数组的前两个维度进行重采样。它还支持双线性插值。但是,似乎不存在用于调整具有任意维数的数组的所有维数的现有函数。如何使用多重线性插值对给定相同秩的新形状的任何数组进行重新采样?
你想要scipy.ndimage.zoom
,可以这样使用:
>>> x = np.arange(8, dtype=np.float_).reshape(2, 2, 2)
>>> scipy.ndimage.zoom(x, 1.5, order=1)
array([[[ 0. , 0.5, 1. ],
[ 1. , 1.5, 2. ],
[ 2. , 2.5, 3. ]],
[[ 2. , 2.5, 3. ],
[ 3. , 3.5, 4. ],
[ 4. , 4.5, 5. ]],
[[ 4. , 4.5, 5. ],
[ 5. , 5.5, 6. ],
[ 6. , 6.5, 7. ]]])
请注意,此函数始终保留图像的边界,本质上是对每个像素中心都有一个节点的网格进行重新采样。如果您需要更好地控制重采样发生的确切位置,您可能需要查看 scipy.ndimage
中的其他函数
有 scipy.misc.imresize
用于对 3D 数组的前两个维度进行重采样。它还支持双线性插值。但是,似乎不存在用于调整具有任意维数的数组的所有维数的现有函数。如何使用多重线性插值对给定相同秩的新形状的任何数组进行重新采样?
你想要scipy.ndimage.zoom
,可以这样使用:
>>> x = np.arange(8, dtype=np.float_).reshape(2, 2, 2)
>>> scipy.ndimage.zoom(x, 1.5, order=1)
array([[[ 0. , 0.5, 1. ],
[ 1. , 1.5, 2. ],
[ 2. , 2.5, 3. ]],
[[ 2. , 2.5, 3. ],
[ 3. , 3.5, 4. ],
[ 4. , 4.5, 5. ]],
[[ 4. , 4.5, 5. ],
[ 5. , 5.5, 6. ],
[ 6. , 6.5, 7. ]]])
请注意,此函数始终保留图像的边界,本质上是对每个像素中心都有一个节点的网格进行重新采样。如果您需要更好地控制重采样发生的确切位置,您可能需要查看 scipy.ndimage
中的其他函数