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()