-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
73 lines (62 loc) · 2.28 KB
/
preprocess.py
File metadata and controls
73 lines (62 loc) · 2.28 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import cv2
import numpy as np
class ProcessPipeline:
def __init__(self):
"""
ProcessPipeline constructor
"""
self.functions = []
self.params = []
self.intermediateOutputs = []
self.intermediateOutputsBGR = []
def add(self, function, **kwargs):
"""
Enqueue one function in the pipeline
:param function: a function that takes a b/w image, process it and return the processed b/w image
:param kwargs: kwargs for the function
:return: reference of self
"""
self.functions.append(function)
self.params.append(kwargs)
return self
def clear(self):
"""
Remove all functions from the pipeline
:return: reference of self
"""
self.functions = []
self.params = []
self.intermediateOutputs = []
return self
def process(self, fgmask):
"""
Execute all the functions in the pipeline
:param fgmask: b/w image to be processed
:return: b/w image processed by all the functions in the pipeline
"""
self.intermediateOutputs = [fgmask]
self.intermediateOutputsBGR = [cv2.cvtColor(fgmask, cv2.COLOR_GRAY2BGR)]
for function, kwargs in zip(self.functions, self.params):
fgmask = function(fgmask, **kwargs)
self.intermediateOutputs.append(fgmask)
self.intermediateOutputsBGR.append(cv2.cvtColor(fgmask, cv2.COLOR_GRAY2BGR))
return self.intermediateOutputs[-1]
class CompositeBackgroundSubtractor:
def __init__(self, *args):
"""
CompositeBackgroundSubtractor constructor
:param args: two or more background subtractors
"""
self.bgSubtractors = args
def apply(self, frame):
"""
Background subtraction is executed with each background subtractor, result is the OR of the subtractions
:param frame: image to segment
:return: OR between results of background subtractions
"""
fgmaskTot = np.zeros((frame.shape[0],frame.shape[1]), dtype="uint8")
for bgSub in self.bgSubtractors:
fgmask = bgSub.apply(frame)
fgmask[fgmask != 255] = 0
fgmaskTot = np.maximum(fgmask, fgmaskTot)
return fgmaskTot