I have a cropping method, that works for (N, M)
array, and I want it to work for (T, N, M)
- see below things I've tried and don't work (hint - couldn't get np.vectorize
to work)
This is the method
def crop(image: np.ndarray) -> np.ndarray:
"""
Crop a single image
"""
image_cropped = image[~np.all(image == 0, axis=1)]
image_cropped = image_cropped[:, ~np.all(image_cropped == 0, axis=0)]
return image_cropped
What I tried
np.vectorize(crop)(sequence_of_images)
leads to this error:numpy.AxisError: axis 1 is out of bounds for array of dimension 0
np.apply_along_axis(crop, 0, sequence_of_images)
leads to this error:numpy.AxisError: axis 1 is out of bounds for array of dimension 1
How can I get this to work without using a loop? (It can be assumed that for each image over time dimension, the size will be equal after cropping, though the images cropping mask is not the same)