-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlego.m
293 lines (271 loc) · 10.3 KB
/
lego.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
%%% FOR THE LEGO VIDEO %%%
%% Parse 'super slowmo' video of Galaxy S21+
% % 1st 1s (30 frames) -- normal 30fps for 1s
% % Next 16s (480 frames) -- 960fps for 0.5s
% % last 1s (30 frames) -- normal 30fps for 1s
% init
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% inputPath = "data/lego.mp4";
% outputPath = "data/lego_src.mp4"; % output must be avi to support custom colormaps
% crop_area = [201 1 920 720];
% resize_area = [360, 360];
% adapt_contrast = true;
% darken = 1; % higher values darkens it more; 1 for no darkening
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% V = parse_ssm_video(inputPath, outputPath, crop_area, resize_area, adapt_contrast);
% V = V/darken;
% h = implay(V, 30);
% h.Parent.Name = "Processed Source Video";
%
% export_video(outputPath, V);
%% Generate Video from v2e Events (Noisy)
init
%%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
inputH5Path = "data/lego_v2e.h5";
outputPath = "data/lego_v2e.mp4"; % output must be avi to support custom colormaps
outputFrameDuration = 0.2; % put 0 to use all events (avoids aliasing from rounding errors in this value)
outputContrast = 2; % increase contrast to see more clearly. (1) is default.
outputColormap = eventVideoColormap; % color the event video. See next next section for info
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generate events
eventInfo = get_v2e_events(inputH5Path);
disp(strcat("Number of events = ", num2str(size(eventInfo.Async, 2))));
% generate grayscale event video
VGray = generate_event_video(eventInfo, outputFrameDuration, outputContrast);
% color video
VColor = color_event_video(VGray, outputColormap);
% export video if required
if strlength(outputPath) > 0
% export_video(outputPath, VColor)
end
% show videos
hGray = implay(VGray, 30);
hGray.Parent.Name = "Grayscale";
hColor = implay(VColor, 30);
hColor.Parent.Name = "Colorised";
%% Generate events from scratch (Noisy)
init
%%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
inputPath = "data/lego_src.mp4";
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
options = EventOptions; % parameter descriptions + default values in file
options.InputPath = inputPath;
options.FPS = 960;
disp("<strong>CA: Noisy Parameters used</strong>")
options.LinLogCutoff = 20;
options.ThetaMean = 0.3;
options.ThetaStd = 0.03;
options.ThetaMin = 0.01;
options.ShotNoiseRate = 1;
options.ShotNoiseReduction = 0.25;
options.LeakNoiseRate = 0.1;
options.LPFMaxCutoff = 200;
options.LPFMinCutoffFraction = 0.1;
options.Noisy = true;
% generate asynchronous events in E.Async
eventInfo = generate_events(options);
disp(strcat("Number of events = ", num2str(size(eventInfo.Async, 2))));
%% Visualise and Export the Event Videos from scratch (Noisy)
% make sure previous section was run
%%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
outputPath = "data/lego_ca.mp4"; % leave blank to stop export of colored video
outputFrameDuration = 1;
% 5
% 0 % put 0 to use all events (avoids aliasing from rounding errors in this value)
outputContrast = 5; % adjust the grayscale video to make events easier to see
outputColormap = eventVideoColormap; % add color to outputvideo
% gray;
% eventVideoColormap;
% bone;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generate grayscale event video
VGray = generate_event_video(eventInfo, outputFrameDuration, outputContrast);
% color video
VColor = color_event_video(VGray, outputColormap);
% export video if required
if strlength(outputPath) > 0
export_video(outputPath, VColor)
end
% show videos
hGray = implay(VGray, 30);
hGray.Parent.Name = "Grayscale";
hColor = implay(VColor, 30);
hColor.Parent.Name = "Colorised";
%% Generate Video from v2e Events (Clean)
init
%%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
inputH5Path = "data/lego_v2e_clean.h5";
outputPath = "data/lego_v2e_clean.mp4"; % output must be avi to support custom colormaps
outputFrameDuration = 0; % put 0 to use all events (avoids aliasing from rounding errors in this value)
outputContrast = 2; % increase contrast to see more clearly. (1) is default.
outputColormap = eventVideoColormap; % color the event video. See next next section for info
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generate events
eventInfo = get_v2e_events(inputH5Path);
disp(strcat("Number of events = ", num2str(size(eventInfo.Async, 2))));
% generate grayscale event video
VGray = generate_event_video(eventInfo, outputFrameDuration, outputContrast);
% color video
VColor = color_event_video(VGray, outputColormap);
% export video if required
if strlength(outputPath) > 0
export_video(outputPath, VColor)
end
% show videos
hGray = implay(VGray, 30);
hGray.Parent.Name = "Grayscale";
hColor = implay(VColor, 30);
hColor.Parent.Name = "Colorised";
%% Generate events from scratch (Clean)
init
%%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
inputPath = "data/lego_src.mp4";
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
options = EventOptions; % parameter descriptions + default values in file
options.InputPath = inputPath;
options.FPS = 960;
disp("<strong>CA: Clean Parameters used</strong>")
options.LinLogCutoff = 20;
options.ThetaMean = 0.3;
options.Noisy = false;
% generate asynchronous events in E.Async
eventInfo = generate_events(options);
disp(strcat("Number of events = ", num2str(size(eventInfo.Async, 2))));
%% Visualise and Export the Event Videos from scratch (Clean)
% make sure previous section was run
%%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
outputPath = "data/lego_ca_clean.mp4"; % leave blank to stop export of colored video
outputFrameDuration = 0;
% 5
% 0 % put 0 to use all events (avoisds aliasing from rounding errors in this value)
outputContrast = 5; % adjust the grayscale video to make events easier to see
outputColormap = eventVideoColormap; % add color to outputvideo
% gray;
% eventVideoColormap;
% bone;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generate grayscale event video
VGray = generate_event_video(eventInfo, outputFrameDuration, outputContrast);
% color video
VColor = color_event_video(VGray, outputColormap);
% export video if required
if strlength(outputPath) > 0
export_video(outputPath, VColor)
end
% show videos
hGray = implay(VGray, 30);
hGray.Parent.Name = "Grayscale";
hColor = implay(VColor, 30);
hColor.Parent.Name = "Colorised";
%% Combine videos (src, noisy V2E, noisy CA)
init
%%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
outputPath = "data/lego_comb.mp4";
inputSrcPath = "data/lego_src.mp4";
inputV2EPath = "data/lego_v2e.mp4";
inputCAPath = "data/lego_ca.mp4";
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp("<strong>Combine source, noisy V2E and noisy CA videos</strong>")
inSrc = VideoReader(inputSrcPath);
inSrc = read(inSrc);
inV2E = VideoReader(inputV2EPath);
inV2E = read(inV2E);
inCA = VideoReader(inputCAPath);
inCA = read(inCA);
numF = size(inSrc, 4); % vids should have same num frames
numX = size(inSrc, 1); % vids must have same height and width
numY = size(inSrc, 2); % vids must have same height and width
V = zeros(numX, 3*numY, 3, numF, 'uint8');
progress_title('Combining (Src, V2E, CA) videos...');
for f = 1:numF
for c = 1:3
fSrc = inSrc(:,:,c,f);
fV2E = inV2E(:,:,c,f);
fCA = inCA(:,:,c,f);
if c == 2 % draw separator
fSrc(:, end) = 1;
fV2E(:, end) = 1;
end
V(:,:,c,f) = [fSrc, fV2E, fCA];
end
progress(f/numF);
end
% write Video
export_video(outputPath, V);
h = implay(V, 30);
h.Parent.Name = "Combined (Src, V2E, CA)";
%% Combine videos (Src, clean v2e, and clean CA)
init
%%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
outputPath = "data/lego_comb_clean.mp4";
inputSrcPath = "data/lego_src.mp4";
inputV2EPath = "data/lego_v2e_clean.mp4";
inputCAPath = "data/lego_ca_clean.mp4";
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp("<strong>Combine Src, clean V2E and clean CA videos</strong>")
inSrc = VideoReader(inputSrcPath);
inSrc = read(inSrc);
inV2E = VideoReader(inputV2EPath);
inV2E = read(inV2E);
inCA = VideoReader(inputCAPath);
inCA = read(inCA);
numF = size(inSrc, 4); % vids should have same num frames
numX = size(inSrc, 1); % vids must have same height and width
numY = size(inSrc, 2); % vids must have same height and width
V = zeros(numX, 3*numY, 3, numF, 'uint8');
progress_title('Combining (Src, Clean V2E, Clean CA) videos...');
for f = 1:numF
for c = 1:3
fSrc = inSrc(:,:,c,f);
fV2E = inV2E(:,:,c,f);
fClean = inCA(:,:,c,f);
if c == 2 % draw separator
fSrc(:, end) = 1;
fV2E(:, end) = 1;
end
V(:,:,c,f) = [fSrc, fV2E, fClean];
end
progress(f/numF);
end
% write Video
export_video(outputPath, V);
h = implay(V, 30);
h.Parent.Name = "Combined (Src, clean V2E, clean CA)";
%% Combine videos (clean v2e 200us interp, and clean CA 200us interp)
% init
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%% USER PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% outputPath = "data/lego_comb_clean_interp.mp4";
% inputV2EPath = "data/lego_v2e_clean_interp.mp4";
% inputCAPath = "data/lego_ca_clean_interp.mp4";
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% disp("<strong>Combine Src, clean V2E and clean CA videos</strong>")
% inV2E = VideoReader(inputV2EPath);
% inV2E = read(inV2E);
% inCA = VideoReader(inputCAPath);
% inCA = read(inCA);
%
% numF = min(size(inV2E, 4), size(inCA, 4)); % vids should have same num frames
% numX = size(inV2E, 1); % vids must have same height and width
% numY = size(inV2E, 2); % vids must have same height and width
% V = zeros(numX, 2*numY, 3, numF, 'uint8');
% progress_title('Combining (Src, Clean V2E, Clean CA) videos...');
% for f = 1:numF
% for c = 1:3
% fV2E = inV2E(:,:,c,f);
% fClean = inCA(:,:,c,f);
% if c == 2 % draw separator
% fV2E(:, end) = 1;
% end
% V(:,:,c,f) = [fV2E, fClean];
% end
% progress(f/numF);
% end
%
% % write Video
% export_video(outputPath, V);
% h = implay(V, 30);
% h.Parent.Name = "Combined (Src, clean V2E, clean CA)";