Created
March 10, 2017 09:23
-
-
Save harshad-tal/5a51bc0ded46b230c5322a234cf8eabf to your computer and use it in GitHub Desktop.
Background Subtraction from video using OpenCV and Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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