82

我正在寻找如何以新的大小对表示图像数据的 numpy 数组进行重新采样,最好可以选择插值方法(最近的、双线性的等)。我知道有

scipy.misc.imresize

它通过包装 PIL 的 resize 函数来做到这一点。唯一的问题是,由于它使用 PIL,numpy 数组必须符合图像格式,最多给我 4 个“颜色”通道。

我希望能够使用任意数量的“颜色”通道调整任意图像的大小。我想知道在 scipy/numpy 中是否有一种简单的方法可以做到这一点,或者我是否需要自己动手。

对于如何自己制作一个,我有两个想法:

  • scipy.misc.imresize单独在每个通道上运行的功能
  • 创建我自己的使用scipy.ndimage.interpolation.affine_transform

对于大数据,第一个可能会很慢,第二个似乎不提供除样条曲线之外的任何其他插值方法。

4

6 回答 6

122

根据您的描述,您想要scipy.ndimage.zoom.

双线性插值是order=1,最近的是order=0,三次是默认值 ( order=3)。

zoom专门用于您想要重新采样到新分辨率的定期网格化数据。

举个简单的例子:

import numpy as np
import scipy.ndimage

x = np.arange(9).reshape(3,3)

print 'Original array:'
print x

print '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', data
print '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]]]
于 2013-05-12T17:19:54.177 回答
15

如果您想重新采样,那么您应该查看 Scipy 的 rebinning食谱。特别是congrid最后定义的函数会支持rebinning或者interpolation(相当于IDL中的同名函数)。如果您不想插值,这应该是最快的选择。

您也可以直接使用scipy.ndimage.map_coordinates,它将为任何类型的重采样(包括非结构化网格)进行样条插值。我发现 map_coordinates 对于大型数组(nx,ny > 200)来说很慢。

对于结构化网格的插值,我倾向于使用scipy.interpolate.RectBivariateSpline. 您可以选择样条的顺序(线性、二次、三次等),甚至可以为每个轴独立选择。一个例子:

    import scipy.interpolate as interp
    f = interp.RectBivariateSpline(x, y, im, kx=1, ky=1)
    new_im = f(new_x, new_y)

在这种情况下,您正在执行双线性插值(kx = ky = 1)。不支持“最近”类型的插值,因为这只是在矩形网格上进行样条插值。这也不是最快的方法。

如果您在进行双线性或双三次插值,则执行两个 1D 插值通常要快得多:

    f = interp.interp1d(y, im, kind='linear')
    temp = f(new_y)
    f = interp.interp1d(x, temp.T, kind='linear')
    new_im = f(new_x).T

您也可以使用kind='nearest',但在这种情况下摆脱横向数组。

于 2012-11-06T04:10:36.687 回答
10

你看过Scikit-image吗?它的transform.pyramid_*功能可能对您有用。

于 2012-11-06T12:43:42.620 回答
7

我最近刚刚发现 scipy.ndimage.interpolation.zoom 的一个问题,我已将其作为错误报告提交:https ://github.com/scipy/scipy/issues/3203

作为替代方案(或至少对我而言),我发现 scikit-image 的 skimage.transform.resize 工作正常:http ://scikit-image.org/docs/dev/api/skimage.transform.html#skimage .transform.resize

但是,它与 scipy 的 interpolation.zoom 的工作方式不同 - 您可以指定所需的输出形状,而不是指定乘数。这适用于 2D 和 3D 图像。

对于 2D 图像,您可以使用 transform.rescale 并指定乘数或比例,就像使用 interpolation.zoom 一样。

于 2014-01-10T16:13:55.997 回答
2

您可以使用interpolate.interp2d.

例如,考虑由 numpy 数组表示的图像arr,您可以将其调整为任意高度和宽度,如下所示:

W, H = arr.shape[:2]
new_W, new_H = (600,300)
xrange = lambda x: np.linspace(0, 1, x)

f = interp2d(xrange(W), xrange(H), arr, kind="linear")
new_arr = f(xrange(new_W), xrange(new_H))

当然,如果您的图像有多个通道,则必须对每个通道执行插值。

于 2019-09-26T23:57:04.657 回答
0

此解决方案在不影响 RGB 通道的情况下缩放输入图像的 X 和 Y:

import numpy as np
import scipy.ndimage

matplotlib.pyplot.imshow(scipy.ndimage.zoom(image_np_array, zoom = (7,7,1), order = 1))

希望这是有用的。

于 2019-05-24T16:17:36.243 回答