In [45]:
...: image = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
...: image_padded = numpy.zeros((image.shape[0] + 2, image.shape[1] + 2))
...:
1:-1
is a slice excluding the outer 2 items. It starts with 1
, and ends before the last -1
:
In [46]: image[1:,:]
Out[46]:
array([[4, 5, 6],
[7, 8, 9]])
In [47]: image[:-1,:]
Out[47]:
array([[1, 2, 3],
[4, 5, 6]])
In [48]: image[1:-1,:]
Out[48]: array([[4, 5, 6]])
Same applies to the 2d indexing.
In [49]: image_padded[1:-1, 1:-1]
Out[49]:
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
In [50]: image_padded[1:-1, 1:-1] = image
In [51]: image_padded[1:-1, 1:-1]
Out[51]:
array([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
In [52]: image_padded
Out[52]:
array([[0., 0., 0., 0., 0.],
[0., 1., 2., 3., 0.],
[0., 4., 5., 6., 0.],
[0., 7., 8., 9., 0.],
[0., 0., 0., 0., 0.]])
Adjacent differences are taken with expressions like image[1:] - image[:-1]
.