import numpy as np import cv2 cap = cv2.VideoCapture(0) kernel = np.ones((3,3),np.uint8) img=cv2.imread('carita-match.jpg',1) imgray1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray1,127,255,0) contours1, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) for i in range(0,len(contours1)): cnt = contours1[i] M = cv2.moments(cnt) if M['m00'] > 1000 and M['m00'] < 5000: cv2.drawContours(img, [cnt], 0, (9,255,0), 2) print i,M['m00'] contorno= contours1[168] while(cap.isOpened()): ret, frame = cap.read() if ret==True: imgray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,127,255,0) contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) for i in range(0 , len(contours)): cnt = contours[i] M = cv2.moments(cnt) if M['m00'] > 1500 and M['m00'] < 2000: cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) perimeter = cv2.arcLength(cnt,False) hull = cv2.convexHull(cnt) hull = cv2.convexHull(cnt,returnPoints = False) defects = cv2.convexityDefects(cnt,hull) 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) dist = cv2.matchShapes(contorno,cnt,1,0.0) print dist cv2.imshow('Frame',frame) cv2.imshow('Bordes',thresh) cv2.imshow('Bordes imagen 1',img) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break # Release everything if job is finished cap.release() cv2.destroyAllWindows()