I am struggling with a vectorization problem which seems to be simple at first glance:
Let's say I have 100 images of size (7, 7) with 2 channels represented by a numpy
array of size (100, 2, 7, 7). I would like to extract on all images small patches (let say of size (2, 3, 3)), but these patches are not located at the same place on every image. Locations of the patch are described by a matrix of size (2, 100) (one x and one y for each image).
I am able to do that with a for loop over all images, but it takes time.
Here is an example code :
data = np.arange(9800).reshape((100, 2, 7, 7))
size = 3
pos = np.random.randint(0, 7-size, (2, 100))
for i in range(a.shape[0]):
patch = data[i, :, pos[0,i]:(pos[0,i]+size), pos[1,i]:pos[1,i]+size]
In other words, I'd like to reproduce this code without the for loop. Does anyone have a clue to do that?