调整一批图像的numpy

Pet*_*ers 5 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], [newHeight, newWidth], 'bilinear')
    images_resized = np.append(images_resized, np.expand_dims(temp, axis=0), axis=0)
Run Code Online (Sandbox Code Playgroud)

有什么方法可以像Apply这样的函数来更快地做到这一点吗?我研究了apply_along_axis

def resize_image(image):
    return imresize(image, [newHeight, newWidth], 'bilinear')
np.apply_along_axis(lambda x: resize_image(x), 0, images)
Run Code Online (Sandbox Code Playgroud)

但这给了

'arr' does not have a suitable array shape for any mode
Run Code Online (Sandbox Code Playgroud)

错误。

B. *_* M. 4

时间很长可能是因为resize很长:

\n\n
In [22]: %timeit for i in range(10000) : pass\n1000 loops, best of 3: 1.06 ms per loop\n
Run Code Online (Sandbox Code Playgroud)\n\n

所以时间花在resize函数上:矢量化不会提高这里的性能。

\n\n

一幅图像的估计时间为 15*60/10000= 90ms。resize使用样条曲线。这对质量有好处,但很费时间。如果目标是减小大小,则重新采样可以给出可接受的结果并且速度更快:

\n\n
In [24]: a=np.rand(752,480)\n\nIn [25]: %timeit b=a[::2,::2].copy()\n1000 loops, best of 3: 369 \xc2\xb5s per loop #\n\nIn [27]: b.shape\nOut[27]: (376, 240) \n
Run Code Online (Sandbox Code Playgroud)\n\n

速度快了大约 300 倍。这样,您可以在几秒钟内完成工作。

\n