-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_color_mean_std.py
63 lines (45 loc) · 1.59 KB
/
video_color_mean_std.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from docopt import docopt
from contextlib import contextmanager
import cv2
import numpy as np
import sys
@contextmanager
def VideoCapture(input_video):
# findFileOrKeep allows more searching paths
capture = cv2.VideoCapture(cv2.samples.findFileOrKeep(input_video))
if not capture.isOpened():
print('Unable to open: ' + input_video, file=sys.stderr)
exit(0)
try:
yield capture
finally:
# Release the video capture object at the end
capture.release()
DOCTEXT = f"""
Usage:
video_color_mean_std.py <video_path>
"""
if __name__ == '__main__':
args = docopt(DOCTEXT, argv=sys.argv[1:], help=True, version=None, options_first=False)
video_path = args['<video_path>']
with VideoCapture(video_path) as cap:
psum = np.array([0.0, 0.0, 0.0]) # BGR
psum_sq = np.array([0.0, 0.0, 0.0]) # BGR
num_px = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) * int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) * cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
nframes = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
i = 0
while True:
ret, frame = cap.read()
if not ret: # frame is None
break
i += 1
if i % 100 == 0:
print(f'{i} / {nframes}')
psum = frame.sum(axis=(0, 1))
psum_sq = (frame ** 2).sum(axis=(0, 1))
total_mean = psum / num_px
total_var = (psum_sq / num_px) - (total_mean ** 2)
total_std = np.sqrt(total_var)
print('\n\n\n')
print(f'mean (BGR): {total_mean}')
print(f'std (BGR): {total_std}')