How to split a numpy array into overlapping tiles?

Viewed 617

How can I convert the a array into the b array as they are specified below in Python and using numpy library? I am looking for a very efficient way since my actual array that I want to use this method on is very big. I should mention that the numbers can be any number and there is no relationship among the numbers. Also, I tried to show in the below picture how I want to slice the array.

import numpy as np
a = np.arange(1, 49).reshape(6, 8)


a =   [[ 1,  2,  3,  4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13, 14, 15, 16],
       [17, 18, 19, 20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29, 30, 31, 32],
       [33, 34, 35, 36, 37, 38, 39, 40],
       [41, 42, 43, 44, 45, 46, 47, 48]]

b =[[1, 2, 9, 10], [2, 3, 10, 11], [3, 4, 11, 12], [4, 5, 12, 13],
    [5, 6, 13, 14], [6, 7, 14, 15], [7, 8, 15, 16], [9, 10, 17, 18],
    [10, 11, 18, 19], [11, 12, 19, 20], [12, 13, 20, 21], [13, 14, 21, 22],
    [14, 15, 22, 23], [15, 16, 23, 24], [17, 18, 25, 26], [18, 19, 26, 27],
    [19, 20, 27, 28], [20, 21, 28, 29], [21, 22, 29, 30], [22, 23, 30, 31],
    [23, 24, 31, 32], [25, 26, 33, 34], [26, 27, 34, 35], [27, 28, 35, 36],
    [28, 29, 36, 37], [29, 30, 37, 38], [30, 31, 38, 39], [31, 32, 39, 40], 
    [33, 34, 41, 42], [34, 35, 42, 43], [35, 36, 43, 44], [36, 37, 44, 45],
    [37, 38, 45, 46], [38, 39, 46, 47], [39, 40, 47, 48]]

I was trying to find a way with reshape and transpose function but the problem is that I could not find a way to include the boundaries. c shows what I was thinking about the solution.

c = a.reshape(3, 2, 4, 2).transpose(0, 2, 3, 1).reshape(3*4, 2*2).

The picture: https://ibb.co/QC7tkPM.

2 Answers

With numpy 1.20 or higher you can use np.lib.stride_tricks.sliding_window_view:

import numpy as np
a = np.arange(12).reshape(3, 4)
print(a)

Gives:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

Below (2,2) is the shape of the sliding window:

windows = np.lib.stride_tricks.sliding_window_view(a, (2,2))
print(windows)

It gives:

[[[[ 0  1]
   [ 4  5]]

  [[ 1  2]
   [ 5  6]]

  [[ 2  3]
   [ 6  7]]]


 [[[ 4  5]
   [ 8  9]]

  [[ 5  6]
   [ 9 10]]

  [[ 6  7]
   [10 11]]]]

In earlier versions of numpy a similar result can be obtained using np.lib.index_tricks.as_strided:

w = 2  # width and height of the sliding window

r, c = a.shape
size = a.itemsize
ast = np.lib.index_tricks.as_strided
windows_ast = ast(a,
                  shape=(r - w + 1, c - w + 1, w, w),
                  strides=(c * size, size, c * size, size))
print(windows_ast)

It gives:

[[[[ 0  1]
   [ 4  5]]

  [[ 1  2]
   [ 5  6]]

  [[ 2  3]
   [ 6  7]]]


 [[[ 4  5]
   [ 8  9]]

  [[ 5  6]
   [ 9 10]]

  [[ 6  7]
   [10 11]]]]

Note that the numpy documentation warns that np.lib.stride_tricks.as_strided should be avoided if possible, since its results may lead to several issues. The above code may also fail if the input array does not have a contiguous memory layout.

In any case, you can reshape the result to the desired shape:

windows.reshape(-1, 4)

It gives:

array([[ 0,  1,  4,  5],
       [ 1,  2,  5,  6],
       [ 2,  3,  6,  7],
       [ 4,  5,  8,  9],
       [ 5,  6,  9, 10],
       [ 6,  7, 10, 11]])

Thank you all for your solutions and insights. It just gave me a reason to update my numpy to 1.21.2. The reshape/transpose combo also can work when we are willing to repeat the rows and columns at the boundaries using np.repeat. However, it would be more costly than np.lib.stride_tricks.sliding_window_view. Here is an example:

a = np.arange(1,49).reshape(6,8)

aa = np.repeat(a, repeats=[1,2,2,2,2,1], axis=0)

aa = np.repeat(aa, repeats=[1,2,2,2,2,2,2,1], axis=1)

b = aa.reshape(5,2,7,2).transpose(0,2,3,1).reshape(5*7,2*2)
Related