如何重新采样代表图像的 NumPy 数组?
在本文中,我们将对表示图像的 NumPy 数组进行重采样。为此,我们使用 scipy 包。 Scipy 包带有ndimage.zoom()方法,它通过使用给定顺序的样条插值放大到 NumPy 数组来为我们准确地做到这一点。默认为 3 阶(又名立方)。
对于包含虚部的输入,scipy。 ndimage.zoom,独立缩放实部和虚部。
Syntax: scipy.ndimage.zoom(input, zoom, output=None, order=3, mode=’constant’, cval=0.0, prefilter=True, *, grid_mode=False)
Parameters:
- Input: It defines the ndarray
- zoom : It takes both a sequence or a single number , if a single number it means apply zoom with same value in on all axis , if a sequence is provided then apply in the given order to x,y,z…etc.
- Output: By default the output of same dtype of input will be created.
- Order: Spline interpolation value , it must range between [0,5] inclusive.
- Mode**: One of the most important parameters which decides how the interpolation must happen beyond for boundary pixels it can take values from this list [‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’].
- prefilter : Takes boolean value and determine if the input array should be prefiltered with spline filter before interpolation or not.
Returns: A ndarray zoomed input.
例子
为了完成我们的缩放任务,我们将首先创建一个如下所示的 ndarray:
Python3
import numpy as np
import scipy.ndimage
ndarray = np.array([[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
[41, 42, 43, 44]])
print(ndarray)
Python3
print(scipy.ndimage.zoom(
ndarray, 2, order = 0))
Python3
print(scipy.ndimage.zoom(
ndarray, 2, order = 1))
Python3
import numpy as np
import scipy.ndimage
ndarray = np.array([[[11, 12, 13, 14],
[21, 22, 23, 24]],
[[31, 32, 33, 34],
[41, 42, 43, 44]]])
print(ndarray)
print(scipy.ndimage.zoom(ndarray, 1).shape)
Python3
import numpy as np
import scipy.ndimage
ndarray = np.array([[[11, 12, 13, 14],
[21, 22, 23, 24]],
[[31, 32, 33, 34],
[41, 42, 43, 44]]])
print(scipy.ndimage.zoom(ndarray, (2, 2, 4)))
输出:
[[11 12 13 14]
[21 22 23 24]
[31 32 33 34]
[41 42 43 44]]
示例 1:在本示例中,我们将通过
- ndarray 作为输入数组
- zoom: 2 (用值缩放)
- 阶数:0(样条插值)
因为 order = 0 和 zoom = 2 所以,缩放是在轴上完成的,具有相同的值。
蟒蛇3
print(scipy.ndimage.zoom(
ndarray, 2, order = 0))
输出:
[[11 11 12 12 13 13 14 14]
[11 11 12 12 13 13 14 14]
[21 21 22 22 23 23 24 24]
[21 21 22 22 23 23 24 24]
[31 31 32 32 33 33 34 34]
[31 31 32 32 33 33 34 34]
[41 41 42 42 43 43 44 44]
[41 41 42 42 43 43 44 44]]
示例 2:在本示例中,我们将通过
- ndarray 作为输入数组
- zoom : 2(用值缩放)
- 顺序:1(样条插值)
因为 order = 1 和 zoom = 2 所以,缩放是在轴上完成的,值+轴即;值+4。
蟒蛇3
print(scipy.ndimage.zoom(
ndarray, 2, order = 1))
输出:
[[11 11 12 12 13 13 14 14]
[15 16 16 17 17 17 18 18]
[20 20 20 21 21 22 22 23]
[24 24 25 25 26 26 26 27]
[28 29 29 29 30 30 31 31]
[32 33 33 34 34 35 35 35]
[37 37 38 38 38 39 39 40]
[41 41 42 42 43 43 44 44]]
示例 3:在多波段图像的情况下,我们通常不想沿 z 轴进行插值以创建向图像中添加新波段,因此我们应该为缩放因子参数传递一个序列而不是单个数字.
蟒蛇3
import numpy as np
import scipy.ndimage
ndarray = np.array([[[11, 12, 13, 14],
[21, 22, 23, 24]],
[[31, 32, 33, 34],
[41, 42, 43, 44]]])
print(ndarray)
print(scipy.ndimage.zoom(ndarray, 1).shape)
输出:
[[[11 12 13 14]
[21 22 23 24]]
[[31 32 33 34]
[41 42 43 44]]]
(2, 2, 4)
示例 4:
蟒蛇3
import numpy as np
import scipy.ndimage
ndarray = np.array([[[11, 12, 13, 14],
[21, 22, 23, 24]],
[[31, 32, 33, 34],
[41, 42, 43, 44]]])
print(scipy.ndimage.zoom(ndarray, (2, 2, 4)))
输出:
[[[11 11 11 11 12 12 12 12 13 13 13 13 14 14 14 14]
[14 14 14 14 14 15 15 15 15 15 16 16 16 16 17 17]
[18 18 19 19 19 19 20 20 20 20 20 21 21 21 21 21]
[21 21 21 21 22 22 22 22 23 23 23 23 24 24 24 24]]
[[16 16 16 17 17 17 17 18 18 18 18 18 19 19 19 19]
[19 19 19 19 20 20 20 20 20 21 21 21 21 22 22 22]
[24 24 24 24 24 25 25 25 25 25 26 26 26 26 27 27]
[26 26 26 27 27 27 27 28 28 28 28 28 29 29 29 29]]
[[26 26 26 26 27 27 27 27 27 28 28 28 28 29 29 29]
[28 28 29 29 29 29 30 30 30 30 30 31 31 31 31 31]
[33 33 33 34 34 34 34 35 35 35 35 35 36 36 36 36]
[36 36 36 36 37 37 37 37 37 38 38 38 38 39 39 39]]
[[31 31 31 31 32 32 32 32 33 33 33 33 34 34 34 34]
[34 34 34 34 34 35 35 35 35 35 36 36 36 36 37 37]
[38 38 39 39 39 39 40 40 40 40 40 41 41 41 41 41]
[41 41 41 41 42 42 42 42 43 43 43 43 44 44 44 44]]]