【问题标题】:Resize a batch of images in numpy在numpy中调整一批图像的大小
【发布时间】:2016-03-02 09:49:21
【问题描述】:

我在一个 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_along_axis

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

错误。

【问题讨论】:

  • 如果您希望任何人帮助您,您必须提供如何调整图像大小的代码。请参阅stackoverflow.com/help/mcve
  • 我不确定这是否是您要找的东西,但请看一下:stackoverflow.com/questions/13242382/…
  • 您是否有一组特定的 newHeightnewWidth 值供您使用?您是仅进行下采样还是上采样,或者两者都可以?
  • 这不是您所要求的,但如果您从头开始创建images_resized = np.zeros([images.shape[0], newHeight, newWidth], dtype=np.uint8) 并通过枚举images 在右侧插入调整大小的x,请检查您的循环有多快。调整 numpy 数组的大小需要时间...
  • 你最后用@Peter做了什么?

标签: python numpy image-resizing


【解决方案1】:

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

In [22]: %timeit for i in range(10000) : pass
1000 loops, best of 3: 1.06 ms per loop

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

一张图片的估计时间是 15*60/10000= 90ms。 resize 使用样条线。这对质量有好处,但很耗时。如果目标是减小尺寸,重采样可以得到可接受的结果并且速度更快:

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 倍。这样,您可以在几秒钟内完成工作。

【讨论】:

    猜你喜欢
    • 2021-01-11
    • 2012-07-17
    • 2021-04-21
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多