-
Notifications
You must be signed in to change notification settings - Fork 4
/
QMUL_partC10.m
128 lines (109 loc) · 3.89 KB
/
QMUL_partC10.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
function [ boundedPicture ] = QMUL_partC10( vid, frame)
%
%QMUL_part11 Writes object details to file
% Calculates displacement details and writes to file
% called 'question10.txt'
%
% boundedPicture = QMUL_partB11(vidFrames, frame)
%
% INPUT
% vidFrames - Frames of the video
% frame - Frame to use
%
% OUTPUT
% boundedPicture - The picture of frame
%
% SOURCES NEEDED
% QMUL_partA5.m , QMUL_thresholding.m and QMUL_FloodFill.m
%open file
output = fopen('question10.txt','w');
fprintf(output,'Frame : %d\n', frame);
colourList(1,:) = [255 255 255]; %White
colourList(2,:) = [0 0 0]; %Black
colourList(3,:) = [255 0 0]; %Red
colourList(4,:) = [0 255 0]; %Green
colourList(5,:) = [0 0 255]; %Blue
colourList(6,:) = [255 140 0]; %Orange
colourList(7,:) = [160 32 240]; %Purple
colourList(8,:) = [255 105 180]; %Pink
colourList(9,:) = [255 255 0]; %Yellow
colourList(10,:) = [0 255 255]; %Cyan
colourList(11,:) = [139 69 19]; %Brown
colourList(12,:) = [148 0 211]; %Dark Violet
colourList(13,:) = [190 190 190]; %Gray
follow = zeros(1,1); %To know index of object in next frame
xdis = zeros(1); %movement along x
ydis = zeros(1); %movement along y
dis = zeros(1); %actual movement calculated using xdis and ydis
xydif = zeros(1,2); %keep linked differences
boundedPicture = vid(:,:,:,frame);
depth = 3;
vidFrame = vid(:,:,:,frame);
nextFrame = vid(:,:,:,frame+1);
background = QMUL_partA5(vid, 100, 'average');
tic;
%details of objects
[details centre avg] = QMUL_partC10i(vidFrame, background);
[details2 centre2 avg] = QMUL_partC10i(nextFrame, background);
for i=1:length(centre)
fprintf(output,'Object : %d\n', i);
follow(i,1) = i;
%find link
for j=1:length(centre2)
xdis(j) = centre(i,1) - centre2(j,2);
ydis(j) = centre(i,2) - centre2(j,2);
dis(j) = sqrt((xdis(j)^2) + (ydis(j)^2)); %Pythagora's
end
[minDis index] = min(dis);
follow(i,2) = index; %follow object
xydif(i,:) = [xdis(index) ydis(index)];
%write displacement details to file
fprintf(output,'\tX Displacement : %f\n', xdis(index));
fprintf(output,'\tY Displacement : %f\n', ydis(index));
fprintf(output,'\tTotal Displacement : %f\n', minDis);
%calculate and write direction
if xdis(index) < 0 && ydis(index) < 0
fprintf(output,'\tDirection : SW\n');
end
if xdis(index) < 0 && ydis(index) == 0
fprintf(output,'\tDirection : W\n');
end
if xdis(index) < 0 && ydis(index) > 0
fprintf(output,'\tDirection : NW\n');
end
if xdis(index) == 0 && ydis(index) > 0
fprintf(output,'\tDirection : N\n');
end
if xdis(index) > 0 && ydis(index) > 0
fprintf(output,'\tDirection : NE\n');
end
if xdis(index) > 0 && ydis(index) == 0
fprintf(output,'\tDirection : E\n');
end
if xdis(index) > 0 && ydis(index) < 0
fprintf(output,'\tDirection : SE\n');
end
if xdis(index) == 0 && ydis(index) < 0
fprintf(output,'\tDirection : S\n');
end
color = colourList(i,:);
maxX = details(i,1)+details(i,3);
minX = details(i,1);
maxY = details(i,2)+details(i,4);
minY = details(i,2);
for row=minX:maxX
for ch=1:depth
boundedPicture(row,maxY,ch) = color(ch);
boundedPicture(row,minY,ch) = color(ch);
end
end
for col=minY:maxY
for ch=1:depth
boundedPicture(minX,col,ch) = color(ch);
boundedPicture(maxX,col,ch) = color(ch);
end
end
end
imshow(boundedPicture);
toc
end