-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathestimate_background.py
More file actions
29 lines (23 loc) · 859 Bytes
/
estimate_background.py
File metadata and controls
29 lines (23 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import argparse
import numpy as np
import cv2
argparse = argparse.ArgumentParser()
argparse.add_argument("-i", "--input", help="Input video", required=True)
argparse.add_argument("-o", "--output", help="Output path (image)", required=True)
argparse.add_argument(
"-s", "--show", help="Show the result in a window", action="store_true"
)
args = argparse.parse_args()
cap = cv2.VideoCapture(args.input)
# The median frame computation is based on 100 random frames
frame_ids = cap.get(cv2.CAP_PROP_FRAME_COUNT) * np.random.uniform(size=100)
frames = []
for frame_id in frame_ids:
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id)
_, frame = cap.read()
frames.append(frame)
median_frame = np.median(frames, axis=0).astype(dtype=np.uint8)
cv2.imwrite(args.output, median_frame)
if args.show:
cv2.imshow("frame", median_frame)
cv2.waitKey(0)