Мы можем использовать np.lib.stride_tricks.as_strided
на основе scikit-image's view_as_windows
, чтобы получить раздвижные окна. Дополнительная информация по использованию as_strided
на основе view_as_windows
.
from skimage.util.shape import view_as_windows
# Get sliding windows as views
w = view_as_windows(image_array,(1,size,size))[...,0,:,:]
# Index with advanced-indexing into windows array and
# assign into it, thus reassigning into input array directly being views
w[np.arange(len(x_array)),x_array,y_array] = 0
Пробный прогон
Настройка входов -
In [60]: image_array # input array
Out[60]:
array([[[54, 57, 74, 77, 77],
[19, 93, 31, 46, 97],
[80, 98, 98, 22, 68],
[75, 49, 97, 56, 98]],
[[91, 47, 35, 87, 82],
[19, 30, 90, 79, 89],
[57, 74, 92, 98, 59],
[39, 29, 29, 24, 49]],
[[42, 75, 19, 67, 42],
[41, 84, 33, 45, 85],
[65, 38, 44, 10, 10],
[46, 63, 15, 48, 27]]])
In [68]: size
Out[68]: 2
# x and y starting indices for 0s assignments
In [65]: x_array
Out[65]: array([1, 0, 1])
In [66]: y_array
Out[66]: array([2, 2, 0])
Используйте предложенное решение -
In [62]: w = view_as_windows(a,(1,size,size))[...,0,:,:]
In [63]: w[np.arange(len(x_array)),x_array,y_array] = 0
In [64]: image_array # verify
Out[64]:
array([[[54, 57, 74, 77, 77], # start at (1,2)
[19, 93, 0, 0, 97],
[80, 98, 0, 0, 68],
[75, 49, 97, 56, 98]],
[[91, 47, 0, 0, 82], # start at (0,2)
[19, 30, 0, 0, 89],
[57, 74, 92, 98, 59],
[39, 29, 29, 24, 49]],
[[42, 75, 19, 67, 42], # start at (1,0)
[ 0, 0, 33, 45, 85],
[ 0, 0, 44, 10, 10],
[46, 63, 15, 48, 27]]])