7

I have close to 10000 greyscale images in a numpy array (10000 x 480 x 752) and would like to resize them with the imresize function from scipy.misc. It works with a for loop build around all the images, but it takes 15 minutes.

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)

Is there any way to do this faster with an apply like function? I looked into apply_along_axis

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

but this gives an

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

error.

Peter Lenaers
  • 419
  • 3
  • 8
  • 17
  • 1
    You must provide the code how you resize the images if you want anyone to help you.. see https://stackoverflow.com/help/mcve – hruske Mar 02 '16 at 09:57
  • i'm not sure whether is that what you are looking for, but take a look at this: http://stackoverflow.com/questions/13242382/resampling-a-numpy-array-representing-an-image – MaxU - stand with Ukraine Mar 02 '16 at 10:02
  • Do you have a specific set of values for `newHeight`, `newWidth` that you are working with? Are you downsampling or upsampling only or could it be either way? – Divakar Mar 02 '16 at 10:04
  • 1
    This is not what you ask for but check how much faster your loop is if you make `images_resized = np.zeros([images.shape[0], newHeight, newWidth], dtype=np.uint8)` from start and just insert the resized on the right `x` by enumerating `images`. It takes time to resize numpy arrays... – deinonychusaur Mar 02 '16 at 11:30
  • What did you end up using @Peter? – mibrahimy Mar 26 '19 at 02:09

1 Answers1

4

the time is long probably because resize is long :

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

so the time is spend by the resize function: Vectorisation will not improve performance here.

Estimate time for one image is 15*60/10000= 90ms. resize use splines. It is good for quality, but time consuming. if the goal is to reduce size, resampling can give acceptable results and is faster:

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) 

It's about 300x faster. This way, you can achieve the job in a few seconds.

B. M.
  • 18,243
  • 2
  • 35
  • 54