I have read Index a numpy array with another array
I have two arrays indices and image
>>> indices.shape
(1031532, 3)
>>> image.shape
(589, 461, 361)
>>> indices[0:4,:]
array([[588, 153, 24],
[588, 154, 340],
[588, 156, 214],
[588, 157, 277]], dtype=int32)
I want to set image to a certain value (say 255) for all indices contained in indices
I am new to python and I tried this:
image=np.zeros((indices[:,0].max()+1,indices[:,1].max()+1,indices[:,2].max()+1),dtype='uint8')
image[indices]=255 # understood that from the link above.
From reading , Index a numpy array with another array, I tried
Looks like I can index it with an array element by element
>>> t=tuple(indices)
>>> image[tuple(t[0])]
but this (doing it all at once without a for loop) fails:
>>> image[tuple(indices)]=255
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: too many indices for array
I am expecting that all elements of image will be zero except for those listed in matrix indices.
So image[588, 153, 24]=255 for example.
Thank you in advance.
image[ indices[:,0], indices[:,1], indices[:,2] ].indicesis (n,3) shape.tuple(indices.T)woukd be a tuple of 3 arrays. Soimage[tuple(indices.T)]will be the same.image[tuple(indices.T)].