import numpy as np import cv2 cap = cv2.VideoCapture(0) #cap = cv2.VideoCapture("siluetas.avi") fgbg = cv2.BackgroundSubtractorMOG() kernel = np.ones((5,5),np.uint8) # leer el primer frame ret, primerframe = cap.read() primerframe = cv2.cvtColor(primerframe,cv2.COLOR_BGR2GRAY) th3 = cv2.adaptiveThreshold(primerframe,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) primerfgmask = fgbg.apply(th3) while(1): ret, frame = cap.read() grises = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) th3 = cv2.adaptiveThreshold(grises,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) fgmask = fgbg.apply(th3) diff = primerfgmask - fgmask #copiafgmask = fgmask #erosion = cv2.dilate(copiafgmask,kernel,iterations = 3) # opcion 1 encontrar contornos contours, hierarchy = cv2.findContours(diff,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) for cont in contours: if cv2.contourArea(cont) > 1000: cv2.drawContours(frame, cont, -1, (255,255,255), 5) x,y,w,h = cv2.boundingRect(cont) cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2) # obtener el centro de masa de esos contornos # cv2.drawContours(erosion, cont, -1, (0,0,0), 1) cv2.imshow('umbral adapatativo',primerfgmask) cv2.imshow('frame',fgmask) cv2.imshow('frame o',frame) cv2.imshow('diferencia ',diff) k = cv2.waitKey(30) & 0xff if k == 27: break cap.release() cv2.destroyAllWindows()