import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while (1):
_, frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
ret, thresh = cv2.threshold(gray,50,255,0)
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([110, 175, 50])
upper_blue = np.array([130, 255, 255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
_, contours, hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
#_, contours, hierarchy = cv2.findContours(mask, 1, 2)
#cnt = contours[0]
## IndexError: list index out of range
cv2.imshow('frame', frame)
cv2.imshow('mask', mask)
cv2.imshow('thresh',thresh)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
I've been able to determine that both 'thresh' and 'mask' are matrices each of 480 rows and 640 columns and each cell contains either a '0' or '255'. Also, 'thresh' and 'mask' are of type 'numpy.ndarray'.
So why is this valid:
_, contours, hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
But this is not:
_, contours, hierarchy = cv2.findContours(mask, 1, 2)
cnt = contours[0]
# cnt = contours[0]
# IndexError: list index out of range