How can I set values in the first 3 channels of a 4 channel numpy array based on values in the 4th channel? Is it possible to do so with a numpy slice as a l-value?
Given a 3 by 2 pixel numpy array with 4 channels
a = np.arange(24).reshape(3,2,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]]])
I can select slices where the 4th channel is modulo 3.
px = np.where(0==a[:,:,3]%3)
(array([0, 1], dtype=int64), array([0, 1], dtype=int64))
a[px]
array([[ 0, 1, 2, 3],
[12, 13, 14, 15]])
Now I want to set the first 3 channels in those rows in a to 0 such that the results looks like:
a
array([[[ 0, 0, 0, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[ 0, 0, 0, 15]],
[[16, 17, 18, 19],
[20, 21, 22, 23]]])
I tried
a[px][:,0:3] = 0
but that leaves the array unchanged.
I read Setting values in a numpy arrays indexed by a slice and two boolean arrays and do not understand how to use a Boolean index to set only the first 3 channels.