I asked this question a few days ago. The question is about producing moving/rolling windows of an array in Python as in the following example:
from skimage.util import view_as_windows
view_as_windows(np.arange(10*10).reshape(10,10), (4,4),4)
Out[14]:
array([[[[ 0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23],
[30, 31, 32, 33]],
[[ 4, 5, 6, 7],
[14, 15, 16, 17],
[24, 25, 26, 27],
[34, 35, 36, 37]]],
[[[40, 41, 42, 43],
[50, 51, 52, 53],
[60, 61, 62, 63],
[70, 71, 72, 73]],
[[44, 45, 46, 47],
[54, 55, 56, 57],
[64, 65, 66, 67],
[74, 75, 76, 77]]]]).
However, this removes elements of the original arrays that don't fit into another window. It was suggested that I should fill the arrays with zeros in order to get a full window, so I wrote the following code for doing so and I thought it would be nice to have a review in case I missed corner cases:
import numpy as np
def extend_image(image, steps=16):
# Required number of rows
required_rows = np.mod(image.shape[0], steps)
# Required number of columns
required_cols = np.mod(image.shape[1], steps)
# Concatenate original array with rows filled with zeros
partial = np.concatenate((image, np.zeros((required_rows,image.shape[1]))))
# Concatenate partial array with columns filled with zeros
extended = np.concatenate((partial, np.zeros((partial.shape[0], required_cols))), axis=1)
return extended
This should work as follows:
view_as_windows(extend_image(np.arange(10*10).reshape(10,10), steps=4), (4,4),4)
array([[[[ 0., 1., 2., 3.],
[ 10., 11., 12., 13.],
[ 20., 21., 22., 23.],
[ 30., 31., 32., 33.]],
[[ 4., 5., 6., 7.],
[ 14., 15., 16., 17.],
[ 24., 25., 26., 27.],
[ 34., 35., 36., 37.]],
[[ 8., 9., 0., 0.],
[ 18., 19., 0., 0.],
[ 28., 29., 0., 0.],
[ 38., 39., 0., 0.]]],
[[[ 40., 41., 42., 43.],
[ 50., 51., 52., 53.],
[ 60., 61., 62., 63.],
[ 70., 71., 72., 73.]],
[[ 44., 45., 46., 47.],
[ 54., 55., 56., 57.],
[ 64., 65., 66., 67.],
[ 74., 75., 76., 77.]],
[[ 48., 49., 0., 0.],
[ 58., 59., 0., 0.],
[ 68., 69., 0., 0.],
[ 78., 79., 0., 0.]]],
[[[ 80., 81., 82., 83.],
[ 90., 91., 92., 93.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 84., 85., 86., 87.],
[ 94., 95., 96., 97.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 88., 89., 0., 0.],
[ 98., 99., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]]])
np.mod(img.shape[1], steps)
I don't see "img" anywhere else, only "image" \$\endgroup\$ – janos Jan 20 '15 at 21:32