I am new to Numpy and OpenCV. I find it odd that Numpy arrays can be indexed with ranges only for the first dimension:
>>> import numpy
>>>
>>> a = numpy.zeros((3, 3), dtype=numpy.int8)
>>>
>>> i_range = range(3)
>>> j_range = range(3)
>>>
>>> print(i_range)
range(0, 3)
>>> print(j_range)
range(0, 3)
>>> print(a[i_range, j_range])
[0 0 0]
>>> print(a[0:3, 0:3])
[[0 0 0]
[0 0 0]
[0 0 0]]
>>> a[i_range, j_range] = numpy.ones((3,3), dtype=numpy.int8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: value array of shape (3,3) could not be broadcast to indexing result of shape (3,)
>>> a[0:3, 0:3] = numpy.ones((3,3), dtype=numpy.int8)
>>> a
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]], dtype=int8)
Indexing with ranges returns a vector of length 3, indexing with full numbers returns a 3x3 array. The former throws an error when assigning values to indexed arrays, the latter works fine.
Why does that happen?