Given a 2D array of any size (X+2*padding, Y+2*padding,) and a window size W, the resulting array should be of shape (X, Y, W*W), (X, Y, W, W), (X*Y, W*W), (X*Y, W, W). The 2D array already has a border padding with 0. Stride is always 1.
For example (with window size = 3):
a = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 2, 3, 4, 5, 6, 0],
[0, 1, 2, 3, 4, 5, 6, 0],
[0, 1, 2, 3, 4, 5, 6, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
# Just some possible results as example (it can be in any form suggested above 2D/3D/4D):
# in case of result shape (X*Y, W, W):
result = [w1, w2, ... wn]
w1 = [[0, 0, 0],
[0, 1, 2],
[0, 1, 2]]
w2 = [[0, 0, 0],
[1, 2, 3],
[1, 2, 3]]
wn = [[5, 6, 0],
[5, 6, 0],
[0, 0, 0]]
# in case of result shape (X*Y, W*W):
result = [[0, 0, 0, 0, 1, 2, 0, 1, 2],
[0, 0, 0, 1, 2, 3, 1, 2, 3],
[0, 0, 0, 2, 3, 4, 2, 3, 4],
...
[4, 5, 6, 4, 5, 6, 0, 0, 0],
[5, 6, 0, 5, 6, 0, 0, 0, 0]]
I would be fine with any resulting shape described above as long as the resulting array consists out of all windows with a defined window size.
Is this possible by only using numpy functions/functionality (no for loops etc.)?
Note: X, Y and W should be flexible, the solution should not only work on my example
The question here: sliding window in numpy doesn't include a window size and the proposed solutions only work on the very specific array shape provided in the question or use other functionality than that provided by numpy.