I am trying to extract patches of fixed size centred at some given position (x,y,z). The code is given below:
x = np.random.randint(0,99,(150, 80, 50, 3))
patch_size = 32
half = int(patch_size//2)
indices = np.array([[40, 20, 30], [60, 30, 27], [20, 18, 21]])
n_patches = indices.shape[0]
patches = np.empty((n_patches, patch_size, patch_size,patch_size, x.shape[-1]))
for ix,_ in enumerate(indices):
patches[ix, ...] = x[indices[ix, 0]-half:indices[ix, 0]+half,
indices[ix, 1]-half:indices[ix, 1]+half,
indices[ix, 2]-half:indices[ix, 2]+half, ...]
Can anyone tell me how to make this work faster? or any other alternatives if you can suggest it would be of great help. I've seen a similar problem solved in https://stackoverflow.com/a/37901746/4296850, but only for 2D images. Could anyone help me to generalize this solution?