-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_eventstream.m
151 lines (125 loc) · 4 KB
/
load_eventstream.m
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
function event_data = load_eventstream(filename, mirrorX, mirrorY)
% event_data = load_eventstream(filename, mirrorX=false, mirrorY=false)
%
% loads DVS and ATIS files (however no grey level events) from .es files that have been recorded with version 2 of the ES format.
if ~exist('mirrorX','var')
mirrorX = false;
end
if ~exist('mirrorY','var')
mirrorY = false;
end
f=fopen(filename);
header = fgets(f, 12);
versions = fread(f, 3);
type = fread(f, 1);
width = fread(f,1) + bitshift(fread(f,1), 8);
height = fread(f,1) + bitshift(fread(f,1), 8);
disp(['width: ', int2str(width), ', height: ', int2str(height)])
ignore_count = 0;
if type == 2 %ATIS file
data = fread(f, 'uint8');
fclose(f);
index = 1;
pmask = bin2dec('00000010');
thresholdmask = bin2dec('00000001');
overflow = 0;
skiploop = 0;
t = 0;
start_t = zeros(width, height);
max = length(data);
for i = 1:max
if skiploop > 0
skiploop = skiploop - 1;
continue;
end
if bitand(data(i), 252) == 252
if data(i) == 252 %reset byte
continue;
else
overflow = overflow + bitand(data(i), 3);
end
else
t = t + bitshift(data(i), -2) + overflow * 63;
event_data.ts(index) = t;
overflow = 0;
x = data(i+1) + bitshift(data(i+2), 8);
event_data.x(index) = x;
y = data(i+3) + bitshift(data(i+4), 8);
event_data.y(index) = y;
if bitand(data(i), pmask) == pmask
p = 1;
else
p = 0;
end
event_data.p(index) = p;
if i < (max - 3) && bitand(data(i), thresholdmask) == thresholdmask
event_data.tc(index) = 1;
if p == 0
event_data.delta_t(index) = 0;
start_t(x+1,y+1) = t;
elseif p == 1 && start_t(x+1,y+1) ~= 0 % OFF threshold crossing after ON threshold crossing
event_data.delta_t(index) = t - start_t(x+1,y+1);
start_t(x+1,y+1) = 0;
else % we ignore an OFF threshold crossing event after another one
ignore_count = ignore_count + 1;
event_data.delta_t(index) = 0;
end
else
event_data.tc(index) = 0;
event_data.delta_t(index) = 0;
end
index = index + 1;
skiploop = 4;
end
end
elseif type == 1 %DVS file
data = fread(f, 'uint8');
fclose(f);
index = 1;
pmask = bin2dec('00000001');
overflow = 0;
skiploop = 0;
t = 0;
max = length(data);
for i = 1:max
if skiploop > 0
skiploop = skiploop - 1;
continue;
end
if bitand(data(i), 254) == 254
if data(i) == 254 %reset byte
continue;
else
overflow = overflow + 1;
end
else
t = t + bitshift(data(i), -1) + overflow * 127;
event_data.ts(index) = t;
overflow = 0;
x = data(i+1) + bitshift(data(i+2), 8);
event_data.x(index) = x;
y = data(i+3) + bitshift(data(i+4), 8);
event_data.y(index) = y;
if bitand(data(i), pmask) == pmask
p = 1;
else
p = 0;
end
event_data.p(index) = p;
index = index + 1;
skiploop = 4;
end
end
else
disp 'unsupported version'
fclose(f);
return;
end
disp(['Ignored ', int2str(ignore_count), ' threshold crossing events.'])
if mirrorX
event_data.x = (width - 1) - event_data.x;
end
if mirrorY
event_data.y = (height - 1) - event_data.y;
end
end