首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >对表示图像的numpy数组进行重采样

对表示图像的numpy数组进行重采样
EN

Stack Overflow用户
提问于 2012-11-06 07:56:27
回答 6查看 101K关注 0票数 79

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

代码语言:javascript
复制
scipy.misc.imresize

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

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

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

使用scipy.ndimage.interpolation.affine_transform在我自己的每个频道上运行

  • 的函数scipy.misc.imresize

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

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-05-13 01:19:54

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

双线性插值将为order=1,最近点为order=0,默认为立方(order=3)。

zoom专门用于要重新采样到新分辨率的规则网格数据。

举个简单的例子:

代码语言:javascript
复制
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)

结果是:

代码语言:javascript
复制
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所指出的,缩放多波段图像有几个注意事项。我几乎是一字不差地从我的一个earlier answers复制下面的部分

缩放也适用于3D (和nD)阵列。但是,请注意,如果您缩放2倍,例如,您将沿所有轴缩放。

代码语言:javascript
复制
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

这会产生以下结果:

代码语言:javascript
复制
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图像,可以通过指定一个元组序列作为缩放因子来执行此操作:

代码语言:javascript
复制
print 'Zoomed by 2x along the last two axes:'
print ndimage.zoom(data, (1, 2, 2))

这会产生以下结果:

代码语言:javascript
复制
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]]]
票数 119
EN

Stack Overflow用户

发布于 2012-11-06 12:10:37

如果您想重新采样,那么您应该查看Scipy的rebinning食谱。特别是,在末尾定义的congrid函数将支持反归或插值(相当于IDL中具有相同名称的函数)。如果你不想要插值,这应该是最快的选择。

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

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

代码语言:javascript
复制
    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)。不支持“最近”类型的插值,因为它只支持矩形网格上的样条线插值。这也不是最快的方法。

如果你想要双线性或双三次插值,通常做两个一维插值要快得多:

代码语言:javascript
复制
    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',但在这种情况下,请去掉横向数组。

票数 15
EN

Stack Overflow用户

发布于 2012-11-06 20:43:43

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

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13242382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档