I am trying to copy a section of an input 2d array "img" and mirroring that section and copying it into the 2d array "out"
The following code does what I need
a = numpy.zeros(shape=(pad, pad))
a[:,:]=img[0:pad,0:pad]
out[0:pad,0:pad]=a[::-1,::-1]
But simply doing the following does not
out[0:pad,0:pad]=img[0:pad:-1,0:pad:-1]
and instead returns ValueError: could not broadcast input array from shape (0,0) into shape (2,2)
for pad=2
and I am not sure why.
img[0:pad:-1,0:pad:-1]
should be
img[pad-1::-1, pad-1::-1]
since you want the index to start at pad-1
and step down to 0. See here for the complete rules governing NumPy basic slicing .
For example,
import numpy as np
img = np.arange(24).reshape(6,4)
# array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11],
# [12, 13, 14, 15],
# [16, 17, 18, 19],
# [20, 21, 22, 23]])
pad = 2
out = img[pad-1::-1, pad-1::-1]
print(out)
yields
[[5 4]
[1 0]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.