import cv2 import numpy as np cap = cv2.VideoCapture(0) def mayorContorno(contornos): mayor=0;puntosmax=0;i=0 for cont in contornos: # print puntosmax, len(cont) if puntosmax < len(cont): mayor = i puntosmax=len(cont) i = i + 1 return mayor while(1): ret ,frame = cap.read() img_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(img_gray, 127, 255,0) contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cnt = contours[mayorContorno(contours)] #print len(cnt),cnt if len(cnt) > 5: hull = cv2.convexHull(cnt,returnPoints = False) defects = cv2.convexityDefects(cnt,hull) #print type(defects) if defects is None: print "nada" else: for i in range(defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) cv2.line(frame,start,end,[0,255,0],2) cv2.circle(frame,far,5,[0,0,255],-1) cv2.imshow('img',frame) k = cv2.waitKey(60) & 0xff if k == 27: break cv2.destroyAllWindows() cap.release()