Search code examples
pythonnumpyopencv

IndexError: an index can only have a single ellipsis


Does anybody know how to fix this line of code:

num = (frame[...,...,2] > 236)

I get this error while executing:

IndexError: an index can only have a single ellipsis ('...')
           

Required modules: cv2,numpy
My python version is: 2.7.14


Solution

  • You should use this:

    num = (frame[:,:,2] > 236)
    

    if you want to be explicit about the indices (given you tried to insert multiple ellipses), or just use one ellipsis:

    num = (frame[...,2] > 236)
    

    As the ellipsis is meant to be used only once, to replace as many colons as needed (two in your case).

    Test:

    >>> frame = np.meshgrid(range(0,4), range(0,2), range(0,3))[0]
    >>> frame[:,:,2]
    array([[0, 1, 2, 3],
           [0, 1, 2, 3]])
    >>> frame[...,2]
    array([[0, 1, 2, 3],
           [0, 1, 2, 3]])