numpynumpy-indexing

Use an array of x,y as indexes in an image array


I have an image:

>> img.shape
(720,1280)

I've identified a set of x,y coordinates I'd like to, where they are true, set a conformant image's value to 255.

Here is what I mean. Forming the indexes are my vals:

>>> vals.shape
(720, 2)

>>> vals[0]
array([  0, 186]) # the x is 0, the y is 186, I'd like to set value at img[0][186]=255

>>> vals[719]
array([719, 207]) # the x is 719, the y is 207, I'd like to set value at img[719][207]=255

The first dimension of vals is redundant with range(719).

I start by creating an image of the same shape as img:

>>> out = np.zeros_like(img)
>>> out.shape
(720, 1280)

But from here, my index into out seems not to work:

>>> out[vals] = 255
>>> out.shape
(720, 1280)
>>> out
array([[255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
>>> out.min()
255

This makes /all/ out values 255, not only those where indexes of out == vals.

I'd expect:

>>> out[0][0]
0

>>> out[0][186]
255

>>> out[719][207]
255

What am I doing wrong?


Solution

  • This works, but is really ugly:

    # out[(vals[:][:,0],vals[:][:,1])]=255
    out[(vals[:,0],vals[:,1])]=255
    

    Is there something better?