1
>>> image = np.arange(20).reshape((4, 5))
>>> image
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> idx = [[2, 1], [2, 3], [3, 4]]

How do I get the values from the image array which coordinate is specified in idx? From the above code, I want to get the values 11 (image[2, 1]), 13 (image[2, 3]), and 19 (image[3, 4]). Thank you.

1
  • 1
    13 is at image [2,3]. I cannot edit, as it takes less than six chars. Commented Aug 25, 2018 at 22:23

2 Answers 2

2

(if ya gonna use numpy, use numpy)

Make definitions:

>>> image = np.arange(20).reshape((4, 5))
>>> idx = np.array([[2, 1], [2, 3], [3, 4]]).T

Solution using fancy indexing capabilities of Numpy:

>>> image[tuple(idx)]
array([11, 13, 19])
Sign up to request clarification or add additional context in comments.

1 Comment

image[tuple(idx)] gives array([11, 13, 18]) using your idx definition. Found at the very end of docs.scipy.org/doc/numpy/user/basics.indexing.html
0

You can use a list comprehension:

[image[x[0], x[1]] for x in idx]

>>> [11, 13, 19]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.