1

I have a numpy array that looks like follows.

img = [
  [
    [135. 100.  72.],
    [124. 102.  63.],
    [161.  67.  59.],
    [102.  92. 165.],
    [127. 215. 155.]
  ],
  [
    [254. 255. 255.],
    [216. 195. 238.],
    [109. 200. 141.],
    [ 99. 141. 153.],
    [ 55. 200.  95.]
  ],
  [
    [255. 254. 255.],
    [176. 126. 221.],
    [121. 185. 158.],
    [134. 224. 160.],
    [168. 136. 113.]
  ]
]

Then I have another array which looks like follows. I'd like to treat this as a co-ordinates array for the previous one

crds = [
  [1, 3], # Corresponds to [ 99. 141. 153.] in img
  [2, 2] # Corresponds to [121. 185. 158.] in img
]

I need the following result, to be extracted from the img array.

[
  [ 99. 141. 153.],
  [121. 185. 158.]
]

How do I achieve this? Can I do this without iterating?

1
  • I think you can do img[list(zip(*crds))] Commented Aug 16, 2022 at 6:02

2 Answers 2

1

Assuming two numpy arrays as input:

img = np.array(img)
crds = np.array(crds)

you can use:

img[crds[:,0], crds[:,1]]

output:

array([[ 99, 141, 153],
       [121, 185, 158]])
Sign up to request clarification or add additional context in comments.

Comments

0

Apologies if I don't fully understand your question, but you could use indices of your coordinates as indices for your array. For example,

print(img[crds[0, 0], crds[0, 1]])

would print the result of [99 141 153]. and

print(img[crds[1, 0], crds[1, 1]])

would print the result of [121 185 158].

This is my first attempt at answering a question, so I am sorry if I misunderstood.

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.