Skip to content

Instantly share code, notes, and snippets.

@harshad-tal
Created March 10, 2017 09:23
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save harshad-tal/5a51bc0ded46b230c5322a234cf8eabf to your computer and use it in GitHub Desktop.
Save harshad-tal/5a51bc0ded46b230c5322a234cf8eabf to your computer and use it in GitHub Desktop.
Background Subtraction from video using OpenCV and Python
import numpy as np
import cv2
file_path = "vid.mp4"
cap = cv2.VideoCapture(file_path)
first_iter = True
result = None
while True:
ret, frame = cap.read()
if frame is None:
break
if first_iter:
avg = np.float32(frame)
first_iter = False
cv2.accumulateWeighted(frame, avg, 0.005)
result = cv2.convertScaleAbs(avg)
cv2.imshow("result", result)
cv2.imwrite("averaged_frame.jpg", result)
cv2.waitKey(0)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment