I have a numpy array with a size of 240 x 320 x 3
and I want to use a window (window size ws=5
) to slide over each pixel and crop out the subarray centered at that pixel. The final output dimension should be 240 x 320 x ws x ws x 3
. So I pad the original array with window size and use for loop
to do so.
height = 240
width = 320
image = np.random.rand((height, width, 3))
image = np.pad(image, ((ws//2, ws//2), (ws//2, ws//2), (0, 0)), mode='reflect')
patches = np.zeros((height, width, ws, ws, 3))
for i in range(height):
for j in range(width):
patches[i, j] = image[i:i+ws, j:j+ws]
Are there any ways to do the cropping of each pixel at the sample time? Like without using the for loop
over each pixel?