-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbggen.py
225 lines (173 loc) · 6.34 KB
/
bggen.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import numpy as np
import sys
import os
import cv2
import time
"""
TEKNOMO-FERNANDEZ ALGORITHM FOR BACKGROUND GENERATION
ADAPTED BY ALEJANDRO CASTANEDA AND RENEE WU
CREATED FOR COMPUTER VISION 410, FENG LIU
"""
def get_mode(img1, img2, img3):
"""
get_mode
Boolean Operation to Determine the Mode.
Compares three images and gets the binary value for the value that occurs
most frequently in the images.
ARGS --
img1, img2, img3 - A series of input images (created by cv2.imread)
RETURNS --
b - The resultant numpy array of the values through the boolean equation.
Returns the background
"""
# Per Paper: b = (g3 and (g1 xor g2)) or (g1 and g2)
b = (img3 & (img1^img2)) | (img1 & img2)
return b
def tf_background_generation(seq, L):
"""
tf_background_generation
Generates the background by iterating through a sequence of frames
a certain amount of time. Collected from the Teknomo Fernandez Algorithm
located on https://arxiv.org/abs/1510.00889.
ARGS --
seq - The sequence of image frames (list of images read by cv2.imread)
L - The amount of times the procedure is repeated until the level L.
RETURNS --
Returns an np array ready for cv2 to imwrite.
"""
result = []
if L <= 0:
print("Error, level must be a positive, nonzero number.")
return None
for i in range(3**(L-1) -1):
s = np.random.choice(len(seq), 3, replace=False)
result.append(get_mode(seq[s[0]], seq[s[1]], seq[s[2]]))
for i in range(2, L):
for j in range(3**(L-i)-1):
img1 = result[3*j]
img2 = result[3*j+1]
img3 = result[3*j+2]
result[j] = get_mode(img1, img2, img3)
return result[0]
def mc_get_mode(frames):
"""
mc_get_mode
Monte Carlo's version of Get Mode, which will get the modal bit.
The idea behind this is to utilize bit shifting to find which
bits will occur most frequently in the sequence of images provided.
* Spatial temporality is terrible with this, can be optimized.
ARGS --
frames - The frames of the images that are being put in.
RETURNS --
Returns the 'mode' for the bits of the frames provided.
"""
w, h, p = frames[0].shape
f = np.zeros((frames[0].shape))
for y in range(w):
for x in range(h):
for c in range(p):
n = 0
while n < 8:
diff = 0
for frame in frames:
pix = frame[y][x][c]
if pix & (1<<n): #np.bitwise_xor(pix,bit) > 0:
diff += 1
else:
diff -= 1
if diff > 0:
f[y][x][c] += (1<<n)#bit
n += 1
return f
def mc_background_generation(seq, S, L):
"""
mc_background_generation
Generates the background by iterating through a sequence of frames
a certain amount of time. Collected from the Monte Carlo inspired RCF Algorithm
located on https://www.researchgate.net/publication/282155172_A_Monte-Carlo-based_Algorithm_for_Background_Generation.
ARGS --
seq - The sequence of image frames (list of images read by cv2.imread)
S - Sampling size. Adjustable by user.
L - The amount of times the procedure is repeated until the level L.
RETURNS --
Returns an np array ready for cv2 to imwrite.
"""
result = []
if L <= 0:
print("Error, level must be a positive, nonzero number.")
return None
N = len(seq)
arrF = []
for i in range(S**L):
r = np.random.randint(N-1)
arrF.append(seq[r])
for l in range(L):
b = 0
for i in range(S):
f = mc_get_mode(seq[b:b+S])
b += S
arrF[i] = f
return arrF[0]
def median_filter(seq,n):
'''
median_filter
Median filter method for background subtraction. Stores all elements of an
array, then will return the median of each pixel.
ARGS:
seq - The sequence of image frames
n - a parameter for the number of frames
RETURNS:
An numpy array that outputs the image of the background
'''
if n <= 0:
print('Error, level must be a positive, nonzero number')
return None
s = np.random.choice(len(seq), n, replace=True)
img = []
for i in range(n):
img.append(seq[s[i]])
result = np.median(img, axis=0)
return result
if __name__ == "__main__":
""" python3 bggen.py sample/ sampleimg.jpg """
path = sys.argv[1]
result_path = sys.argv[2]
image_sequence = []
for image in os.listdir(path):
image_path = os.path.join(path, image)
if os.path.isfile(image_path):
img = cv2.imread(image_path)
if img is not None:
image_sequence.append(img)
start = time.time()
tfrgb_result = tf_background_generation(image_sequence, L=6)
if tfrgb_result is not None:
cv2.imwrite(filename='tfrgb'+result_path, img=tfrgb_result)
end = time.time()
print("Teknomo Fernandez ran in ", end-start, " seconds.")
start = time.time()
mcrgb_result = mc_background_generation(image_sequence, S=3, L=2)
if mcrgb_result is not None:
cv2.imwrite(filename='mcrgb'+result_path, img=mcrgb_result)
end = time.time()
print("Monte Carlo ran in ", end-start, " seconds.")
start = time.time()
median_result = median_filter(image_sequence,n = 10)
if median_result is not None:
cv2.imwrite(filename='median'+result_path, img=median_result)
end = time.time()
print("Median Filter ran in ", end-start, " seconds.")
"""
CODE USED FOR HSV -- FOUND INEFFECTIVE, THEREFORE REMOVED.
hsv_seq = []
for image in image_sequence:
hsv_seq.append(cv2.cvtColor(image, cv2.COLOR_BGR2HSV))
tfhsv_result = tf_background_generation(hsv_seq, L=6)
if tfhsv_result is not None:
tfhsvrgb_result = cv2.cvtColor(tfhsv_result, cv2.COLOR_HSV2BGR)
cv2.imwrite(filename='tfhsv'+result_path, img=tfhsvrgb_result)
mchsv_result = mc_background_generation(hsv_seq, S=3, L=3)
if mchsv_result is not None:
mchsvrgb_result = cv2.cvtColor(mchsv_result.astype('float32'), cv2.COLOR_HSV2BGR)
cv2.imwrite(filename='mchsv'+result_path, img=mchsvrgb_result)
"""