-1

Can somebody please point out the error in the code below? The last line keeps throwing an error:

tmp = img1[coords]
IndexError: index 409 is out of bounds for axis 0 with size 352

I'm only shifting the indices by 10 so I don't understand how I'm suddenly out of bounds in img1

img1 = np.random.randint(0,256, (352,870, 3), dtype=np.uint8)
img2 = np.random.randint(0,256, (44,853, 3), dtype=np.uint8)
coords = np.where(img2[:, :, 2] >= 250)
coords = np.transpose(np.transpose(coords)+(10, 10))
tmp = img1[coords]
Miki
  • 40,887
  • 13
  • 123
  • 202
deekay42
  • 616
  • 1
  • 6
  • 19
  • 2
    You get some indices from `img2`, and then try to use them, with modification, on `img1` which has a different shape. Why do you expect that to work? `np.argwhere` does `transpose(where...)`, so I can guess what it does, but haven't tried it. Show us that it is meaningful with smaller non-random arrays. – hpaulj May 09 '18 at 05:22
  • img2 has a smaller shape than img1 so all its indices should also be valid indices in img1 – deekay42 May 09 '18 at 05:26
  • 1
    The error shows that they aren't valid. Have you printed `coords` to verify that they are valid? Or that you are using them in a valid way? Remember `where` produces a tuple of arrays. After the transpose you have an array, not a tuple. Indexing with an array is not the same as indexing with a tuple. – hpaulj May 09 '18 at 05:28

2 Answers2

1

Not clear what you are trying to achieve but I assume it's this: tmp = img1[coords[0], coords[1]]

Julien
  • 13,986
  • 5
  • 29
  • 53
  • Yes! Thank you. I didnt know that the difference between index tuple and index array mattered. Can you please point me to some explanation as to how those two differ? Thanks! – deekay42 May 09 '18 at 05:42
  • It's all here: https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html, especially the "advanced indexing" section – Julien May 09 '18 at 05:46
1
In [1]: x = np.arange(24).reshape(2,3,4)
In [2]: idx = np.where(x%2)
In [3]: idx
Out[3]: 
(array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], dtype=int32),
 array([0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2], dtype=int32),
 array([1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3], dtype=int32))
In [4]: x[idx]
Out[4]: array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23])
In [5]: idx1 = np.transpose(np.transpose(idx))
In [6]: idx1
Out[6]: 
array([[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2],
       [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]], dtype=int32)

the wrong way to use idx1:

In [7]: x[idx1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-7-ffe1818b2257> in <module>()
----> 1 x[idx1]

IndexError: index 2 is out of bounds for axis 0 with size 2

The right way:

In [8]: x[tuple(idx1)]
Out[8]: array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23])

Test this with a where taken on a slice of x (I don't like assuming code works):

In [22]: idx = np.where(x[:,:,2]%3)
In [23]: idx
Out[23]: (array([0, 0, 1, 1], dtype=int32), array([0, 2, 0, 2], dtype=int32))
In [24]: idx1 = np.transpose(np.transpose(idx))
In [25]: x[idx]
Out[25]: 
array([[ 0,  1,  2,  3],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [20, 21, 22, 23]])
In [26]: x[tuple(idx1)]
Out[26]: 
array([[ 0,  1,  2,  3],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [20, 21, 22, 23]])

Indexing with a n-row array is not the same as indexing with a n-tuple of arrays. The elements of a tuple are applied to successive dimensions. The elements of the array are applied to just one dimension, the first.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Now this is the explanation i was hoping for. I was clear on most of this but your last sentence was the insight I was missing. I was under the assumption the data type didn't matter. Thanks! – deekay42 May 09 '18 at 05:49