Issue
I would very much be grateful if you don't close this question without even giving a hint of how to solve this problem,please.
I have the following
import numpy as np
layer1 = np.zeros((5,3,4),dtype=np.uint8)
layer1[0,0,0]=20
layer1[1,1,0]=20
layer1[2,2,0]=20
layer1[3,1,0]=20
layer1[4,0,0]=20
layer1[0,0,1] =50
layer1[1,0,1]=50
layer1[2,0,1]=50
print(layer1)
print("---------------")
which gives me
[[[20 50 0 0]
[ 0 0 0 0]
[ 0 0 0 0]]
[[ 0 50 0 0]
[20 0 0 0]
[ 0 0 0 0]]
[[ 0 50 0 0]
[ 0 0 0 0]
[20 0 0 0]]
[[ 0 0 0 0]
[20 0 0 0]
[ 0 0 0 0]]
[[20 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]]]
How can I reduce a get the values only of one channel ?
For example for channel=0
I want to get
[[20 0 0]
[ 0 20 0]
[ 0 0 20]
[ 0 20 0]
[20 0 0]]
where channel can be 0,1,2 or 3
EDIT: Just in case, the layer1[0,0,0]=20
is just a convenient way to fill up the matrix. My question is how to tranform layer1 once filled to the matrix of (5,3)
EDIT: if the "channel" is 1 then I would get
[[50 0 0]
[ 50 0 0]
[ 50 0 0]
[ 0 0 0]
[0 0 0]]
Solution
numpy
array indexing is well documented. Don't skip it!
In [1]: layer1 = np.zeros((5,3,4),dtype=np.uint8)
...: layer1[0,0,0]=20
...: layer1[1,1,0]=20
...: layer1[2,2,0]=20
...: layer1[3,1,0]=20
...: layer1[4,0,0]=20
...:
...: layer1[0,0,1] =50
...: layer1[1,0,1]=50
...: layer1[2,0,1]=50
In [2]: layer1.shape
Out[2]: (5, 3, 4)
In [3]: layer1[:,:,0]
Out[3]:
array([[20, 0, 0],
[ 0, 20, 0],
[ 0, 0, 20],
[ 0, 20, 0],
[20, 0, 0]], dtype=uint8)
In [4]: layer1[:,:,2]
Out[4]:
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]], dtype=uint8)
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.