1

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.

4
  • image[ indices[:,0], indices[:,1], indices[:,2] ]. indices is (n,3) shape. tuple(indices.T) woukd be a tuple of 3 arrays. So image[tuple(indices.T)] will be the same. Commented Oct 15, 2023 at 7:18
  • In particular, this answer: stackoverflow.com/a/62370504/6275103 Commented Oct 15, 2023 at 7:23
  • Basically for you, image[tuple(indices.T)]. Commented Oct 15, 2023 at 7:23
  • @DjaouadNM, thanks very much. That link and the command helped. Commented Oct 16, 2023 at 0:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.