问题描述
我正在寻找如何以新的大小重新采样表示图像数据的 numpy 数组,最好选择插值方法(最近的、双线性的等).我知道有
I am looking for how to resample a numpy array representing image data at a new size, preferably having a choice of the interpolation method (nearest, bilinear, etc.). I know there is
scipy.misc.imresize
通过包装 PIL 的 resize 函数来做到这一点.唯一的问题是,由于它使用 PIL,numpy 数组必须符合图像格式,最多给我 4 个颜色"通道.
which does exactly this by wrapping PIL's resize function. The only problem is that since it uses PIL, the numpy array has to conform to image formats, giving me a maximum of 4 "color" channels.
我希望能够使用任意数量的颜色"通道调整任意图像的大小.我想知道在 scipy/numpy 中是否有一种简单的方法可以做到这一点,或者我是否需要自己动手.
I want to be able to resize arbitrary images, with any number of "color" channels. I was wondering if there is a simple way to do this in scipy/numpy, or if I need to roll my own.
关于如何自己制作一个,我有两个想法:
I have two ideas for how to concoct one myself:
- 在每个通道上分别运行
scipy.misc.imresize
的函数 - 使用
scipy.ndimage.interpolation.affine_transform
创建我自己的
- a function that runs
scipy.misc.imresize
on every channel separately - create my own using
scipy.ndimage.interpolation.affine_transform
第一个对于大数据可能会很慢,第二个似乎没有提供除样条之外的任何其他插值方法.
The first one would probably be slow for large data, and the second one does not seem to offer any other interpolation method except splines.
推荐答案
根据您的描述,您想要 scipy.ndimage.zoom
.
Based on your description, you want scipy.ndimage.zoom
.
双线性插值将是 order=1
,最近的是 order=0
,三次是默认值 (order=3
).
Bilinear interpolation would be order=1
, nearest is order=0
, and cubic is the default (order=3
).
zoom
专门用于您想要重新采样到新分辨率的规则网格数据.
zoom
is specifically for regularly-gridded data that you want to resample to a new resolution.
举个简单的例子:
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. 指出的那样,缩放多波段图像有几个注意事项.我正在从我的早期答案之一中几乎逐字复制下面的部分:
As Matt S. pointed out, there are a couple of caveats for zooming multi-band images. I'm copying the portion below almost verbatim from one of my earlier answers:
缩放也适用于 3D(和 nD)阵列.但是,请注意,例如,如果缩放 2 倍,则会沿所有轴进行缩放.
Zooming also works for 3D (and nD) arrays. However, be aware that if you zoom by 2x, for example, you'll zoom along all axes.
data = np.arange(27).reshape(3,3,3)
print 'Original:
', 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"进行插值.轴,创建新的波段.
In the case of multi-band images, you usually don't want to interpolate along the "z" axis, creating new bands.
如果您有想要缩放的 3 波段 RGB 图像之类的东西,您可以通过将元组序列指定为缩放因子来实现:
If you have something like a 3-band, RGB image that you'd like to zoom, you can do this by specifying a sequence of tuples as the zoom factor:
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]]]
这篇关于对表示图像的 numpy 数组重新采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!