-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA8Part2.m
214 lines (177 loc) · 7.56 KB
/
PA8Part2.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
% Amarachi Akunna Onyekachi
% 06/15/2020
% PA8Part2.m
%
% Records video of LEDs on moving joints and calculates their centroid
clc
clear
close all
warning('off')
%% Declarations
% Video information
vidFile = 'Akunna_AmaraLED.mp4'; % with extension
filenameString1 = 'centroid_joints_akunnaPart2.avi'; % Output video name
filenameString2 = 'joined_joints_akunnaPart2.avi'; % Output video name
desiredFrameRate = 30;
% Load video
vid = VideoReader(vidFile); % reads in .mp4 file
frameRate = vid.FrameRate;
vid_duration = vid.Duration;
total_frames = frameRate * vid_duration;
vid_width = vid.Width;
vid_height = vid.Height;
vid_bits = vid.BitsPerPixel;
frameStart = floor(3.316*frameRate);
frameStop = floor(8.241*frameRate);
% Cropping rectangle dimension
crop_x_start = 250; % x-coordinate to begin cropping image
crop_y_start = 290; % y-coordinate to begin cropping image
crop_x_size = 1670; % shifts crop_x_start by crop_x_size along x-axis
crop_y_size = 500; % shifts crop_y_start by crop_y_size along y-axis
% Threshold
rLED_Th_range = [120,255;0,82;50,160]; % LED color red threshold range
gLED_Th_range = [0,90;90,255;90,200]; % LED color green threshold range
bLED_Th_range = [0,61;15,210;200,255]; % LED color blue threshold range
%% Threshold Video
n = 1; % place holder for centroid index
vidObj = VideoWriter(filenameString1); % include extension in string
vidObj.FrameRate = desiredFrameRate; % set frame rate (e.g., 20)
open(vidObj) % opens video for recording
% Step through each frame
for k = frameStart:frameStop
frameSlice = read(vid,k); % loads current frame into frameSlice
% Crop image
frameSlice_crop = imcrop(frameSlice,...
[crop_x_start,crop_y_start,crop_x_size,crop_y_size]);
% Creates separate binary(black & white) image array for each LED color
% using different RGB color ranges for the colors we want to select
% and later combines them to one array using logical or
img_arr_bw_r = (frameSlice_crop(:,:,1) > rLED_Th_range(1,1) &...
frameSlice_crop(:,:,1) < rLED_Th_range(1,2) &...
frameSlice_crop(:,:,2) > rLED_Th_range(2,1) &...
frameSlice_crop(:,:,2) < rLED_Th_range(2,2) &...
frameSlice_crop(:,:,3) > rLED_Th_range(3,1) &...
frameSlice_crop(:,:,3) < rLED_Th_range(3,2));
img_arr_bw_g = (frameSlice_crop(:,:,1) > gLED_Th_range(1,1) &...
frameSlice_crop(:,:,1) < gLED_Th_range(1,2) &...
frameSlice_crop(:,:,2) > gLED_Th_range(2,1) &...
frameSlice_crop(:,:,2) < gLED_Th_range(2,2) &...
frameSlice_crop(:,:,3) > gLED_Th_range(3,1) &...
frameSlice_crop(:,:,3) < gLED_Th_range(3,2));
img_arr_bw_b = (frameSlice_crop(:,:,1) > bLED_Th_range(1,1) &...
frameSlice_crop(:,:,1) < bLED_Th_range(1,2) &...
frameSlice_crop(:,:,2) > bLED_Th_range(2,1) &...
frameSlice_crop(:,:,2) < bLED_Th_range(2,2) &...
frameSlice_crop(:,:,3) > bLED_Th_range(3,1) &...
frameSlice_crop(:,:,3) < bLED_Th_range(3,2));
% Combines arrays showing the position of each LED color into one
img_arr_bw = img_arr_bw_r | img_arr_bw_g | img_arr_bw_b;
% Uses custom function centroid to return row and col of centroid
% of each black and white image array
[cent_r(2,n),cent_r(1,n)] = Centroid(img_arr_bw_r);
[cent_g(2,n),cent_g(1,n)] = Centroid(img_arr_bw_g);
[cent_b(2,n),cent_b(1,n)] = Centroid(img_arr_bw_b);
% Display the thresholded image and plot centroid movement dynamically
% Make sure image and plot are same size, and no change in axes from
% plot to plot
centroid_plot_Xmin = 0;
centroid_plot_Xmax = crop_x_size;
centroid_plot_Ymin = 0;
centroid_plot_Ymax = crop_y_size;
figure(1)
img_reg = subplot(3,1,1);
imshow(frameSlice_crop)
title('Color Cropped Image')
img_sp = subplot(3,1,2); % binary image subplot
imshow(img_arr_bw)
title('Combined Threshold Image')
% had to include this if function because the red LED was not bright
% enough in some frames, so the if function assigns the previous
% calculated centroid as the new one
if cent_r(1,n) == 0
cent_r(1,n) = cent_r(1,n-1);
end
if cent_r(2,n) == 0
cent_r(2,n) = cent_r(2,n-1);
end
cent_sp = subplot(3,1,3); % centroid subplot
plot(cent_r(1,:),cent_r(2,:),'r','LineWidth',1.5)
hold on
plot(cent_g(1,:),cent_g(2,:),'g','LineWidth',1.5)
hold on
plot(cent_b(1,:),cent_b(2,:),'cy','LineWidth',1.5)
hold on
plot(cent_r(1,n),cent_r(2,n),'x','MarkerFaceColor','red',...
'LineWidth',2,"Color",'r');
hold on
plot(cent_g(1,n),cent_g(2,n),'x','MarkerFaceColor','green',...
'LineWidth',2,"Color",'g');
hold on
plot(cent_b(1,n),cent_b(2,n),'x','MarkerFaceColor','cyan',...
'LineWidth',2,"Color",'cy');
hold off
title('Joint Centroids')
axis([centroid_plot_Xmin centroid_plot_Xmax...
centroid_plot_Ymin centroid_plot_Ymax])
pbaspect([crop_x_size crop_y_size 1])
set(gca,'XAxisLocation','top','YAxisLocation','left','ydir','reverse')
set(gca,'xtick',[])
set(gca,'xticklabel',[])
set(gca,'ytick',[])
set(gca,'yticklabel',[])
drawnow
currentFrame = getframe(gcf); % saves plot in variable
writeVideo(vidObj,currentFrame); % writes frame to video
n = n + 1;
end
close(vidObj);
vidObj = VideoWriter(filenameString2); % include extension in string
vidObj.FrameRate = desiredFrameRate; % set frame rate (e.g., 20)
open(vidObj) % opens video for recording
n = 1; % resets place holder for centroid index to 1
for k = frameStart:frameStop
frameSlice = read(vid,k); % loads current frame into frameSlice
% Crop image
frameSlice_crop = imcrop(frameSlice,...
[crop_x_start,crop_y_start,crop_x_size,crop_y_size]);
% The centroids do not have to be calculated again because we have them
% saved to different centroid variables, so we can loop through them
% using the reset index n
% Display the thresholded image and plot centroid movement dynamically
% Make sure image and plot are same size, and no change in axes from
% plot to plot
centroid_plot_Xmin = 0;
centroid_plot_Xmax = crop_x_size;
centroid_plot_Ymin = 0;
centroid_plot_Ymax = crop_y_size;
figure(2)
img_reg = subplot(2,1,1);
imshow(frameSlice_crop)
title('Color Cropped Image')
cent_sp = subplot(2,1,2); % centroid joined subplot
plot([cent_r(1,n) cent_b(1,n)], [cent_r(2,n) cent_b(2,n)],'Color','r')
hold on
plot([cent_b(1,n) cent_g(1,n)], [cent_b(2,n) cent_g(2,n)],'Color','g')
hold on
plot(cent_r(1,n),cent_r(2,n),'ro','MarkerFaceColor','red');
hold on
plot(cent_b(1,n),cent_b(2,n),'cyo','MarkerFaceColor','cyan');
hold on
plot(cent_g(1,n),cent_g(2,n),'go','MarkerFaceColor','green');
hold off
title('Joined Joint Centroids')
axis([centroid_plot_Xmin centroid_plot_Xmax...
centroid_plot_Ymin centroid_plot_Ymax])
pbaspect([crop_x_size crop_y_size 1])
set(gca,'XAxisLocation','top','YAxisLocation','left','ydir','reverse')
set(gca,'xtick',[])
set(gca,'xticklabel',[])
set(gca,'ytick',[])
set(gca,'yticklabel',[])
drawnow
currentFrame = getframe(gcf); % saves plot in variable
writeVideo(vidObj,currentFrame); % writes frame to video
n = n + 1;
end
close(vidObj) % closes video