forked from andrewssobral/lrslibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.m
150 lines (126 loc) · 4.2 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
%% LRSLibrary: A <Low-Rank and Sparse tools> Library for Background Modeling and Subtraction in Videos
close, clear, clc;
% restoredefaultpath;
%% First run the setup script
lrs_setup; % or run('C:/GitHub/lrslibrary/lrs_setup')
%% GUI
lrs_gui;
%% Load configuration
lrs_load_conf;
%% UTILS
%%% 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
output_mat = fullfile(lrs_conf.lrs_dir,'dataset','demo.mat');
video2mat(input_avi, output_mat);
%%% 2D demo
M = im2double(convert_video_to_2d(video));
show_2dvideo(M,video.height,video.width);
%%% 3D demo
video3d = im2double(convert_video_to_3d(video));
show_3dvideo(video3d);
%%% 3D tensor demo
T = convert_video_to_3dtensor(video);
tensorlab.slice3(double(T)), colormap('gray');
%%% 4D demo
video4d = convert_video_to_4d(video);
video4d = crop_4dvideo(video4d,1,10);
video4d = resize_4dvideo(video4d,2);
show_4dvideo(video4d);
%% DEMO 01
load(fullfile(lrs_conf.lrs_dir,'dataset','trafficdb','traffic_patches.mat'));
V = im2double(imgdb{100});
show_3dvideo(V);
%%% Matrix-based algorithms
[M,m,n,p] = convert_video3d_to_2d(V);
show_2dvideo(M,m,n);
% Robust PCA
out = process_matrix('RPCA', 'FPCP', M, []);
% Subspace Tracking
out = process_matrix('ST', 'GRASTA', M, []);
% Matrix Completion
out = process_matrix('MC', 'GROUSE', M, []);
% Low Rank Recovery
out = process_matrix('LRR', 'FastLADMAP', M, []);
% Three-Term Decomposition
out = process_matrix('TTD', '3WD', M, []);
% Non-Negative Matrix Factorization
out = process_matrix('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 = process_tensor('NTF', 'bcuNCP', T);
% Tensor Decomposition
out = process_tensor('TD', 'Tucker-ALS', T);
% Show results
show_3dtensors(T,out.L,out.S,out.O);
%% DEMO 02
% 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 03 - For Large Videos (block by block)
input_avi = fullfile(lrs_conf.lrs_dir,'dataset','highway.avi');
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 = process_matrix('RPCA', 'GoDec', M, []);
%results = process_matrix('RPCA', 'IALM', M, []);
%results = process_matrix('RPCA', 'FPCP', M, []);
%results = process_matrix('LRR', 'FastLADMAP', M, []);
%results = process_matrix('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');