import numpy as np import cv2 cap = cv2.VideoCapture(0) def mayorContorno(contornos): mayor=0;puntosmax=0;i=0 for cont in contornos: # print puntosmax, len(cont) if puntosmax < cv2.contourArea(cont): mayor = i puntosmax=cv2.contourArea(cont) i = i + 1 return mayor # Define the codec and create VideoWriter object #ourcc = cv2.cv.CV_FOURCC(*'XVID') # cv2.VideoWriter_fourcc() does not exist #ut = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) kernel = np.ones((3,3),np.uint8) while(cap.isOpened()): ret, frame = cap.read() if ret==True: img = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) # Output dtype = cv2.CV_8U edges = cv2.Canny(img,100,240) contours, hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) micontorno= mayorContorno(contours) print len(contours[micontorno]) # cv2.drawContours(frame, contours, micontorno, (0,255,255), 1) cnt = contours[micontorno] M = cv2.moments(cnt) # hull = cv2.convexHull(cnt) # print len(hull),hull[0] cv2.drawContours(frame, contours, -1, (0,255,255), 4) if M['m00'] != 0: cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) perimeter = cv2.arcLength(cnt,False) # print( "cx=%d cy=%d Area=%d o %d Perimetro=%d" % (cx, cy, M['m00'],cv2.contourArea(cnt),perimeter)) # epsilon = 0.1*cv2.arcLength(cnt,False) # approx = cv2.approxPolyDP(cnt,epsilon,False) # cv2.drawContours(frame, [approx], 0, (255,5,5), 1) cv2.imshow('frame',frame) cv2.imshow('Bordes',edges) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break # Release everything if job is finished cap.release() cv2.destroyAllWindows()