-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compute_optical_flow.m
66 lines (50 loc) · 1.7 KB
/
Compute_optical_flow.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
addpath('mex');
% Author: Jiawei Chen
% Revised based on Dr Chi Liu's code : https://people.csail.mit.edu/celiu/OpticalFlow/
a = load('your\data\path');% load your video data
[w,h,c,num] = size(a.images.data);
Opt_img = (zeros(32,32,3,num));
for i = 1:num-1
i
im1 = a.images.data(:,:,:,i);
im2 = a.images.data(:,:,:,i+1);
% set optical flow parameters (see Coarse2FineTwoFrames.m for the definition of the parameters)
alpha = 0.012;
ratio = 0.75;
minWidth = 20;
nOuterFPIterations = 7;
nInnerFPIterations = 1;
nSORIterations = 30;
para = [alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations];
% this is the core part of calling the mexed dll file for computing optical flow
% it also returns the time that is needed for two-frame estimation
tic;
[vx,vy,warpI2] = Coarse2FineTwoFrames(im1,im2,para);
toc
% figure;imshow(im1);figure;imshow(warpI2);
% output gif
clear volume;
volume(:,:,:,1) = im1;
volume(:,:,:,2) = im2;
% if exist('output','dir')~=7
% mkdir('output');
% end
% frame2gif(volume,fullfile('output',[example '_input.gif']));
volume(:,:,:,2) = warpI2;
% frame2gif(volume,fullfile('output',[example '_warp.gif']));
% visualize flow field
clear flow;
flow(:,:,1) = vx;
flow(:,:,2) = vy;
imflow = flowToColor(flow);
Opt_img(:,:,:,i) = imflow;
%
hold on
imshow(imflow);
shg
pause(0.1)
end
imdb.images.data = (Opt_img);
imdb.images.labels = a.images.labels;
imdb.images.set = a.images.set;
save('your/data/path','imdb')