Given an Nx2 array of N points ([row, col]):
points = np.array([
[r1, c1],
[r2, c2],
...
])
And given a 2D matrix I want to operate on:
img = np.arange(400).reshape(20,20)
I'm looking for an efficient way to take 2D slices of img
using the indices.
So if I want a slice of a given height h
and width w
, the pseudocode would be:
p_rows = points[:,0]
p_cols = points[:,1]
patches = img[p_rows:p_rows+h, p_cols:p_cols+w]
Such that the result would be an Nxhxw matrix. But, alas, broadcasting didn't save me this time.
I've looked at np.r_
, np.select
, np.lib.stride_tricks.as_strided
, np.take
... But not had any luck yet.