-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoGUI.m
656 lines (489 loc) · 17.1 KB
/
videoGUI.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
function [x,y] = videoGUI(vid_path,v,frames,imInvert,acqMode,varargin)
% Interactive acquisition of coordinate points from a video
%
%% Parse inputs
if nargin< 5
acqMode = 'simple';
end
if strcmp(acqMode,'simple')
% roi radius
if length(varargin)>0
r = varargin{1};
else
r = 0;
end
% Marker color
if length(varargin)>1
mClr = varargin{2};
else
mClr = [0 1 0];
end
% Starting values
if length(varargin)>3
xStart = varargin{3};
yStart = varargin{4};
else
xStart = nan(length(frames),1);
yStart = nan(length(frames),1);
end
else
error('Do not recognize acqMode');
end
%% Default parameters
% Container of handle data
H = [];
% Adjust window docking
set(0,'DefaultFigureWindowStyle','normal')
% Initial frame
im0 = getFrame(vid_path,v,frames(1),imInvert,'gray');
% Alter default
if r == 0
r = min(size(im0))/5;
end
% Apply mask
%im0 = applyMask(im0,iC.xTank,iC.yTank);
% Set starting roi
roi = [size(im0,2)/2-r(1)/2 size(im0,1)/2-r(1)/2 2*r 2*r];
% Display options
disp(' ')
disp('COMMANDS ------------------------------------------------ ')
disp(' left click : select position in current frame')
disp(' delete : delete current position')
disp(' right arrow : advance one frame')
disp(' left arrow : go back one frame')
disp(' up arrow : advance five frames')
disp(' down arrow : go back five frames')
disp(' 1 - 9 : jump to relative position in video segment')
disp(' + : zoom in')
disp(' - : zoom out')
disp(' d : delete all points after current in interval')
disp(' a : delete all points in interval')
disp(' q : quit interaction mode')
disp(' ')
% Create figure window
[hFig, hAxes] = createFigureAndAxes;
% Add data to figure
[hFig, hAxes] = putData(hFig, hAxes);
% Wait for completion of interactive mode
waitfor(hFig)
if isempty(H)
return
else
x = H.x;
y = H.y;
end
function [hFig, hAxes] = createFigureAndAxes
% Close figure opened by last run
figTag = 'CVST_VideoOnAxis_9804532';
close(findobj('tag',figTag));
AR = size(im0,2)/size(im0,1);
%AR = range(iC.yTank)/range(iC.xTank);
AR_roi = roi(4)/roi(3);
% Relative width of figure window
rel_width = 0.4;
% Create new figure
hFig = figure('numbertitle', 'off', ...
'name', 'Analysis GUI', ...
'menubar','none', ...
'toolbar','none', ...
'resize', 'on', ...
'tag',figTag, ...
'renderer','painters',...
'Units','pixels');
scrsize = get(0,'screensize');
set(hFig,'Position',scrsize);
%set(hFig,'Units','normalized');
% Size of width and height of 2 windows
win_size = [scrsize(3)*rel_width scrsize(3)*rel_width/AR];
% Spacing between windows
spacer = 10;
% Relative position of main panel
Lrect(3) = win_size(1);
Lrect(4) = win_size(2);
Lrect(1) = scrsize(3)/2 - Lrect(3) - spacer/2;
Lrect(2) = scrsize(4)/2 - Lrect(4)/2;
Ltitle(3) = Lrect(3);
Ltitle(4) = 20;
Ltitle(1) = Lrect(1);
Ltitle(2) = Lrect(2) + Lrect(4);
% Relative position of small panel
Rrect(3) = win_size(1);
Rrect(4) = win_size(1)/AR_roi;
Rrect(1) = scrsize(3)/2 + spacer/2;
Rrect(2) = scrsize(4)/2 - Rrect(4)/2;
Rtitle(3) = Rrect(3);
Rtitle(4) = 20;
Rtitle(1) = Rrect(1);
Rtitle(2) = Rrect(2) + Rrect(4) ;
% Get figure position
%fPos = get(hFig,'Position');
% Create axes and titles
hAxes.axis1 = createPanelAxisTitle(hFig,...
Lrect, Ltitle,'Full frame', 'title1'); % [X Y W H]
hAxes.axis2 = createPanelAxisTitle(hFig, ...
Rrect, Rtitle, 'ROI', 'title2');
% Keystrokes
set(hFig, 'WindowButtonDownFcn', {@butDown, hFig, hAxes});
% Set callback for button press
set(hFig, 'WindowKeyPressFcn', {@keyPress, hFig, hAxes});
end
%% Deposite data into figure
function [hFig, hAxes] = putData(hFig, hAxes)
% Store inital values into axis1
H.roi = roi;
H.iFrame = 1;
H.frames = frames;
H.vid_path = vid_path;
H.v = v;
H.clr = mClr;
H.x = xStart;
H.y = yStart;
H.imInvert = imInvert;
% Store coordinate data
guidata(hAxes.axis1, H);
% Render images in figure
update_fig(hFig, hAxes);
end
%% Create Axis and Title
% Axis is created on uipanel container object. This allows more control
% over the layout of the GUI. Video title is created using uicontrol.
function hAxis = createPanelAxisTitle(hFig, posWin, posTitle, axisTitle, textTag)
% Create panel
%hPanel = uipanel('parent',hFig,'Position',pos,'Units','Normalized');
hPanel = uipanel('parent',hFig,'Units','pixels','Visible','on');
% Set position
hPanel.Position = posWin;
hPanel.Units = 'pixels';
% Create axis
hAxis = axes('Parent',hPanel,'Units','pixels');
hAxis.Position = [0 0 posWin(3:4)];
%hAxis.Position = [-min(iC.xTank)/2 min(iC.yTank)/2 range(iC.xTank) range(iC.yTank)];
hAxis.XTick = [];
hAxis.YTick = [];
hAxis.XColor = [1 1 1];
hAxis.YColor = [1 1 1];
% Revert units to pixels
hAxis.Units = 'pixels';
% Set video title using uicontrol. uicontrol is used so that text
% can be positioned in the context of the figure, not the axis.
titlePos = posTitle;
hUI = uicontrol('style','text',...
'String', axisTitle,...
'Units','pixels',...
'Parent',hFig,...
'Position', titlePos,...
'BackgroundColor',hFig.Color, ...
'HorizontalAlignment','left',...
'Tag',textTag);
% Revert units to pixels
%hUI.Units = 'pixels';
end
end
%% Small helper functions
function showFrameOnAxis(hAxis, frame, zoomlevel)
% This helper function displays a frame of video on a user-defined axis.
frame = convertToUint8RGB(frame);
try
hChild = get(hAxis, 'Children');
catch %#ok<CTCH>
return; % hAxis does not exist; nothing to draw
end
isFirstTime = isempty(hChild);
if isFirstTime
hIm = displayImage(hAxis, frame);
zoom(hAxis,zoomlevel)
if 0
addScrollPanel(hAxis, hIm);
end
else
hIm = hChild(end);
try
set(hIm,'cdata',frame);
drawnow;
catch %#ok<CTCH>
% figure closed
return;
end
end
end
function frame = convertToUint8RGB(frame)
% Convert input data type to uint8
if ~isa(class(frame), 'uint8')
frame = im2uint8(frame);
end
% If the input is grayscale, turn it into an RGB image
if (size(frame,3) ~= 3) % must be 2d
frame = cat(3,frame, frame, frame);
end
end
function hIm = displayImage(hAxis, frame)
% Display image in the specified axis
frameSize = size(frame);
xdata = [1 frameSize(2)];
ydata = [1 frameSize(1)];
cdata = frame;
cdatamapping = 'direct';
hIm = image(xdata,ydata,cdata, ...
'BusyAction', 'cancel', ...
'Parent', hAxis, ...
'CDataMapping', cdatamapping, ...
'Interruptible', 'off');
set(hAxis, ...
'YDir','reverse',...
'TickDir', 'out', ...
'XGrid', 'off', ...
'YGrid', 'off', ...
'PlotBoxAspectRatioMode', 'auto', ...
'Visible', 'off');
end
% function [x, y] = full_to_roi(xVal,yVal,roi)
% % Converts from full frame coordinates to ROI coords
%
% % Normalize to size of rendered frame
% xNorm = (xVal-rect(1))/roi(3);
% yNorm = (yVal-rect(2))/roi(4);
%
% % Transform into frame coords
% x = xNorm .* range(hAxes.axis2.XLim) + hAxes.axis2.XLim(1);
% y = yNorm .* range(hAxes.axis2.YLim) + hAxes.axis2.YLim(1);
% end
%% Key press callback
function keyPress(fig, key, hFig, hAxes)
closefig = 0;
% Load data
H = guidata(hAxes.axis2);
% QUIT ('q')
if strcmp(key.Key,'Q') || strcmp(key.Key,'q')
assignin('caller','H',H);
closefig = 1;
% DELETE
elseif strcmp(key.Key,'backspace')
% Delete data
H.x(H.iFrame) = nan;
H.y(H.iFrame) = nan;
% Store coordinate data
guidata(hAxes.axis1, H);
% Update figure
update_fig(hFig, hAxes)
% PLUS (zoom in)
elseif strcmp(key.Key,'hyphen') || strcmp(key.Key,'minus')
xCntr = H.roi(1) + H.roi(3)/2;
yCntr = H.roi(2) + H.roi(4)/2;
% Enlarge roi
H.roi(3) = 1.5*H.roi(3);
H.roi(4) = 1.5*H.roi(4);
H.roi(1) = xCntr - H.roi(3)/2;
H.roi(2) = yCntr - H.roi(4)/2;
% Store coordinate data
guidata(hAxes.axis1, H);
% Update figure
update_fig(hFig, hAxes)
% PLUS (zoom out)
elseif strcmp(key.Key,'equal') || strcmp(key.Key,'plus')
xCntr = H.roi(1) + H.roi(3)/2;
yCntr = H.roi(2) + H.roi(4)/2;
% Enlarge roi
H.roi(3) = H.roi(3)/1.5;
H.roi(4) = H.roi(4)/1.5;
H.roi(1) = xCntr - H.roi(3)/2;
H.roi(2) = yCntr - H.roi(4)/2;
% Store coordinate data
guidata(hAxes.axis1, H);
% Update figure
update_fig(hFig, hAxes)
elseif strcmp(key.Key,'d') || strcmp(key.Key,'D')
but = questdlg(['You want to delete all points after this frame?'],...
'ALERT!','Yes','No','Yes');
if strcmp(but,'Yes')
H.x((H.iFrame+1):end) = nan;
H.y((H.iFrame+1):end) = nan;
end
% Store coordinate data
guidata(hAxes.axis1, H);
% Update figure
update_fig(hFig, hAxes)
elseif strcmp(key.Key,'a') || strcmp(key.Key,'A')
but = questdlg(['You want to delete all points in this interval?'],...
'ALERT!','Yes','No','Yes');
if strcmp(but,'Yes')
H.x(:) = nan;
H.y(:) = nan;
end
% Store coordinate data
guidata(hAxes.axis1, H);
% Update figure
update_fig(hFig, hAxes)
% The following involve changing frame number
else
newIndex = [];
% Index of current frame
% iFrame = find(H.cFrame==H.frames,1,'first');
% RIGHT ARROW/SPACE
if strcmp(key.Key,'rightarrow') || strcmp(key.Key,'space')
if H.iFrame==length(H.frames)
beep
else
% Advance frame
newIndex = H.iFrame+1;
end
% LEFT ARROW
elseif strcmp(key.Key,'leftarrow')
if H.iFrame==1
beep
else
% Reverse frame
newIndex = H.iFrame-1;
end
% UP ARROW
elseif strcmp(key.Key,'uparrow')
if H.iFrame+5>length(H.frames)
beep
else
% Advance frame
newIndex = H.iFrame+5;
end
% DOWN ARROW
elseif strcmp(key.Key,'downarrow')
if H.iFrame-5<1
beep
else
% Reverse frame
newIndex = H.iFrame-5;
end
% NUMBER
elseif length(key.Key)==1 && (sum(key.Key==num2str([1:9]))==1)
% Requested relative number
req_num = str2num(key.Key);
% Start
if req_num == 1
newIndex = 1;
% End
elseif req_num==9
newIndex = length(H.frames);
% Something between
else
% Set new frame
newIndex = round((req_num/10)*length(H.frames));
end
end
% If new newframe
if ~isempty(newIndex)
% Update frame number
H.iFrame = newIndex;
% Store coordinate data
guidata(hAxes.axis1, H);
end
end
if closefig==1
close(hFig)
else
% Update figure
update_fig(hFig, hAxes)
end
end
%% Button down callback
function butDown(fig, key, hFig, hAxes)
% Load data
H = guidata(hAxes.axis2);
% Collect current coordinate
C2 = get(hAxes.axis2, 'CurrentPoint');
C1 = get(hAxes.axis1, 'CurrentPoint');
hold on
% If in full frame box . . .
if (C1(1,1)>=0) && (C1(1,1)<=hAxes.axis1.XLim(2)) && ...
(C1(1,2)>=0) && (C1(1,2)<=hAxes.axis1.YLim(2))
% Update roi
H.roi(1) = C1(1,1)-H.roi(3)/2;
H.roi(2) = C1(1,2)-H.roi(4)/2;
% If in roi box . . .
elseif (C2(1,1)>=0) && (C2(1,1)<=hAxes.axis2.XLim(2)) && ...
(C2(1,2)>=0) && (C2(1,2)<=hAxes.axis2.YLim(2))
% Current coordinate
xCurr = H.roi(1) + C2(1,1);
yCurr = H.roi(2) + C2(1,2);
% Store data
H.x(H.iFrame,1) = xCurr;
H.y(H.iFrame,1) = yCurr;
else
set(gcf,'Pointer','arrow')
end
% Store coordinate data
guidata(hAxes.axis1, H);
% Update figure
update_fig(hFig, hAxes);
end
%% Update GUI
function update_fig(hFig, hAxes)
% Activate figure window
figure(hFig)
% Load data
H = guidata(hAxes.axis1);
% List frame number in title 1
obj = findobj('tag','title1');
t_str = ['Full Frame : Frame ' num2str(H.frames(H.iFrame)) ];
set(obj,'String',t_str);
% List mode in title 2
obj = findobj('tag','title2');
set(obj,'String','ROI');
% Index for current frame
% iFrame = find(H.cFrame==H.frames,1,'first');
% Current point
xCurr = H.x(H.iFrame);
yCurr = H.y(H.iFrame);
% All points
xAll = H.x(~isnan(H.x));
yAll = H.y(~isnan(H.y));
% Current frame
cFrame = H.frames(H.iFrame);
% Read input video frame
frame = getFrame(H.vid_path,H.v,cFrame,H.imInvert,'gray');
% Display full video frame
delete(hAxes.axis1.Children)
showFrameOnAxis(hAxes.axis1, frame, 0);
% % Get window dimensions (full frame FOR)
% winWidth1 = size(hAxes.axis1.Children(end).CData,2);
% winHeight1 = size(hAxes.axis1.Children(end).CData,1);
%
%
% Get coordinates for the ROI (Full frame FOR)
%rect = get_val;
% Center of ROI (Full frame FOR)
xCntr = H.roi(1) + H.roi(3)/2;
yCntr = H.roi(2) + H.roi(4)/2;
% Hold on Axis1
set(hAxes.axis1,'NextPlot','Add')
% Set H.roi square on full frame
x1 = H.roi(1); x2 = H.roi(1)+H.roi(3);
y1 = H.roi(2); y2 = H.roi(2)+H.roi(4);
plot([x1 x2 x2 x1 x1], [y1 y1 y2 y2 y1], 'w-',...
mean([x1 x2]).*[1 1],[y1 y2], 'w:', ...
[x1 x2], mean([y1 y2]).*[1 1], 'w:',...
'Parent',hAxes.axis1);
% Hold off Axis1
set(hAxes.axis1,'NextPlot','Replace');
% Get cropped image
im2 = imcrop(frame, H.roi);
% Display cropped image
delete(hAxes.axis2.Children);
showFrameOnAxis(hAxes.axis2, im2, 0);
% Limits for x and y axes
xL = hAxes.axis2.XLim;
yL = hAxes.axis2.YLim;
% Hold on Axis 2
set(hAxes.axis2,'NextPlot','Add');
% Add cross hairs
% h = plot([mean(xL) mean(xL)],yL,'w:',xL,[mean(yL) mean(yL)],'w:',...
% 'Parent',hAxes.axis2);
h2 = line(xAll-H.roi(1),yAll-H.roi(2),...
'Parent',hAxes.axis2,'Color',[H.clr 0.3],'LineWidth',3);
h2 = scatter(xCurr-H.roi(1),yCurr-H.roi(2),'Parent',hAxes.axis2,...
'MarkerEdgeColor',H.clr,'MarkerfaceColor','none','SizeData',150);
% h2 = plot(xCurr-H.roi(1),yCurr-H.roi(2),'+',...
% 'Parent',hAxes.axis2,'Color',H.clr);
% Hold off Axis 2
set(hAxes.axis2,'NextPlot','Replace');
% Activate figure
figure(hFig)
% Activate ROI panel
set(hAxes.axis2,'Selected','on')
end