-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMovieSlider.m
1270 lines (1098 loc) · 56.4 KB
/
MovieSlider.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% MOVIESLIDER Embeddable panel for playback, scrolling, and contrast/color map adjustment of 3D movies.
%
% This software depends on the GUI Layout Toolbox:
% http://www.mathworks.com/matlabcentral/fileexchange/47982-gui-layout-toolbox
% Make sure to get the version appropriate for your version of Matlab (R2014b onwards vs. older).
%
% The class MovieSlider creates a panel (that can be embedded like any other uix object) that
% displays the current frame of the movie tensor provided by the user, which is assumed to be
% 3-dimensional where the first two dimensions comprise a frame and the third dimension indexes a
% particular frame in the movie. GUI and keyboard controls allow automatic playback/looping as well
% as live scrolling through frames. There is also a button to cycle through contrast settings, and
% an extended configuration panel where the user can fine-tune the playback speed, color map and
% mapping range.
%
% Example usage:
% shape = normpdf(-2.5:0.05:2.5, 0, 1);
% tensor = bsxfun(@times, bsxfun(@times, shape, shape'), reshape(shape,1,1,[]));
% movie = MovieSlider(tensor);
%
% Example of additional graphics drawn per frame:
% hMark = line('Parent', movie.axsMovie, 'XData', [], 'YData', [], 'LineWidth', 2, 'Color', [1 1 1], 'Marker', 'o', 'MarkerSize', 8);
% time = ( 0:size(tensor,3) )'; values = [size(tensor,2)/2 + 40*sin(time/5), size(tensor,1)/2 + 40*cos(time/8)];
% movie.addIllustrations(hMark, {'XData', 'YData'}, num2cell(values));
%
% Various member functions exists for programatic control of some features:
% show(...) : Sets the currently displayed movie (can also be done at
% construction time)
% setPlaybackFPS(playbackFPS) : Set playback rate in frames per second
% setTitle(string) : Sets the current title, or removes it if an empty string is
% provided; a cellstring can also be provided to show a different
% title per frame
% setFrame(index) : Sets the current frame to the given index
% setFocusReturn(fcnReturn) : Sets the function to call upon an 'escape' keypress
%
% Movies including illustrations can be converted to a standard output format (.avi, .mp4 etc.)
% using the function save().
%
% Author: Sue Ann Koay ([email protected])
% Acknowledgements:
% The GUI icons are creations of Olha Kozachenko (https://www.iconfinder.com/olgakozachenko)
% that were made public under the Creative Commons license.
%
classdef MovieSlider < uix.VBox
%------- Constants
properties (Constant)
DEFAULT_FPS = 30
FRAME_STEP = 10
MIN_NUMBINS = 20
CONTRAST_BINNING = 30
% CONTRAST_RANGE = 10.^(-7:-1)
% CONTRAST_RANGE = 5e-7 * (1:100:1e3).^2
% CONTRAST_RANGE = 6e-10 * (1:1e2:1e3).^3
CONTRAST_RANGE = 6e-9 * (1:10:100).^4
DIALOG_POSITION = [0.4 0.35 0.2 0.3]
DIALOG_MONITOR = MovieSlider.getMonitorBySize()
SELECT_COLOR = cat(3, 156, 230, 255)/255
GUI_COLOR = cat(3, 0.9400, 0.9400, 0.9400)
GUI_FONT = 11
GUI_BTNSIZE = 20
GUI_BUTTON = [100 30]
GUI_BORDER = 5
RANGE_COLOR = [255 251 222]/255
RANGE_BORDER = [255 217 0 ]/255
CONFIGURABLES = {'contrastIndex', 'pixelRange', 'playbackFPS', 'colors'}
COLORMAPS = { 'gray', 'parula', 'hot', 'cool' ...
, 'jet', 'hsv' ...
, 'spring', 'summer', 'autumn', 'winter' ...
, 'winter', 'bone', 'copper', 'pink' ...
};
CODE_PATH = parsePath(mfilename('fullpath'))
ICON_END = MovieSlider.loadIcon('end.png')
ICON_PAUSE = MovieSlider.loadIcon('pause.png')
ICON_PLAY = MovieSlider.loadIcon('play.png')
ICON_REFRESH_OFF = MovieSlider.loadIcon('refresh.png', MovieSlider.GUI_COLOR)
ICON_REFRESH_ON = MovieSlider.loadIcon('refresh.png', MovieSlider.SELECT_COLOR)
ICON_STOP = MovieSlider.loadIcon('stop.png')
ICON_BEGIN = MovieSlider.loadIcon('begin.png')
ICON_CONFIG = MovieSlider.loadIcon('config.png')
ICON_SEARCH = MovieSlider.loadIcon('search.png')
end
%------- Private data
properties (Access = protected)
figParent
figConfig
cntControl
txtDummy
txtInfo
btnConfig
btnPlay
btnRepeat
btnBegin
btnEnd
sldFrame
hZoom
fcnReturn
illusProperty = {}
illusValue = {}
lsnScroll
tmrPlayback
syncSiblings
end
%------- Public data
properties (SetAccess = protected)
movie
totalFrames
frameTitle = {}
pixelPDF = [0 0]
pixelCDF = [0 1]
pixelValue = [0 1]
pixelDomain = [-inf inf]
pixelRange = [0 1]
colors
doRepeat = false
currentFrame = 1
contrastIndex = 1
playbackFPS = MovieSlider.DEFAULT_FPS
axsMovie
imgMovie
titleMovie = gobjects(1)
illustration = gobjects(0)
imgX = []
imgY = []
end
properties
overlay = struct()
end
%________________________________________________________________________
methods
%----- Constructor
function obj = MovieSlider(parent, varargin)
%% Default arguments and parent class constructor
if nargin < 1
parent = figure;
elseif isnumeric(parent) || numel(parent) > 1 || ~ishghandle(parent)
varargin = [{parent}, varargin];
parent = figure;
end
[email protected]( 'Parent', parent );
%% Movie frame display
obj.axsMovie = axes ( 'Parent' , uicontainer('Parent', obj) ...
, 'ActivePositionProperty' , 'Position' ...
, 'Box' , 'on' ...
, 'Layer' , 'top' ...
, 'XTick' , [] ...
, 'YTick' , [] ...
, 'Color' , [0 0 0] ...
, 'XColor' , [0 0 0] ...
, 'YColor' , [0 0 0] ...
);
obj.imgMovie = image ( 'Parent' , obj.axsMovie ...
, 'CData' , [] ...
, 'CDataMapping' , 'scaled' ...
, 'UserData' , obj.currentFrame ...
);
hold(obj.axsMovie, 'on');
%% Movie info and controls
obj.cntControl = uix.HBox ( 'Parent' , obj ...
);
obj.txtDummy = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'text' ...
);
obj.txtInfo = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'edit' ...
, 'Enable' , 'inactive' ...
, 'FontSize' , MovieSlider.GUI_FONT - 3 ...
, 'Callback' , @obj.editSetFrame ...
, 'ButtonDownFcn' , @obj.enableSetFrame ...
, 'TooltipString' , 'Set frame' ...
);
obj.btnConfig = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'pushbutton' ...
, 'CData' , MovieSlider.ICON_CONFIG ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'Callback' , @obj.cycleContrast ...
, 'ButtonDownFcn' , @obj.configureDisplay ...
, 'TooltipString' , 'Cycle contrast / Configure (right-click)' ...
);
obj.btnPlay = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'pushbutton' ...
, 'CData' , MovieSlider.ICON_PLAY ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'Callback' , @obj.togglePlayback ...
, 'UserData' , false ...
, 'TooltipString' , sprintf('Play (%.4g FPS)', obj.playbackFPS) ...
);
obj.btnRepeat = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'togglebutton' ...
, 'CData' , MovieSlider.ICON_REFRESH_OFF ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'Callback' , @obj.setRepeat ...
, 'Value' , obj.doRepeat ...
, 'TooltipString' , 'Turn on repeat' ...
);
obj.btnBegin = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'pushbutton' ...
, 'CData' , MovieSlider.ICON_BEGIN ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'Callback' , {@obj.setFrame, 1} ...
, 'TooltipString' , 'First frame' ...
);
obj.sldFrame = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'slider' ...
, 'Min' , 1 ...
, 'Max' , 1 ...
, 'Value' , 1 ...
, 'SliderStep' , [1 1] ...
, 'Enable' , 'off' ...
);
obj.btnEnd = uicontrol ( 'Parent' , obj.cntControl ...
, 'Style' , 'pushbutton' ...
, 'CData' , MovieSlider.ICON_END ...
, 'BackgroundColor' , MovieSlider.GUI_COLOR ...
, 'TooltipString' , 'Last frame' ...
);
%% Configure formatting and sizes
axis( obj.axsMovie, 'image', 'ij');
set ( obj.cntControl, 'Widths' , [0 6 1 1 1 1 -1 1] * MovieSlider.GUI_BTNSIZE );
set ( obj , 'Heights' , [-1 MovieSlider.GUI_BTNSIZE] );
%% Listeners for frame scrolling
obj.lsnScroll = addlistener(obj.sldFrame, 'ContinuousValueChange', @obj.drawFrame);
%% Callback for mouseover info display and keyboard commands
obj.figParent = findParent(parent, 'figure');
movieSliders = get(obj.figParent, 'UserData');
if isempty(movieSliders)
movieSliders = obj;
else
movieSliders(end+1) = obj;
end
set ( obj.figParent ...
, 'UserData' , movieSliders ...
, 'WindowButtonDownFcn' , @MovieSlider.startZooming ...
, 'WindowButtonMotionFcn' , @MovieSlider.updatePixelInfo ...
, 'WindowKeyPressFcn' , @MovieSlider.keyboardControl ...
);
%% Playback timer
obj.tmrPlayback = timer ( 'TimerFcn' , @obj.playMovie ...
, 'ExecutionMode' ,'fixedRate' ...
, 'BusyMode' ,'drop' ...
);
%% Shortcut to load a given movie at construction time
if ~isempty(varargin)
obj.show(varargin{:});
end
MovieSlider.registry(obj);
end
%----- Destructor
function delete(obj)
if ~isempty(obj.tmrPlayback)
stop(obj.tmrPlayback);
delete(obj.tmrPlayback);
end
[email protected](obj);
end
%----- Retrieves the current configuration as can be passed to show()
function config = configuration(obj)
config = {obj.colors, obj.pixelDomain, obj.pixelRange, obj.contrastIndex};
end
%----- Sets the currently displayed movie
function show(obj, movie, colors, pixelDomain, contrast, contrastIndex)
%% Default arguments
if iscell(movie)
xCoord = movie{1};
yCoord = movie{2};
movie = movie{3};
else
xCoord = [];
yCoord = [];
end
if nargin < 3
colors = [];
end
if nargin < 4 || isempty(pixelDomain)
if min(movie(:)) == 0
pixelDomain = [0, inf];
else
pixelDomain = [-inf, inf];
end
end
if nargin < 5 || isempty(contrast)
contrast = 1;
end
if nargin > 5 % overridden by contrast, but set anyway for continuity
obj.contrastIndex = contrastIndex;
end
%% Movie properties
obj.movie = movie;
obj.totalFrames = size(obj.movie, max(3,ndims(obj.movie)));
%% Contrast determination only for 3D movies (as opposed to directly provided RGB)
if ndims(obj.movie) < 4
if obj.totalFrames >= MovieSlider.MIN_NUMBINS * MovieSlider.CONTRAST_BINNING
binnedMovie = rebin(obj.movie, MovieSlider.CONTRAST_BINNING, 3, @mean, 'omitnan');
else
binnedMovie = obj.movie;
end
if islogical(contrast) && numel(contrast) == size(movie,1)*size(movie,2)
imgMask = frameMask(size(binnedMovie), contrast, 1);
[obj.pixelPDF, edges] = histcounts(binnedMovie(imgMask), 'Normalization', 'prob');
if nargin > 5
contrast = contrastIndex;
else
contrast = 1;
end
else
[obj.pixelPDF, edges] = histcounts(binnedMovie, 'Normalization', 'prob');
end
if numel(obj.pixelPDF) < 2
obj.pixelPDF = [0 1];
obj.pixelValue = [0 1];
else
obj.pixelValue = (edges(1:end-1) + edges(2:end)) / 2;
end
obj.pixelCDF = cumsum(obj.pixelPDF);
obj.pixelDomain = pixelDomain;
obj.currentFrame = 1;
elseif isempty(colors)
colors = 'default';
end
%% Movie frame display
set ( obj.axsMovie ...
, 'XLim' , [0.5, 0.5+max(1,size(movie,2))] ...
, 'YLim' , [0.5, 0.5+max(1,size(movie,1))] ...
);
if isempty(movie)
set ( obj.imgMovie ...
, 'CData' , 0 ...
, 'UserData' , obj.currentFrame ...
);
else
if ndims(movie) == 3
movieFrame = movie(:,:,obj.currentFrame);
else
movieFrame = movie(:,:,:,obj.currentFrame);
end
set ( obj.sldFrame ...
, 'Min' , 1 ...
, 'Max' , obj.totalFrames ...
, 'SliderStep' , min([1 MovieSlider.FRAME_STEP]/(obj.totalFrames - 1), 1) ...
, 'Value' , obj.currentFrame ...
);
set ( obj.btnEnd ...
, 'Callback' , {@obj.setFrame, obj.totalFrames} ...
);
%% Special case for patch-based display, if custom x- and y-coordinates are provided
if isempty(xCoord) && isempty(yCoord)
set ( obj.imgMovie ...
, 'CData' , movieFrame ...
, 'UserData' , obj.currentFrame ...
);
else
%%
delete(obj.imgMovie);
[obj.imgMovie, ~, obj.imgX, obj.imgY] ...
= imagenan(obj.axsMovie, xCoord, yCoord, movieFrame, false);
set(obj.imgMovie, 'UserData', obj.currentFrame);
set(obj.axsMovie, 'XLim', obj.imgX([1 end]), 'YLim', obj.imgY([1 end]));
end
end
if obj.totalFrames > 1
set(obj.sldFrame, 'Enable', 'on');
else
set(obj.sldFrame, 'Enable', 'off');
end
stop(obj.tmrPlayback);
%% Apply formatting
obj.setContrast(contrast);
%% Use temperature palette if the movie sufficiently spans both negative and positive values
if isempty(colors)
colors = parula;
pixelSpread = quantile(binnedMovie(:), [0.1, 0.9]);
if pixelSpread(1) < 0 && pixelSpread(2) > 0 && -pixelSpread(1) > 0.5*pixelSpread(2) && pixelSpread(2) > -0.5*pixelSpread(1)
colors = cool();
obj.pixelRange = [-1 1]*max(abs(obj.pixelRange));
set(obj.axsMovie, 'CLim', obj.pixelRange);
end
end
obj.colors = colors;
colormap( obj.axsMovie, colors );
end
%----- Sets the display region size for this MovieSlider
function displayPos = setSize(obj, widthInPixels, heightInPixels)
if nargin < 3 && numel(widthInPixels) == 2
heightInPixels = widthInPixels(1);
widthInPixels = widthInPixels(2);
end
%% Compute required size of figure to contain the desired display area size
figSize = get(obj.figParent, 'Position');
figCorner = figSize(1:2) + figSize(3:4);
targetSize = [widthInPixels + 2*MovieSlider.GUI_BTNSIZE, heightInPixels + 3*MovieSlider.GUI_BTNSIZE];
targetPos = [max(1, figCorner - targetSize), targetSize];
%% Set the figure and axes positions
displayPos = [MovieSlider.GUI_BTNSIZE, MovieSlider.GUI_BTNSIZE, widthInPixels, heightInPixels];
set(obj.figParent, 'Position', targetPos);
set(obj.axsMovie, 'Units', 'pixels');
set(obj.axsMovie, 'Position', displayPos);
%% Restore normalized units so that the display area resizes with figure size
displayPos = get(obj.axsMovie, 'Position');
set(obj.axsMovie, 'Units', 'normalized');
end
%----- Set playback rate
function setPlaybackFPS(obj, playbackFPS)
obj.playbackFPS = playbackFPS;
set(obj.btnPlay, 'TooltipString', sprintf('Play (%.4g FPS)', obj.playbackFPS));
end
%----- Sets the current title, or removes it if an empty string is provided
function setTitle(obj, string, varargin)
if isempty(string)
delete(obj.titleMovie);
obj.titleMovie = gobjects(1);
obj.frameTitle = {};
elseif iscell(string)
obj.titleMovie = title(obj.axsMovie, string{obj.currentFrame}, varargin{:});
obj.frameTitle = string;
elseif isnumeric(string) && numel(string) == obj.totalFrames
string = arrayfun(@(x) sprintf('%.4g',x), string, 'UniformOutput', false);
obj.titleMovie = title(obj.axsMovie, string{obj.currentFrame}, varargin{:});
obj.frameTitle = string;
else
obj.titleMovie = title(obj.axsMovie, string, varargin{:});
obj.frameTitle = {};
end
end
%----- Sets per-frame updates for illustration objects
% Input:
% handle : a graphics handle to set the properties of per frame
% properties : a length p cell array of properties to set
% values : an n-by-p cell array of property values, where n is the number of frames and
% p the number of properties
function addIllustrations(obj, handle, properties, values)
for iIllus = 1:numel(handle)
obj.illustration(end+1) = handle;
obj.illusProperty{end+1} = properties;
obj.illusValue{end+1} = values(:,:,iIllus);
end
obj.drawFrame(obj.currentFrame, true);
end
%----- Sets the current frame to the given index
function setFrame(obj, handle, event, index)
if nargin < 3
index = handle;
end
index = max(1, min(index, obj.totalFrames));
if index ~= obj.currentFrame
set(obj.sldFrame, 'Value', index);
obj.drawFrame(index);
end
end
%----- Sets the function to call upon an 'escape' keypress
function setFocusReturn(obj, fcnReturn)
obj.fcnReturn = fcnReturn;
end
%----- Set contrast level
function setContrast(obj, contrast)
if ndims(obj.movie) > 3
return;
end
if numel(contrast) == 1
%% Select only values of CDF that are unique
obj.contrastIndex = max(1, min(contrast, numel(MovieSlider.CONTRAST_RANGE)));
saturation = MovieSlider.CONTRAST_RANGE(obj.contrastIndex);
sel = [1, 1 + find(diff(obj.pixelCDF) ~= 0)];
%% The following avoids creating very large ranges in the case of many empty bins in the PDF
iValue = interp1(obj.pixelCDF(sel), 1:numel(sel), [saturation, 1-saturation], 'linear', 'extrap');
inRange = iValue > 1 & iValue < numel(sel);
iValue = iValue(inRange);
pdfLo = obj.pixelValue(sel(floor(iValue)));
pdfUp = obj.pixelValue(sel(ceil(iValue)));
%% Default to maximum range unless there is a valid solution above
obj.pixelRange = obj.pixelValue([1 end]);
obj.pixelRange(inRange) = pdfLo + (pdfUp - pdfLo) .* (iValue - floor(iValue));
% obj.pixelRange = interp1(obj.pixelCDF(sel), obj.pixelValue(sel), [saturation, 1-saturation], 'linear', 'extrap');
%% Domain-specific constraints (e.g. fix left edge at 0 for positive domain)
sel = isfinite(obj.pixelDomain);
obj.pixelRange(sel) = obj.pixelDomain(sel);
elseif contrast(2) > contrast(1)
obj.pixelRange = contrast;
else
return;
end
set(obj.axsMovie, 'CLim', obj.pixelRange);
end
%----- Makes a clone of this MovieSlider in a new figure
function clone(obj, handle, event)
dup = MovieSlider(figure, obj.movie, obj.colors, obj.pixelDomain, obj.contrastIndex);
for prop = {'pixelPDF', 'pixelCDF', 'pixelValue', 'pixelRange'}
dup.(prop{:}) = obj.(prop{:});
end
prop = get(obj.title);
dup.setTitle(obj.frameTitle, prop{:}, 'Parent', dup.axsMovie);
end
%----- Saves all frames of this MovieSlider into a video file; see VideoWriter for allowed formats
function save(obj, outputFile, frameRate, varargin)
%% Default arguments
if nargin < 3
frameRate = [];
end
%% Optionally the user can provide a VideoWriter object with their desired configuration
if isa(outputFile, 'VideoWriter')
writer = outputFile;
else
writer = VideoWriter(outputFile, varargin{1:min(1,end)});
end
if ~isempty(frameRate)
writer.FrameRate = frameRate;
end
for iArg = 2:2:numel(varargin)-1
writer.(varargin{iArg}) = varargin{iArg+1};
end
%% Save current state
currentFrame = obj.currentFrame;
isGrayscale = get(writer, 'ColorChannels') == 1 && ndims(obj.movie) <= 3;
%% Iterate through frames and output into video file
open(writer);
if isGrayscale
writeVideo(writer, reshape(obj.movie, size(obj.movie,1), size(obj.movie,2), 1, []));
else
for iFrame = 1:obj.totalFrames
obj.setFrame(iFrame);
frame = getframe(obj.axsMovie);
writeVideo(writer, frame);
end
end
close(writer);
%% Restore current state
obj.setFrame(currentFrame);
end
end
%________________________________________________________________________
methods (Access = protected)
%----- Ensure that synchronized objects exist
function checkSiblings(obj)
obj.syncSiblings(~ishghandle(obj.syncSiblings)) = [];
end
%----- Shift the current frame by the given amount
function shiftFrame(obj, shift)
if shift < 0
obj.setFrame(max(1, obj.currentFrame + shift));
else
obj.setFrame(min(obj.totalFrames, obj.currentFrame + shift));
end
end
%----- Callback for user to enter the desired frame
function editSetFrame(obj, handle, event, index)
set(handle, 'Enable', 'inactive');
uicontrol(obj.txtDummy);
stop(obj.tmrPlayback);
value = sscanf(get(handle, 'String'), '%d');
if isempty(value)
value = obj.currentFrame;
end
obj.setFrame(value);
end
%----- Callback for user to enter the desired frame
function enableSetFrame(obj, handle, event, index)
set(handle, 'Enable', 'on', 'String', '');
uicontrol(handle);
end
%----- Callback to set whether the movie replays when at the end
function setRepeat(obj, handle, event)
obj.doRepeat = get(handle, 'Value');
if obj.doRepeat
set(handle, 'CData', MovieSlider.ICON_REFRESH_ON , 'TooltipString', 'Turn off repeat');
else
set(handle, 'CData', MovieSlider.ICON_REFRESH_OFF, 'TooltipString', 'Turn on repeat');
end
obj.checkSiblings();
for iSib = 1:numel(obj.syncSiblings)
other = obj.syncSiblings(iSib);
other.doRepeat = obj.doRepeat;
if other.doRepeat
set(other.btnRepeat, 'CData', MovieSlider.ICON_REFRESH_ON);
else
set(other.btnRepeat, 'CData', MovieSlider.ICON_REFRESH_OFF);
end
end
end
%----- Helper for drawFrame() that only affects this object
function doDrawFrame(obj, iFrame)
%% Set movie frame image
if ndims(obj.movie) == 3
set(obj.imgMovie, 'CData', obj.movie(:,:,iFrame), 'UserData', iFrame);
else
set(obj.imgMovie, 'CData', obj.movie(:,:,:,iFrame), 'UserData', iFrame);
end
obj.currentFrame = iFrame;
set(obj.txtInfo, 'String', sprintf('%d/%d', iFrame, obj.totalFrames));
%% Update title per frame
if ~isempty(obj.frameTitle)
set(obj.titleMovie, 'String', obj.frameTitle{iFrame});
end
%% Update illustration objects per frame
for iIllus = 1:numel(obj.illustration)
prop = [ obj.illusProperty{iIllus} ...
; obj.illusValue{iIllus}(iFrame,:) ...
];
set( obj.illustration(iIllus), prop{:} );
end
end
%----- Callback for scrolling between frames
function drawFrame(obj, handle, event, doForce)
% Frame to set the currently displayed one to
if isnumeric(handle)
iFrame = handle;
else
iFrame = min(obj.totalFrames, max(1, round(get(handle, 'Value'))));
end
if nargin > 2 && islogical(event)
doForce = event;
elseif nargin < 4
doForce = false;
end
% Update frame only if different from previous
if doForce || iFrame ~= obj.currentFrame
obj.doDrawFrame(iFrame);
end
% Scroll to the same frame for all associated MovieSlider siblings
obj.checkSiblings();
for iSib = 1:numel(obj.syncSiblings)
other = obj.syncSiblings(iSib);
jFrame = min(other.totalFrames, max(1, iFrame));
if doForce || jFrame ~= other.currentFrame
other.doDrawFrame(jFrame);
end
end
% Set input focus and refresh graphics
set(obj.figParent, 'CurrentAxes', obj.axsMovie);
drawnow;
end
%----- Callback to load the next frame in playback mode
function playMovie(obj, handle, event)
if obj.currentFrame >= obj.totalFrames
if obj.doRepeat
obj.currentFrame = 0;
else
obj.togglePlayback(obj.btnPlay, [], true);
return;
end
end
obj.setFrame(obj.currentFrame + 1);
end
%----- Callback to start/stop automatic playback
function togglePlayback(obj, handle, event, forceStop)
if nargin < 4
forceStop = false;
end
stop(obj.tmrPlayback);
% If already playing, stop
if forceStop || get(handle, 'UserData')
set(handle, 'CData', MovieSlider.ICON_PLAY, 'UserData', false, 'TooltipString', sprintf('Play (%.4g FPS)', obj.playbackFPS));
% If not playing, start
else
if obj.currentFrame >= obj.totalFrames
obj.currentFrame = 0;
end
set(handle, 'CData', MovieSlider.ICON_STOP, 'UserData', true, 'TooltipString', 'Stop');
set(obj.tmrPlayback, 'Period', max(1,round(1000/obj.playbackFPS))/1000);
start(obj.tmrPlayback);
end
end
%----- Callback for cycling contrast levels
function cycleContrast(obj, handle, event)
obj.setContrast(1 + mod(obj.contrastIndex, numel(MovieSlider.CONTRAST_RANGE)));
end
%----- Callback for adjusting frame display parameters
function configureDisplay(obj, handle, event)
% Allow only one instance
if ishghandle(obj.figConfig)
figure(obj.figConfig);
return;
end
% Store current configuration in case of user cancel
original = struct();
for field = MovieSlider.CONFIGURABLES
original.(field{:}) = obj.(field{:});
end
% Create dialog box
obj.figConfig = makePositionedFigure( MovieSlider.DIALOG_POSITION ...
, MovieSlider.DIALOG_MONITOR ...
, 'OuterPosition' ...
, 'Name' , 'Movie Slider Configuration' ...
, 'ToolBar' , 'none' ...
, 'MenuBar' , 'none' ...
, 'NumberTitle' , 'off' ...
, 'CloseRequestFcn' , @restoreConfig ...
, 'WindowButtonMotionFcn' , @mouseHoverHint ...
, 'WindowButtonUpFcn' , @(h,e) set(h,'WindowButtonMotionFcn',@mouseHoverHint) ...
, 'Visible' , 'off' ...
);
% Configuration panels
cntConfig = uix.HBox('Parent', obj.figConfig, 'Padding', MovieSlider.GUI_BORDER);
cntContrast = uix.VBox('Parent', cntConfig);
uix.Empty('Parent', cntConfig);
cntControls = uix.VBox('Parent', cntConfig, 'Padding', MovieSlider.GUI_BORDER, 'Spacing', MovieSlider.GUI_BORDER);
% Contrast adjustment
leeway = 0.05 * (obj.pixelValue(end) - obj.pixelValue(1));
axsRange = obj.pixelValue([1 end]) + [-1 1]*leeway;
axsPDF = axes ( 'Parent' , cntContrast ...
, 'Box' , 'on' ...
, 'Layer' , 'top' ...
, 'XLim' , axsRange ...
, 'FontSize' , MovieSlider.GUI_FONT - 1 ...
, 'Units' , 'pixels' ...
);
sldContrast = uicontrol ( 'Parent' , cntContrast ...
, 'Style' , 'slider' ...
, 'Min' , 1 ...
, 'Max' , numel(MovieSlider.CONTRAST_RANGE) ...
, 'SliderStep' , [1 5]/(numel(MovieSlider.CONTRAST_RANGE)-1) ...
, 'Value' , obj.contrastIndex ...
, 'Callback' , @slideContrast ...
, 'TooltipString' , 'Preset contrast levels' ...
);
cntManual = uix.HBox( 'Parent', cntContrast, 'Padding', MovieSlider.GUI_BORDER );
editBound(1) = uicontrol ( 'Parent' , cntManual ...
, 'Style' , 'edit' ...
, 'String' , sprintf('%.4g', obj.pixelRange(1)) ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , {@editContrast, 1} ...
, 'TooltipString' , 'Color range minimum' ...
);
uix.Empty ( 'Parent', cntManual );
editBound(2) = uicontrol ( 'Parent' , cntManual ...
, 'Style' , 'edit' ...
, 'String' , sprintf('%.4g', obj.pixelRange(end)) ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , {@editContrast, 2} ...
, 'TooltipString' , 'Color range maximum' ...
);
% Other controls
txtFPS = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'text' ...
, 'String' , 'Frames/sec:' ...
, 'FontSize' , MovieSlider.GUI_FONT ...
);
edtFPS = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'edit' ...
, 'String' , sprintf('%.4g', obj.playbackFPS) ...
, 'FontSize' , MovieSlider.GUI_FONT ...
);
edtColors = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'edit' ...
, 'String' , obj.colors ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , @setColors ...
, 'TooltipString' , 'Custom colormap' ...
);
mnuColors = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'listbox' ...
, 'String' , MovieSlider.COLORMAPS ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , {@setColors, edtColors} ...
, 'Min' , 0 ...
, 'Max' , 2 ...
, 'Value' , find(strcmp(MovieSlider.COLORMAPS, obj.colors)) ...
);
uix.Empty ( 'Parent', cntControls );
% Dialog box ok/cancel
btnOK = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'pushbutton' ...
, 'String' , 'OK' ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , @acceptConfig ...
, 'TooltipString' , 'Apply configuration' ...
);
btnCancel = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'pushbutton' ...
, 'String' , 'Cancel' ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , @restoreConfig ...
, 'TooltipString' , 'Discard configuration' ...
);
btnClone = uicontrol ( 'Parent' , cntControls ...
, 'Style' , 'pushbutton' ...
, 'String' , 'Clone' ...
, 'FontSize' , MovieSlider.GUI_FONT ...
, 'Callback' , @cloneSelf ...
, 'TooltipString' , sprintf('Clone MovieSlider into new figure\n(discards changes to configuration!)') ...
);
% Size of various panels
set ( cntConfig ...
, 'Widths' , [-1, MovieSlider.GUI_BTNSIZE, MovieSlider.GUI_BUTTON(1) + 2*MovieSlider.GUI_BORDER] ...
);
set ( cntContrast ...
, 'Heights' , [-1, MovieSlider.GUI_BTNSIZE, MovieSlider.GUI_BUTTON(2)+MovieSlider.GUI_BORDER] ...
);
set ( cntManual ...
, 'Widths' , [MovieSlider.GUI_BUTTON(1), -1, MovieSlider.GUI_BUTTON(1)] ...
);
set ( cntControls ...
, 'Heights' , [0.5 1 1 -1 1 1 1 1]*MovieSlider.GUI_BUTTON(2) ...
);
% Plot pixel distribution and range
hPDF = line ( 'Parent' , axsPDF ...
, 'XData' , obj.pixelValue ...
, 'YData' , obj.pixelPDF ...
, 'LineWidth' , 1 ...
, 'Color' , [0 0 0] ...
, 'PickableParts' , 'none' ...
);
xlabel(axsPDF, 'Pixel value', 'FontSize', MovieSlider.GUI_FONT-1);
ylabel(axsPDF, 'Frequency' , 'FontSize', MovieSlider.GUI_FONT-1);
yRange = get(axsPDF, 'YLim');
yRange(1) = 0;
[rngX, rngY] = rectangleCorners(obj.pixelRange(1), 0, obj.pixelRange(end)-obj.pixelRange(1), yRange(end));
hRange = patch ( 'Parent' , axsPDF ...
, 'XData' , rngX ...
, 'YData' , rngY ...
, 'EdgeColor' , 'none' ...
, 'FaceColor' , MovieSlider.RANGE_COLOR ...
, 'PickableParts' , 'none' ...
);
hBound(1) = line ( 'Parent' , axsPDF ...
, 'XData' , [1 1]*obj.pixelRange(1) ...
, 'YData' , yRange ...
, 'LineWidth' , 2 ...
, 'Color' , MovieSlider.RANGE_BORDER ...
, 'ButtonDownFcn' , {@startDragBound, 1} ...
);
hBound(2) = line ( 'Parent' , axsPDF ...
, 'XData' , [1 1]*obj.pixelRange(end) ...
, 'YData' , yRange ...
, 'LineWidth' , 2 ...
, 'Color' , MovieSlider.RANGE_BORDER ...
, 'ButtonDownFcn' , {@startDragBound, 2} ...
);
uistack(hBound, 'bottom');
uistack(hRange, 'bottom');
set(obj.figConfig, 'Visible', 'on');
% Callbacks for GUI interaction
function redrawRange(index)
if ~ishghandle(obj.figConfig)
return;
end
if nargin < 1
index = 1:numel(hBound);
end
[rngX, rngY] = rectangleCorners(obj.pixelRange(1), 0, obj.pixelRange(end)-obj.pixelRange(1), yRange(end));
set(hRange, 'XData', rngX, 'YData', rngY);
for iBound = index
set(hBound(iBound), 'XData', [1 1]*obj.pixelRange(iBound));
end
end
function setColors(handle, event, hEdit)
if ~ishghandle(obj.figConfig)
return;
end
value = get(handle, 'String');
if nargin > 2
indices = get(handle, 'Value');
if isempty(indices)
return;
end
value = value{indices(1)};
set(hEdit, 'String', value);
end
obj.colors = value;
colormap(obj.axsMovie, value);
end
function slideContrast(handle, event)
if ~ishghandle(obj.figConfig)
return;
end
obj.setContrast(round(get(handle, 'Value')));
set(editBound(1), 'String', sprintf('%.4g', obj.pixelRange(1)));
set(editBound(2), 'String', sprintf('%.4g', obj.pixelRange(end)));
redrawRange();
drawnow;
end
function editContrast(handle, event, index)
if ~ishghandle(obj.figConfig)
return;
end
obj.pixelRange(index) = str2double(get(handle, 'String'));
obj.setContrast(obj.pixelRange);
redrawRange(index);