1

I have an ndarray I of 100 colored images, where I.shape is: (100,1,3,100,200).

This resizes a single image: i=cv2.resize(i,(10,25)), but what is an efficient way to resize all the images in I, such that the ndarray shape becomes: (100,1,3,10,25) ?

Hesham Eraqi
  • 2,444
  • 4
  • 23
  • 45

1 Answers1

2

Here is a proposition using zoom function from ndimage. The resizing takes about 69ms on my computer :

import numpy as np
I=np.random.randint(0,255,size=(100,1,3,100,200),dtype=np.uint8)

from scipy.ndimage.interpolation import zoom
I2=zoom(I,zoom=(1,1,1,1./10,1./8),order=1)
jadsq
  • 3,033
  • 3
  • 20
  • 32
  • This is a neat solution. It should be: "1./10.,1./8." though. Thank you. – Hesham Eraqi Oct 23 '16 at 11:41
  • Oh right, sorry ! Should work for python2 and 3 now. – jadsq Oct 23 '16 at 11:50
  • This was inefficient when I used 0.5 Million images for a Deep Learning application :( I wish if you have some hints to speed it up or to paralyze it. Than you. – Hesham Eraqi Nov 03 '16 at 13:35
  • @HeshamEraqi Could you be more precise as to what *efficient* means to you ? 0.5 Million 100x200 8-bit RGB images is **30 Gigabytes of data** , that's a lot of calculations and it will require a lot of ram . But as far as efficiency goes I'm really not sure what you are after ... From tests on my laptop the resizing of 20K picture (1.2GB) takes about 12.5 seconds so your 0.5M woud take about 5 minutes to process, is that not fast enough to you? – jadsq Nov 04 '16 at 12:28
  • Efficiency means faster in that case. Given the fact that an image takes 69ms to be zoomed, it would take 9.5 hours to zoom 0.5M images. I was looking for a more efficient solution, but as I couldn't, I would accept this answer as best answer. Now, I'm doing it on multiple CPU cores to make it even faster using: pool.map(preprocess_images, [imgs for imgs in chunks]). – Hesham Eraqi Nov 05 '16 at 11:32
  • 1
    @HeshamEraqi Actually the code is resizing **one *hundred* images in 69ms** so it would take **5 or 6 minutes to resize 0.5M images** – jadsq Nov 05 '16 at 11:36
  • I missed that in my calculations, you are right. Thank you. – Hesham Eraqi Nov 05 '16 at 11:38