Python 在numpy中调整一批图像的大小

Python 在numpy中调整一批图像的大小,python,numpy,image-resizing,Python,Numpy,Image Resizing,我在一个numpy数组(10000 x 480 x 752)中有近10000个灰度图像,希望使用scipy.misc中的imresize函数调整它们的大小。它与围绕所有图像的for循环构建一起工作,但需要15分钟 images_resized = np.zeros([0, newHeight, newWidth], dtype=np.uint8) for image in range(images.shape[0]): temp = imresize(images[image], [ne

我在一个numpy数组(10000 x 480 x 752)中有近10000个灰度图像,希望使用scipy.misc中的imresize函数调整它们的大小。它与围绕所有图像的for循环构建一起工作,但需要15分钟

images_resized = np.zeros([0, newHeight, newWidth], dtype=np.uint8)
for image in range(images.shape[0]):
    temp = imresize(images[image], [newHeight, newWidth], 'bilinear')
    images_resized = np.append(images_resized, np.expand_dims(temp, axis=0), axis=0)
是否有任何方法可以使用类似apply的函数更快地完成此操作?我沿着_轴查看应用_

def resize_image(image):
    return imresize(image, [newHeight, newWidth], 'bilinear')
np.apply_along_axis(lambda x: resize_image(x), 0, images)
但这给了我们一个机会

'arr' does not have a suitable array shape for any mode

错误。

时间很长,可能是因为
调整大小
很长:

In [22]: %timeit for i in range(10000) : pass
1000 loops, best of 3: 1.06 ms per loop
因此,时间是由
resize
函数花费的:在这里,矢量化不会提高性能

一幅图像的估计时间为15*60/10000=90ms<代码>调整大小使用样条线。这对质量很好,但很费时。如果目标是减小尺寸,则重采样可以提供可接受的结果,而且速度更快:

In [24]: a=np.rand(752,480)

In [25]: %timeit b=a[::2,::2].copy()
1000 loops, best of 3: 369 µs per loop #

In [27]: b.shape
Out[27]: (376, 240) 

大约快300倍。这样,您就可以在几秒钟内完成任务。

如果需要任何人帮助,您必须提供调整图像大小的代码。。看,我不确定这是否是您要寻找的,但看看这个:您是否有一组您正在使用的
newHeight
newWidth
的特定值?您是只进行下采样还是上采样,还是两种方式都可以?这不是您要求的,但如果您将
images\u resized=np.zero([images.shape[0],newHeight,newWidth],dtype=np.uint8)]
从一开始,只需通过枚举
images在右侧插入调整大小的
x
。调整numpy数组的大小需要时间…您最终使用@Peter做了什么?