-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccumulateBackground.py
More file actions
54 lines (35 loc) · 1.13 KB
/
AccumulateBackground.py
File metadata and controls
54 lines (35 loc) · 1.13 KB
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
"""
Run once to accumulate background of a video, i.e. an image with non-moving objects is generated. Every moving object will vanish.
Uncomment imwrite command in Line 53 to save background as backg.jpg
"""
import numpy as np
import cv2
import os
from PIL import Image
dir = 'C:\\Users\\xyz\\Videos'
os.chdir(dir)
fname = 'video.avi'
cap = cv2.VideoCapture(fname)
_, firstframe = cap.read()
gray = cv2.cvtColor(firstframe, cv2.COLOR_BGR2GRAY)
avg = np.float64(gray)
cv2.namedWindow('backg', cv2.WINDOW_NORMAL)
while True:
grabbed, frame = cap.read()
key = cv2.waitKey(1) & 0xFF
frame_nr = cap.get(cv2.CAP_PROP_POS_FRAMES)
print frame_nr
if not grabbed:
print "Processing " + fname + " ended"
break
img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).astype('float64')
cv2.accumulate(img_gray,avg)
backg = avg/frame_nr
cv2.imshow('backg',backg.astype('uint8'))
im = Image.fromarray(backg)
filename = 'background%i.tif' %frame_nr
im.save(filename)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#cv2.imwrite('backg.jpg',backg)
cv2.destroyAllWindows()