-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project_3_Code.m
481 lines (468 loc) · 17.5 KB
/
Project_3_Code.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
clear all; close all; clc;
% Test 1: Ideal Case
load cam1_1.mat; % -- Load Movie cam1_1 (Ideal Case from Cam 1)
[height1_1 width1_1 rgb num_frames1_1] = size(vidFrames1_1);
x_1_1 = [];
y_1_1 = [];
for j = 1:num_frames1_1 % -- Watch Movie
X = rgb2gray(vidFrames1_1(:,:,:,j)); % convert to grey scale
%imshow(X); drawnow
X1 = double(X); % converted to double precision for mathematical processing
X1(:,1:300) = 0; % crop the video to the target region
X1(:,400:end) = 0;
X1(1:200,:) = 0;
X1(450:end,:) = 0;
[M,I] = max(X1(:));
[y,x] = ind2sub(size(X1),I);
x_1_1 = [x_1_1 x]; % record the bright spot position in each frame
y_1_1 = [y_1_1 y];
end
load cam2_1.mat; % -- Load Movie cam2_1 (Ideal Case from Cam 2)
[height2_1 width2_1 rgb num_frames2_1] = size(vidFrames2_1);
x_2_1 = [];
y_2_1 = [];
for j=1:num_frames2_1 % -- Watch Movie
X = rgb2gray(vidFrames2_1(:,:,:,j)); % convert to grey scale
%imshow(X); drawnow
X2 = double(X); % converted to double precision for mathematical processing
X2(:,1:250) = 0; % crop the video to the target region
X2(:,350:end) = 0;
X2(1:100,:) = 0;
X2(380:end,:) = 0;
[M,I] = max(X2(:));
[y,x] = ind2sub(size(X2),I);
x_2_1 = [x_2_1 x]; % record the bright spot position in each frame
y_2_1 = [y_2_1 y];
end
load cam3_1.mat; % -- Load Movie cam3_1 (Ideal Case from Cam 3)
[height3_1 width3_1 rgb num_frames3_1] = size(vidFrames3_1);
x_3_1 = [];
y_3_1 = [];
for j=1:num_frames3_1 % -- Watch Movie
X = rgb2gray(vidFrames3_1(:,:,:,j)); % convert to grey scale
%imshow(X); drawnow
X3 = double(X); % converted to double precision for mathematical processing
X3(:,1:270) = 0; % crop the video to the target region
X3(:,480:end) = 0;
X3(1:230,:) = 0;
X3(340:end,:) = 0;
[M,I] = max(X3(:));
[y,x] = ind2sub(size(X3),I);
x_3_1 = [x_3_1 x]; % record the bright spot position in each frame
y_3_1 = [y_3_1 y];
end
% make all 3 cams start at same relative position
[Min,Ind] = min(y_1_1(1:25));
x_1_1 = x_1_1(Ind:end);
y_1_1 = y_1_1(Ind:end);
[Min,Ind] = min(y_2_1(1:25));
x_2_1 = x_2_1(Ind:end);
y_2_1 = y_2_1(Ind:end);
[Min,Ind] = min(x_3_1(1:25));
x_3_1 = x_3_1(Ind:end);
y_3_1 = y_3_1(Ind:end);
% trim each movie clip down to the same size
frames_min = min([length(x_1_1),length(x_2_1),length(x_3_1)]);
x_1_1 = x_1_1(1:frames_min);
x_1_1 = x_1_1 - mean(x_1_1);
y_1_1 = y_1_1(1:frames_min);
y_1_1 = y_1_1 - mean(y_1_1);
x_2_1 = x_2_1(1:frames_min);
x_2_1 = x_2_1 - mean(x_2_1);
y_2_1 = y_2_1(1:frames_min);
y_2_1 = y_2_1 - mean(y_2_1);
x_3_1 = x_3_1(1:frames_min);
x_3_1 = x_3_1 - mean(x_3_1);
y_3_1 = y_3_1(1:frames_min);
y_3_1 = y_3_1 - mean(y_3_1);
XY_total = [x_1_1; y_1_1; x_2_1; y_2_1; x_3_1; y_3_1];
figure(1) % Plot the Raw Data Positional Data of Paint Can
subplot(3,1,1)
plot(1:frames_min, XY_total(2,:),1:frames_min, XY_total(1,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 1-Ideal Case: Raw Positional Data of Paint Can, Cam 1");
legend("Y", "X")
subplot(3,1,2)
plot(1:frames_min, XY_total(4,:),1:frames_min, XY_total(3,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 1-Ideal Case: Raw Positional Data of Paint Can, Cam 2");
legend("Y", "X")
subplot(3,1,3)
plot(1:frames_min, XY_total(6,:),1:frames_min, XY_total(5,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 1-Ideal Case: Raw Positional Data of Paint Can, Cam 3")
legend("Y", "X")
% PCA on test 1
[U,S,V] = svd(XY_total/sqrt(frames_min-1), 'econ');
sig = diag(S);
figure(2)
plot(sig.^2/sum(sig.^2),'ko','Linewidth',2)
title("Test 1-Ideal Case: Energy Captured by Each Nonzero Singular Value");
xlabel("Singular Values"); ylabel("Energy");
axis([0 6 0 1])
set(gca,'Fontsize',14,'Xtick',0:1:6)
% Create Projection on first three Principal Components
figure(3)
XY_proj = V';
plot(1:frames_min, XY_proj(1:2,:),'Linewidth',1.5)
title("Test 1-Ideal Case: Movements on Principal Components");
ylabel("Displacement"); xlabel("Time (frames)");
legend("1st Principal Component", "2nd Principal Component", ...
"3rd Principal Component",'location','southeast');
set(gca,'Fontsize',14)
%% Test 2: Noisy Case.
clear all; close all; clc;
load cam1_2.mat; % -- Load Movie cam1_2 (Noisy Case from Cam 1)
[height1_2 width1_2 rgb num_frames1_2] = size(vidFrames1_2);
x_1_2 = [];
y_1_2 = [];
for j = 1:num_frames1_2 % -- Watch Movie
X = rgb2gray(vidFrames1_2(:,:,:,j)); % convert to grey scale
% imshow(X); drawnow
X1 = double(X); % converted to double precision for mathematical processing
X1(:,1:300) = 0; % crop the video to the target region
X1(:,410:end) = 0;
X1(1:220,:) = 0;
X1(450:end,:) = 0;
[M,I] = max(X1(:));
[y,x] = ind2sub(size(X1),I);
x_1_2 = [x_1_2 x]; % record the bright spot position in each frame
y_1_2 = [y_1_2 y];
end
load cam2_2.mat; % -- Load Movie cam2_2 (Noisy Case from Cam 2)
[height2_2 width2_2 rgb num_frames2_2] = size(vidFrames2_2);
x_2_2 = [];
y_2_2 = [];
for j=1:num_frames2_2 % -- Watch Movie
X = rgb2gray(vidFrames2_2(:,:,:,j)); % convert to grey scale
% imshow(X); drawnow
X2 = double(X); % converted to double precision for mathematical processing
X2(:,1:200) = 0; % crop the video to the target region
X2(:,400:end) = 0;
X2(1:50,:) = 0;
X2(450:end,:) = 0;
[M,I] = max(X2(:));
[y,x] = ind2sub(size(X2),I);
x_2_2 = [x_2_2 x]; % record the bright spot position in each frame
y_2_2 = [y_2_2 y];
end
load cam3_2.mat; % -- Load Movie cam3_2 (Noisy Case from Cam 3)
[height3_2 width3_2 rgb num_frames3_2] = size(vidFrames3_2);
x_3_2 = [];
y_3_2 = [];
for j=1:num_frames3_2 % -- Watch Movie
X = rgb2gray(vidFrames3_2(:,:,:,j)); % convert to grey scale
%imshow(X); drawnow
X3 = double(X); % converted to double precision for mathematical processing
X3(:,1:270) = 0; % crop the video to the target region
X3(:,480:end) = 0;
X3(1:200,:) = 0;
X3(340:end,:) = 0;
[M,I] = max(X3(:));
[y,x] = ind2sub(size(X3),I);
x_3_2 = [x_3_2 x]; % record the bright spot position in each frame
y_3_2 = [y_3_2 y];
end
% make all 3 cams start at same relative position
[Min,Ind] = min(y_1_2(1:25));
x_1_2 = x_1_2(Ind:end);
y_1_2 = y_1_2(Ind:end);
[Min,Ind] = min(y_2_2(1:25));
x_2_2 = x_2_2(Ind:end);
y_2_2 = y_2_2(Ind:end);
[Min,Ind] = min(x_3_2(1:25));
x_3_2 = x_3_2(Ind:end);
y_3_2 = y_3_2(Ind:end);
% trim each movie clip down to the same size
frames_min = min([length(x_1_2),length(x_2_2),length(x_3_2)]);
x_1_2 = x_1_2(1:frames_min);
x_1_2 = x_1_2 - mean(x_1_2);
y_1_2 = y_1_2(1:frames_min);
y_1_2 = y_1_2 - mean(y_1_2);
x_2_2 = x_2_2(1:frames_min);
x_2_2 = x_2_2 - mean(x_2_2);
y_2_2 = y_2_2(1:frames_min);
y_2_2 = y_2_2 - mean(y_2_2);
x_3_2 = x_3_2(1:frames_min);
x_3_2 = x_3_2 - mean(x_3_2);
y_3_2 = y_3_2(1:frames_min);
y_3_2 = y_3_2 - mean(y_3_2);
XY_total = [x_1_2; y_1_2; x_2_2; y_2_2; x_3_2; y_3_2];
figure(1) % Plot the Raw Data Positional Data of Paint Can
subplot(3,1,1)
plot(1:frames_min, XY_total(2,:),1:frames_min, XY_total(1,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 2-Noisy Case: Raw Positional Data of Paint Can, Cam 1");
legend("Y", "X")
subplot(3,1,2)
plot(1:frames_min, XY_total(4,:),1:frames_min, XY_total(3,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 2-Noisy Case: Raw Positional Data of Paint Can, Cam 2");
legend("Y", "X")
subplot(3,1,3)
plot(1:frames_min, XY_total(6,:),1:frames_min, XY_total(5,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 2-Noisy Case: Raw Positional Data of Paint Can, Cam 3")
legend("Y", "X")
% PCA on test 2
[U,S,V] = svd(XY_total/sqrt(frames_min-1), 'econ');
sig = diag(S); % Calculating the energy of the truncations
figure(2)
plot(sig.^2/sum(sig.^2),'ko','Linewidth',2)
title("Test 2-Noisy Case: Energy Captured by Each Nonzero Singular Value");
xlabel("Singular Values"); ylabel("Energy");
axis([0 6 0 1])
set(gca,'Fontsize',14,'Xtick',0:1:6)
% Create Projection on first three Principal Components
figure(3)
XY_proj = V';
plot(1:frames_min, XY_proj(1:3,:),'Linewidth',1.5)
title("Test 2-Noisy Case: Movements on Principal Components");
ylabel("Displacement"); xlabel("Time (frames)");
legend("1st Principal Component", "2nd Principal Component", ...
"3rd Principal Component",'location','southeast');
set(gca,'Fontsize',14)
%% Test 3: Horizontal Displacement Case
clear all; close all; clc;
load cam1_3.mat; % -- Load Movie cam1_3 (Horizontal Displacement Case from Cam 1)
[height1_3 width1_3 rgb num_frames1_3] = size(vidFrames1_3);
x_1_3 = [];
y_1_3 = [];
for j = 1:num_frames1_3 % -- Watch Movie
X = rgb2gray(vidFrames1_3(:,:,:,j)); % convert to grey scale
% imshow(X); drawnow
X1 = double(X); % converted to double precision for mathematical processing
X1(:,1:280) = 0; % crop the video to the target region
X1(:,400:end) = 0;
X1(1:225,:) = 0;
X1(380:end,:) = 0;
[M,I] = max(X1(:));
[y,x] = ind2sub(size(X1),I);
x_1_3 = [x_1_3 x]; % record the bright spot position in each frame
y_1_3 = [y_1_3 y];
end
load cam2_3.mat; % -- Load Movie cam2_3 (Horizontal Displacement Case from Cam 2)
[height2_3 width2_3 rgb num_frames2_3] = size(vidFrames2_3);
x_2_3 = [];
y_2_3 = [];
for j=1:num_frames2_3 % -- Watch Movie
X = rgb2gray(vidFrames2_3(:,:,:,j)); % convert to grey scale
% imshow(X); drawnow
X2 = double(X); % converted to double precision for mathematical processing
X2(:,1:220) = 0; % crop the video to the target region
X2(:,400:end) = 0;
X2(1:180,:) = 0;
X2(380:end,:) = 0;
[M,I] = max(X2(:));
[y,x] = ind2sub(size(X2),I);
x_2_3 = [x_2_3 x]; % record the bright spot position in each frame
y_2_3 = [y_2_3 y];
end
load cam3_3.mat; % -- Load Movie cam3_3 (Horizontal Displacement Case from Cam 3)
[height3_3 width3_3 rgb num_frames3_3] = size(vidFrames3_3);
x_3_3 = [];
y_3_3 = [];
for j=1:num_frames3_3 % -- Watch Movie
X = rgb2gray(vidFrames3_3(:,:,:,j)); % convert to grey scale
% imshow(X); drawnow
X3 = double(X); % converted to double precision for mathematical processing
X3(:,1:270) = 0; % crop the video to the target region
X3(:,480:end) = 0;
X3(1:170,:) = 0;
X3(320:end,:) = 0;
[M,I] = max(X3(:));
[y,x] = ind2sub(size(X3),I);
x_3_3 = [x_3_3 x]; % record the bright spot position in each frame
y_3_3 = [y_3_3 y];
end
% make all 3 cams start at same relative position
[Min,Ind] = min(y_1_3(1:25));
x_1_3 = x_1_3(Ind:end);
y_1_3 = y_1_3(Ind:end);
[Min,Ind] = min(y_2_3(1:25));
x_2_3 = x_2_3(Ind:end);
y_2_3 = y_2_3(Ind:end);
[Min,Ind] = min(x_3_3(1:25));
x_3_3 = x_3_3(Ind:end);
y_3_3 = y_3_3(Ind:end);
% trim each movie clip down to the same size
frames_min = min([length(x_1_3),length(x_2_3),length(x_3_3)]);
x_1_3 = x_1_3(1:frames_min);
x_1_3 = x_1_3 - mean(x_1_3);
y_1_3 = y_1_3(1:frames_min);
y_1_3 = y_1_3 - mean(y_1_3);
x_2_3 = x_2_3(1:frames_min);
x_2_3 = x_2_3 - mean(x_2_3);
y_2_3 = y_2_3(1:frames_min);
y_2_3 = y_2_3 - mean(y_2_3);
x_3_3 = x_3_3(1:frames_min);
x_3_3 = x_3_3 - mean(x_3_3);
y_3_3 = y_3_3(1:frames_min);
y_3_3 = y_3_3 - mean(y_3_3);
XY_total = [x_1_3; y_1_3; x_2_3; y_2_3; x_3_3; y_3_3];
figure(1) % Plot the Raw Data Positional Data of Paint Can
subplot(3,1,1)
plot(1:frames_min, XY_total(2,:),1:frames_min, XY_total(1,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 3-Horizontal Displacement Case: Raw Positional Data of Paint Can, Cam 1");
legend("Y", "X")
subplot(3,1,2)
plot(1:frames_min, XY_total(4,:),1:frames_min, XY_total(3,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 3-Horizontal Displacement Case: Raw Positional Data of Paint Can, Cam 2");
legend("Y", "X")
subplot(3,1,3)
plot(1:frames_min, XY_total(6,:),1:frames_min, XY_total(5,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 3-Horizontal Displacement Case: Raw Positional Data of Paint Can, Cam 3")
legend("Y", "X")
% PCA on test 3
[U,S,V] = svd(XY_total/sqrt(frames_min-1), 'econ');
sig = diag(S); % Calculating the energy of the truncations
figure(2)
plot(sig.^2/sum(sig.^2),'ko','Linewidth',2)
title("Test 3-Horizontal Displacement Case: Energy Captured by Each Nonzero Singular Value");
xlabel("Singular Values"); ylabel("Energy");
axis([0 6 0 1])
set(gca,'Fontsize',14,'Xtick',0:1:6)
% Create Projection on first three Principal Components
figure(3)
subplot(2,1,1)
XY_proj = V';
plot(1:frames_min, XY_proj(1:2,:),'Linewidth',1.5)
title("Test 3-Horizontal Displacement Case: Movements on Principal Components");
ylabel("Displacement"); xlabel("Time (frames)");
legend("1st Principal Component", "2nd Principal Component",'location','southeast');
set(gca,'Fontsize',14)
subplot(2,1,2)
plot(1:frames_min, XY_proj(3:4,:),'Linewidth',1.5)
title("Test 3-Horizontal Displacement Case: Movements on Principal Components");
ylabel("Displacement"); xlabel("Time (frames)");
legend("3rd Principal Component","4th Principal Component",'location','southeast');
set(gca,'Fontsize',14)
%% Test 4: Horizontal Displacement and Rotation.
clear all; close all; clc;
load cam1_4.mat; % -- Load Movie cam1_4 (Horizontal Displacement and Rotation from Cam 1)
[height1_4 width1_4 rgb num_frames1_4] = size(vidFrames1_4);
x_1_4 = [];
y_1_4 = [];
for j = 1:num_frames1_4 % -- Watch Movie
X = rgb2gray(vidFrames1_4(:,:,:,j)); % convert to grey scale
% imshow(X); drawnow
X1 = double(X); % converted to double precision for mathematical processing
X1(:,1:320) = 0; % crop the video to the target region
X1(:,460:end) = 0;
X1(1:225,:) = 0;
X1(400:end,:) = 0;
[M,I] = max(X1(:));
[y,x] = ind2sub(size(X1),I);
x_1_4 = [x_1_4 x]; % record the bright spot position in each frame
y_1_4 = [y_1_4 y];
end
load cam2_4.mat; % -- Load Movie cam2_4 (Horizontal Displacement and Rotation from Cam 2)
[height2_4 width2_4 rgb num_frames2_4] = size(vidFrames2_4);
x_2_4 = [];
y_2_4 = [];
for j=1:num_frames2_4 % -- Watch Movie
X = rgb2gray(vidFrames2_4(:,:,:,j)); % convert to grey scale
% imshow(X); drawnow
X2 = double(X); % converted to double precision for mathematical processing
X(:,1:220) = 0; % crop the video to the target region
X(:,400:end) = 0;
X(1:80,:) = 0;
X(380:end,:) = 0;
[M,I] = max(X2(:));
[y,x] = ind2sub(size(X2),I);
x_2_4 = [x_2_4 x]; % record the bright spot position in each frame
y_2_4 = [y_2_4 y];
end
load cam3_4.mat; % -- Load Movie cam3_4 (Horizontal Displacement and Rotation from Cam 3)
[height3_4 width3_4 rgb num_frames3_4] = size(vidFrames3_4);
x_3_4 = [];
y_3_4 = [];
for j=1:num_frames3_4 % -- Watch Movie
X = rgb2gray(vidFrames3_4(:,:,:,j)); % convert to grey scale
% imshow(X); drawnow
X3 = double(X); % converted to double precision for mathematical processing
X3(:,1:300) = 0; % crop the video to the target region
X3(:,480:end) = 0;
X3(1:140,:) = 0;
X3(280:end,:) = 0;
[M,I] = max(X3(:));
[y,x] = ind2sub(size(X3),I);
x_3_4 = [x_3_4 x]; % record the bright spot position in each frame
y_3_4 = [y_3_4 y];
end
% make all 3 cams start at same relative position
[Min,Ind] = min(y_1_4(1:25));
x_1_4 = x_1_4(Ind:end);
y_1_4 = y_1_4(Ind:end);
[Min,Ind] = min(y_2_4(1:25));
x_2_4 = x_2_4(Ind:end);
y_2_4 = y_2_4(Ind:end);
[Min,Ind] = min(x_3_4(1:25));
x_3_4 = x_3_4(Ind:end);
y_3_4 = y_3_4(Ind:end);
% trim each movie clip down to the same size
frames_min = min([length(x_1_4),length(x_2_4),length(x_3_4)]);
x_1_4 = x_1_4(1:frames_min);
x_1_4 = x_1_4 - mean(x_1_4);
y_1_4 = y_1_4(1:frames_min);
y_1_4 = y_1_4 - mean(y_1_4);
x_2_4 = x_2_4(1:frames_min);
x_2_4 = x_2_4 - mean(x_2_4);
y_2_4 = y_2_4(1:frames_min);
y_2_4 = y_2_4 - mean(y_2_4);
x_3_4 = x_3_4(1:frames_min);
x_3_4 = x_3_4 - mean(x_3_4);
y_3_4 = y_3_4(1:frames_min);
y_3_4 = y_3_4 - mean(y_3_4);
% Deal with deal with outliers in cam2 -the frames bright light not appearing in some frames
for i = 2:frames_min
if abs(x_2_4(1,i) - x_2_4(1,i-1)) > 50
x_2_4(1,i) = x_2_4(1,i-1);
y_2_4(1,i) = y_2_4(1,i-1);
end
end
XY_total = [x_1_4; y_1_4; x_2_4; y_2_4; x_3_4; y_3_4];
figure(1) % Plot the Raw Data Positional Data of Paint Can
subplot(3,1,1)
plot(1:frames_min, XY_total(2,:),1:frames_min, XY_total(1,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 4-Horizontal Displacement and Rotation: Raw Positional Data of Paint Can, Cam 1");
legend("Y", "X")
subplot(3,1,2)
plot(1:frames_min, XY_total(4,:),1:frames_min, XY_total(3,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 4-Horizontal Displacement and Rotation: Raw Positional Data of Paint Can, Cam 2");
legend("Y", "X")
subplot(3,1,3)
plot(1:frames_min, XY_total(6,:),1:frames_min, XY_total(5,:), 'Linewidth', 2)
ylabel("Displacement (pixels)"); xlabel("Time (frames)");
title("Test 4-Horizontal Displacement and Rotation: Raw Positional Data of Paint Can, Cam 3")
legend("Y", "X")
% PCA on test 3
[U,S,V] = svd(XY_total/sqrt(frames_min-1), 'econ');
sig = diag(S); % Calculating the energy of the truncations
figure(2)
plot(sig.^2/sum(sig.^2),'ko','Linewidth',2)
title("Test 4-Horizontal Displacement and Rotation: Energy Captured by Each Nonzero Singular Value");
xlabel("Singular Values"); ylabel("Energy");
axis([0 6 0 1])
set(gca,'Fontsize',14,'Xtick',0:1:6)
% Create Projection on first three Principal Components
figure(3)
subplot(2,1,1)
XY_proj = V';
plot(1:frames_min, XY_proj(1:2,:),'Linewidth',1.5)
title("Test 4-Horizontal Displacement and Rotation: Movements on Principal Components");
ylabel("Displacement"); xlabel("Time (frames)");
legend("1st Principal Component", "2nd Principal Component", ...
"3rd Principal Component",'location','southeast');
set(gca,'Fontsize',14)
subplot(2,1,2)
plot(1:frames_min, XY_proj(3:4,:),'Linewidth',1.5)
title("Test 4-Horizontal Displacement and Rotation: Movements on Principal Components");
ylabel("Displacement"); xlabel("Time (frames)");
legend("3rd Principal Component","4th Principal Component",'location','southeast');
set(gca,'Fontsize',14)