IndexError when saving variables in a list

1

I get a series of values (they are outlines of an image) and I keep them in a list called contours .

What I intend after that is if there are contours, that is to say that the list is greater than 0, go through this list, and keep saving a series of data. etc. So far so good. The image processing part with OpenCV is no problem.

The problem is that I want to save the values of the central point of the studied figure. And for this I have created a list for the X coordinate and another for the Y. And a counter to vary the index of the list.

cnt= 0    
centroX = []
centroY = []

The thing is that when I go to work with them, I get the error:

  

centroX [cnt] = x + np.int (w / 2)

     

IndexError: list assignment index out of range

And I do not understand the reason, since I'm creating a counter called cnt and increasing it.

All the code written below is inside the main.

def main():

    cap = cv2.VideoCapture(1)

    while True:
        cnt = 0
        centroX = []
        centroY = []

        _,frame = cap.read()
        flip_frame = cv2.flip(frame, 1)

        # ****************************************************************************************
        gray = cv2.cvtColor(flip_frame,cv2.COLOR_BGR2GRAY)
        _,threshold_frame = cv2.threshold(gray,254,255,cv2.THRESH_BINARY)

        _, contours,_ = cv2.findContours(threshold_frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        #cv2.drawContours(flip_frame, contours, -1,(0,0,255),1)

        for contour in contours:
            x, y, w, h = cv2.boundingRect(contour)

            centroX[cnt] = x + np.int(w/2)
            centroY[cnt] = y + np.int(h/2)

            cv2.rectangle(flip_frame, (centroX[cnt], centroY[cnt]), (centroX[cnt], centroY[cnt]), (0, 0, 255), 3)
            cnt = cnt +1
        # ****************************************************************************************

        cv2.imshow('Ventana', flip_frame)

        # key events
        key = cv2.waitKey(1)
        if key == 27:  # esc
            break

    cap.release()
    cv2.destroyAllWindows()
    
asked by NEA 14.10.2018 в 13:34
source

2 answers

1

When you declare centroX = [] , you have a new list, but it is empty. Then, you try to access the [0] element in the first iteration, but this element does not exist and that is why you get the error.

Instead of assigning the position with centroX[cnt] , you could go adding new items to the list:

centroX.append(x + np.int(w/2))

Another option you have is to initialize the list before the cycle, so that the positions already exist, for example with:

centroX = [None]*max(contours)
    
answered by 19.11.2018 / 06:26
source
0

centroX = [ ] is a list of python, which can not be added to an np data of Numpy. So you could transform your list centroX = [] to one using numpy, for example:

'centroX = []'

centroX=np.array(centroX) and thus be able to add the data with numpy.

On the contrary, if you want to keep the list created in python, then you should not add a numpy data.

I hope I have helped you! :)

    
answered by 17.11.2018 в 20:49