-
Notifications
You must be signed in to change notification settings - Fork 40
/
vibe_test.py
79 lines (70 loc) · 2.39 KB
/
vibe_test.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
import numpy as np
import os
import cv2
def initial_background(I_gray, N):
I_pad = np.pad(I_gray, 1, 'symmetric')
height = I_pad.shape[0]
width = I_pad.shape[1]
samples = np.zeros((height,width,N))
for i in range(1, height - 1):
for j in range(1, width - 1):
for n in range(N):
x, y = 0, 0
while(x == 0 and y == 0):
x = np.random.randint(-1, 1)
y = np.random.randint(-1, 1)
ri = i + x
rj = j + y
samples[i, j, n] = I_pad[ri, rj]
samples = samples[1:height-1, 1:width-1]
return samples
def vibe_detection(I_gray, samples, _min, N, R):
height = I_gray.shape[0]
width = I_gray.shape[1]
segMap = np.zeros((height, width)).astype(np.uint8)
for i in range(height):
for j in range(width):
count, index, dist = 0, 0, 0
while count < _min and index < N:
dist = np.abs(I_gray[i,j] - samples[i,j,index])
if dist < R:
count += 1
index += 1
if count >= _min:
r = np.random.randint(0, N-1)
if r == 0:
r = np.random.randint(0, N-1)
samples[i,j,r] = I_gray[i,j]
r = np.random.randint(0, N-1)
if r == 0:
x, y = 0, 0
while(x == 0 and y == 0):
x = np.random.randint(-1, 1)
y = np.random.randint(-1, 1)
r = np.random.randint(0, N-1)
ri = i + x
rj = j + y
try:
samples[ri, rj, r] = I_gray[i, j]
except:
pass
else:
segMap[i, j] = 255
return segMap, samples
rootDir = r'data/input'
image_file = os.path.join(rootDir, os.listdir(rootDir)[0])
image = cv2.imread(image_file, 0)
N = 20
R = 20
_min = 2
phai = 16
samples = initial_background(image, N)
for lists in os.listdir(rootDir):
path = os.path.join(rootDir, lists)
frame = cv2.imread(path)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
segMap, samples = vibe_detection(gray, samples, _min, N, R)
cv2.imshow('segMap', segMap)
if cv2.waitKey(1) and 0xff == ord('q'):
break
cv2.destroyAllWindows()