2

The image array shape is (540, 960, 3), and it like this:

img_rgb = [[[ 95  71  71]
  [ 95  71  71]
  [ 95  71  71]
  ...
  [182 171 181]
  [182 171 181]
  [182 171 181]]

 [[ 95  71  70]
  [ 95  71  70]
  [ 95  71  71]
  ...
  [183 172 182]
  [183 172 182]
  [183 172 182]]

 [[ 95  72  70]
  [ 95  71  70]
  [ 95  71  71]
  ...
  [183 172 182]
  [183 172 182]
  [183 172 182]]

 ...

 [[ 36  35  45]
  [ 36  35  45]
  [ 36  35  45]
  ...
  [ 49  45  50]
  [ 49  45  50]
  [ 49  45  50]]

 [[ 36  35  45]
  [ 36  35  45]
  [ 36  35  45]
  ...
  [ 49  45  50]
  [ 49  45  50]
  [ 49  45  50]]

 [[ 36  35  45]
  [ 36  35  45]
  [ 36  35  45]
  ...
  [ 49  45  50]
  [ 49  45  50]
  [ 49  45  50]]]

And I want to get elements by indices that indicate the each element index, and the indices like this:

indices = [
 [0, 0], [0, 1], [0, 2]
]

Expected output

[
  [ 95  71  71],
  [ 95  71  71],
  [ 95  71  71],
]

There are tow similar question in those link, one is Python numpy 2D array sum over certain indices, and another is Finding the (x,y) indexes of specific (R,G,B) color values from images stored in NumPy ndarrays.

It got a IndexError: too many indices for array When I tried question one by img_rgb[tuple(indices)].

luneice
  • 157
  • 1
  • 10
  • 1
    Does this answer your question? [Indexing numpy array with another numpy array](https://stackoverflow.com/questions/5508352/indexing-numpy-array-with-another-numpy-array) - you'd need to transpose though: `img_rgb[tuple(np.array(indices).T)]` – Daniel F Jan 18 '21 at 08:15
  • @DanielF or `img_rgb[tuple(zip(*indices))]` – Sayandip Dutta Jan 18 '21 at 08:23
  • Thanks, `img_rgb[tuple(np.array(indices).T)]` works. – luneice Jan 18 '21 at 08:24

1 Answers1

0

You just need to transpose indices.

img_rgb[tuple(np.transpose(indices))]

np.tranpose() would work even if indices is just a list, since it expects as input any "array-like" structure.

fountainhead
  • 3,584
  • 1
  • 8
  • 17