0

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?

pbellot
  • 95
  • 1
  • 7

1 Answers1

1

We can leverage np.lib.stride_tricks.as_strided based scikit-image's view_as_windows to get sliding windows. More info on use of as_strided based view_as_windows.

from skimage.util.shape import view_as_windows

# Get sliding windows
w = view_as_windows(x,(2*half,2*half,2*half,1))[...,0]

# Get starting indices for indexing along the first three three axes
idx = indices-half

# Use advanced-indexing to index into first 3 axes with idx and a
# final permuting of axes to bring the output format as desired
out = np.moveaxis(w[idx[:,0],idx[:,1],idx[:,2]],1,-1)
Divakar
  • 218,885
  • 19
  • 262
  • 358