forked from andrewssobral/lrslibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.m
169 lines (140 loc) · 4.94 KB
/
demo.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
%% LRSLibrary: A library of low-rank and sparse tools for background/foreground separation in videos
close, clear, clc;
% restoredefaultpath;
%% First run the setup script
lrs_setup; % or run('C:/GitHub/lrslibrary/lrs_setup')
%% LRS GUI (graphical user interface)
lrs_gui;
%% Load configuration (for demos)
lrs_load_conf;
%% Auxiliary functions
%%% Load video
input_avi = fullfile(lrs_conf.lrs_dir,'dataset','demo.avi');
video = load_video_file(input_avi);
show_video(video);
%%% Convert video to MAT (MATLAB format)
output_mat = fullfile(lrs_conf.lrs_dir,'dataset','demo.mat');
video2mat(input_avi, output_mat);
%%% Convert video to matrix representation (2D array)
M = im2double(convert_video_to_2d(video));
show_2dvideo(M,video.height,video.width);
%%% Convert video to 3D array
V = im2double(convert_video_to_3d(video));
show_3dvideo(V);
%%% Convert video to 3D tensor
T = convert_video_to_3dtensor(video);
tensorlab.slice3(double(T)), colormap('gray'); % Visualize a third-order tensor with slices.
%%% Convert video to 4D array/tensor
video4d = convert_video_to_4d(video);
video4d = crop_4dvideo(video4d,1,10);
video4d = resize_4dvideo(video4d,2);
show_4dvideo(video4d);
%% DEMO 01 (process matrix/tensor data)
load(fullfile(lrs_conf.lrs_dir,'dataset','trafficdb','traffic_patches.mat'));
V = im2double(imgdb{100});
show_3dvideo(V);
%%% For matrix-based algorithms
[M,m,n,p] = convert_video3d_to_2d(V);
show_2dvideo(M,m,n);
% Robust PCA
out = run_algorithm('RPCA', 'FPCP', M);
% Subspace Tracking
out = run_algorithm('ST', 'GRASTA', M);
% Matrix Completion (sample randomly 50% of the observations)
out = run_algorithm('MC', 'GROUSE', M);
% Low Rank Recovery
out = run_algorithm('LRR', 'FastLADMAP', M);
% Three-Term Decomposition
out = run_algorithm('TTD', '3WD', M);
% Non-Negative Matrix Factorization
out = run_algorithm('NMF', 'ManhNMF', M);
% Show results
show_results(M,out.L,out.S,out.O,p,m,n);
%%% Tensor-based algorithms
T = tensor(V);
% Non-Negative Tensor Factorization
out = run_algorithm('NTF', 'bcuNCP', T);
% Tensor Decomposition
out = run_algorithm('TD', 'Tucker-ALS', T);
% Show results
show_3dtensors(T,out.L,out.S,out.O);
%% DEMO 02 (data completion)
load(fullfile(lrs_conf.lrs_dir,'dataset','trafficdb','traffic_patches.mat'));
V = im2double(imgdb{100});
show_3dvideo(V);
%%% For matrix-based algorithms
[M,m,n,p] = convert_video3d_to_2d(V);
show_2dvideo(M,m,n);
%%% Define observed entries
obs = 0.5; % Percentual [0...1]
[params.Idx, params.Omega] = subsampling(M, obs);
%%% Matrix completion
out = run_algorithm('MC', 'GROUSE', M, params);
%%% Show results
show_results(M.*out.Omega,out.L,out.S,out.O,p,m,n);
%% DEMO 03 (process video data)
% Load video
input_avi = fullfile(lrs_conf.lrs_dir,'dataset','demo.avi');
output_avi = fullfile(lrs_conf.lrs_dir,'output','output.avi');
% Robust PCA
process_video('RPCA', 'FPCP', input_avi, output_avi);
% Subspace Tracking
process_video('ST', 'GRASTA', input_avi, output_avi);
% Matrix Completion
process_video('MC', 'GROUSE', input_avi, output_avi);
% Low Rank Recovery
process_video('LRR', 'FastLADMAP', input_avi, output_avi);
% Three-Term Decomposition
process_video('TTD', '3WD', input_avi, output_avi);
% Non-Negative Matrix Factorization
process_video('NMF', 'ManhNMF', input_avi, output_avi);
% Non-Negative Tensor Factorization
process_video('NTF', 'bcuNCP', input_avi, output_avi);
% Tensor Decomposition
process_video('TD', 'Tucker-ALS', input_avi, output_avi);
%% DEMO 04 - Process large videos (block by block)
input_avi = fullfile(lrs_conf.lrs_dir,'dataset','highway.mpg');
video = load_video_file(input_avi);
% show_video(video);
M_total = [];
L_total = [];
S_total = [];
O_total = [];
M = []; k = 1; k_max = 50;
nframes = 250; % video.nrFramesTotal;
for i = 1 : nframes
%disp(['#frame ' num2str(i)]);
frame = video.frames(i).cdata;
if(size(frame,3) == 3)
frame = rgb2gray(frame);
end
I = reshape(frame,[],1);
M(:,k) = I;
if(k == k_max || i == nframes)
disp(['#last frame ' num2str(i)]);
M = im2double(M);
tic;
out = run_algorithm('RPCA', 'GoDec', M, []);
%results = run_algorithm('RPCA', 'IALM', M, []);
%results = run_algorithm('RPCA', 'FPCP', M, []);
%results = run_algorithm('LRR', 'FastLADMAP', M, []);
%results = run_algorithm('NMF', 'NMF-MU', M, []);
toc
M_total = [M_total M];
L_total = [L_total out.L];
S_total = [S_total out.S];
O_total = [O_total out.O];
displog('Displaying results...');
show_results(M,out.L,out.S,out.O,size(M,2),video.height,video.width);
M = []; k = 0;
%break;
end
k = k + 1;
end
disp('Finished');
%% Show results
show_results(M_total,L_total,S_total,O_total,size(M_total,2),video.height,video.width);
%% Convert 2D matrix to AVI
convert_video2d_to_avi(S_total,size(S_total,2),video.height,video.width,'output/highway_S.avi');
convert_video2d_to_avi(L_total,size(L_total,2),video.height,video.width,'output/highway_L.avi');
convert_video2d_to_avi(O_total,size(O_total,2),video.height,video.width,'output/highway_O.avi');