Skip to content

Instantly share code, notes, and snippets.

@duhaime
Created March 31, 2018 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duhaime/e4706906cf2ec57b9f82643c5cd405c0 to your computer and use it in GitHub Desktop.
Save duhaime/e4706906cf2ec57b9f82643c5cd405c0 to your computer and use it in GitHub Desktop.
Multidimensional sliding window in numpy
import numpy as np
def get_windows(arr, window_size=64, step=32):
windows = []
row = 0
col = 0
max_row, max_col = arr.shape
while row < max_row:
while col < max_col:
windows.append(arr[row:row+window_size, col:col+window_size])
col += step
row += step
col = 0
return windows
a = np.random.rand(4, 4)
windows = get_windows(a, window_size=2, step=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment