重新采样表示图像的numpy数组

发布于 2021-01-29 19:37:04

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

scipy.misc.imresize

通过包装PIL的调整大小功能可以做到这一点。唯一的问题是,由于它使用PIL,因此numpy数组必须符合图像格式,最多可以提供4个“颜色”通道。

我希望能够使用任意数量的“彩色”通道来调整任意图像的大小。我想知道是否有简单的方法可以在scipy / numpy中执行此操作,或者是否需要自己滚动。

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

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

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

关注者
0
被浏览
525
1 个回答
  • 面试哥
    面试哥 2021-01-30
    为面试而生,有面试问题,就找面试哥。

    根据您的描述,您想要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]]]
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看