-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoiEditor.m
1640 lines (1327 loc) · 64.8 KB
/
RoiEditor.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
classdef RoiEditor < hgsetget
% class RoiEditor: interactive gui to edit rois for image processing
% data.
%
% @file: RoiEditor.m
% @brief: interactive gui to edit rois for image processing
% @author: Paxon Frady
% @created: 9/27/2011
properties
% gui properties
h; % graphic object handles.
gui; % settings for the gui.
% hgsetget properties
Position; % The size and position of the object
Parent; % The parent of the object
% object properties
im_data; % The image data.
im_x; % The x image values.
im_y; % The y image values.
rois; % The roi data.
selected_rois; % The currently selected rois.
current_frame; % The currently selected frame.
is_editing_enabled; % Flag for enabling/disabling roi editing.
color_rois; % Flag for enabling/disabling roi coloring. Disable if you want an outside function to set the colors.
roi_mode; % xyrra rois or blob rois.
move_mode; % move all frames, all frames after current, or current frame only.
show_inactive_rois; % whether or not to show the inactive rois.
end %properties
properties (Constant)
ShiftAfter = 1;
ShiftAll = 2;
Each = 3;
MoveAfter = 4;
MoveAll = 5;
end
events
NewRois; % Triggered when new rois are created.
DeletedRois; % Triggered when rois are deleted.
AlteredRois; % Triggered when rois are altered.
SelectionChanged; % Triggered when selected rois change.
FrameChanged; % Triggered when the current frame is changed.
LevelsChanged;
end %events
methods
%%%%%%%%%% Initialization %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function self = RoiEditor(parent, im_data)
% constructor: creates a new RoiEditor object.
%
% @param: parent the parent handle of this object. If no parent
% is given, a new figure will be created and used as the
% parent.
% @param: im_data an MxNxt or MxNx3xt matrix of image data.
% @return: self handle to the RoiEditor object.
if nargin < 1
parent = [];
end
if nargin < 2 || isempty(im_data)
im_data = rand(128, 128, 10);
end
self.im_data = im_data;
self = self.init_state();
self = self.init_gui(parent);
end
function self = init_state(self)
% init_state: initializes the state variables of the gui.
self.gui.inited = 0;
self.selected_rois = [];
self.current_frame = 1;
self.is_editing_enabled = true;
self.color_rois = true;
self.rois.xyrra = [];
self.rois.blob = [];
self.gui.Position = [0 0 512 547];
self.gui.SLIDER_H = 30;
self.gui.MARGIN = 5;
self.gui.BUTTON_W = 60;
self.gui.BUTTON_H = 40;
self.gui.SCROLL_W = 20;
self.gui.play_button_im = imread('Resources/play_button.png');
self.gui.pause_button_im = imread('Resources/pause_button.png');
self.gui.play_back_im = self.gui.play_button_im;
for i = 1:size(self.gui.play_back_im, 3)
self.gui.play_back_im(:,:,i) = fliplr(self.gui.play_back_im(:,:,i));
end
self.gui.play_rate = 0.01;
self.gui.frame_step = 1;
self.gui.play_timer = timer('TimerFcn', @self.play_timer_cb, ...
'ExecutionMode', 'fixedRate', 'Period', self.gui.play_rate);
self.im_x = 1:size(self.im_data, 2);
self.im_y = 1:size(self.im_data, 1);
self.move_mode = RoiEditor.MoveAll; % all, after, current
self.show_inactive_rois = 1;
self.gui.dynamic_contrast = 10;
end
function self = init_gui(self, parent)
% init_gui: initializes the gui objects.
%
% @param: parent the parent handle of this object. Default is
% to create a new figure. Use [] for default.
if nargin < 2 || isempty(parent)
% No parent given, then make a new figure as the parent.
parent = figure();
set(parent, 'Position', [100, 100, self.gui.Position(3), self.gui.Position(4)]);
end
self.h.Parent = parent;
% We must use a figure event notifier, which is attached to the
% top-most figure. Go up until we get the figure.
self.h.fh = self.h.Parent;
while ~strcmp(get(self.h.fh, 'Type'), 'figure')
% Then the fh object is not a figure, so go up.
self.h.fh = get(self.h.fh, 'Parent');
end
% Now fh is the parent figure. Set its event notifier.
self.gui.fen = FigureEventNotifier(self.h.fh);
addlistener(self.gui.fen, 'WindowKeyPress', @self.window_key_press_cb);
% Create the main panel.
%self.h.panel = uipanel('Parent', self.h.Parent, 'Units', 'pixels');
%set(self.h.panel, 'BorderType', 'none');
self.h.panel = uiextras.BoxPanel('Parent', self.h.Parent, 'Title', 'ROI Editor', 'Units', 'normalized', 'Position', [0 0 1 1]);
% Image
self.h.im_axes = axes('Units', 'normalized', 'Position', [0 0 1 1]);
set(self.h.im_axes, 'XTick', [], 'YTick', [], 'NextPlot', 'add', 'Box', 'off');
self.gui.im_ax_aen = AxesEventNotifier(self.h.im_axes);
addlistener(self.gui.im_ax_aen, 'ButtonDown', @self.image_button_down_cb);
%set(self.h.im_axes, 'ButtonDownFcn', @self.image_button_down_cb);
self.h.im = imagesc(self.get_frame(1), 'Parent', self.h.im_axes);
set(self.h.im, 'HitTest', 'off'); % Turns off mouse interaction with the image.
colormap(self.h.im_axes, 'gray');
axis(self.h.im_axes, 'tight');
% handle for roi lines.
self.h.rois = [];
% Frame slider.
self.h.frame_slider = uicontrol('Style', 'slider');
set(self.h.frame_slider, 'Callback', @self.frame_slider_cb);
set(self.h.frame_slider, 'Value', 1);
self.update_frame_slider();
% Play/pause button
self.h.play_toggle = uicontrol('Style', 'togglebutton', 'Callback', @self.play_toggle_cb, ...
'CData', self.gui.play_button_im, 'BackgroundColor', 'w');
% Fast-forward/frame skip
self.h.ff_button = uicontrol('Style', 'pushbutton', 'Callback', @self.ff_button_cb, ...
'CData', imresize(self.gui.play_button_im, 0.5), 'BackgroundColor', 'w');
self.h.rew_button = uicontrol('Style', 'pushbutton', 'Callback', @self.rew_button_cb, ...
'CData', imresize(self.gui.play_back_im, 0.5));
self.h.frame_step_edit = uicontrol('Style', 'edit', 'Callback', @self.frame_step_edit_cb);
set(self.h.frame_step_edit, 'String', num2str(self.gui.frame_step));
% Frame display
self.h.frame_edit = uicontrol('Style', 'edit', 'Callback', @self.frame_edit_cb);
set(self.h.frame_edit, 'String', 'none');
% Blob mode check box
%self.h.blob_mode_checkbox = uicontrol('Style', 'checkbox');
%set(self.h.blob_mode_checkbox, 'Callback', @self.blob_mode_checkbox_cb);
%set(self.h.blob_mode_checkbox, 'String', 'Blob Roi Mode');
%set(self.h.blob_mode_checkbox, 'Value', 0);
% Move mode Push button
self.h.move_mode_push = uicontrol('Style', 'pushbutton');
set(self.h.move_mode_push, 'Callback', @self.move_mode_push_cb);
set(self.h.move_mode_push, 'String', 'ShiftAfter');
% Markers for rotation and resize.
self.h.rotation_marker = plot(self.h.im_axes, 0, 0, 's', 'Visible', 'off', ...
'MarkerFaceColor', 'k', 'MarkerEdgeColor', 'k', 'MarkerSize', 10, ...
'ButtonDownFcn', @self.rotation_marker_cb);
self.h.xresize_marker = plot(self.h.im_axes, 0, 0, 's', 'Visible', 'off', ...
'MarkerFaceColor', 'k', 'MarkerEdgeColor', 'k', 'MarkerSize', 10, ...
'ButtonDownFcn', @self.xresize_marker_cb);
self.h.yresize_marker = plot(self.h.im_axes, 0, 0, 's', 'Visible', 'off', ...
'MarkerFaceColor', 'k', 'MarkerEdgeColor', 'k', 'MarkerSize', 10, ...
'ButtonDownFcn', @self.yresize_marker_cb);
self = self.uiextras_layout();
self.gui.inited = 1;
self.update();
end
function self = uiextras_layout(self)
% self = uiextras_layout(self): Sets the layout of the gui components
self.h.main_vbox = uiextras.VBoxFlex();
self.h.control_hbox = uiextras.HBox();
self.h.frame_step_vbox = uiextras.VBox();
self.h.ff_rew_hbox = uiextras.HBox();
% Top-level hierarchy
set(self.h.main_vbox, 'Parent', self.h.panel);
set(self.h.im_axes, 'Parent', self.h.main_vbox.double());
set(self.h.control_hbox, 'Parent', self.h.main_vbox);
% axes level
% Frame step hierarchy
set(self.h.frame_step_edit, 'Parent', self.h.frame_step_vbox.double());
set(self.h.ff_rew_hbox, 'Parent', self.h.frame_step_vbox);
set(self.h.rew_button, 'Parent', self.h.ff_rew_hbox.double());
set(self.h.ff_button, 'Parent', self.h.ff_rew_hbox.double());
% Control
set(self.h.play_toggle, 'Parent', self.h.control_hbox.double());
set(self.h.frame_step_vbox, 'Parent', self.h.control_hbox);
set(self.h.frame_slider, 'Parent', self.h.control_hbox.double());
set(self.h.frame_edit, 'Parent', self.h.control_hbox.double());
set(self.h.move_mode_push, 'Parent', self.h.control_hbox.double());
set(self.h.control_hbox, 'Sizes', [self.gui.BUTTON_W, self.gui.BUTTON_W, -1, self.gui.BUTTON_W, self.gui.BUTTON_W]);
set(self.h.main_vbox, 'Sizes', [-1, self.gui.BUTTON_H]);
end
%%%%%%%%%% Callbacks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function window_key_press_cb(self, source_h, eventdata)
% Callback for when a keyboard key is pressed.
eventdata = self.gui.fen.last_eventdata;
if self.is_editing_enabled
% Only allow these keys to work if editing is enabled.
% Control+[ijkl] moves the rois
if ~isempty(self.selected_rois) && self.gui.fen.is_key_down('control')
if strcmp(eventdata.Key, 'j')
self.make_roi_move([-1 0]);
elseif strcmp(eventdata.Key, 'l')
self.make_roi_move([1 0]);
elseif strcmp(eventdata.Key, 'i')
self.make_roi_move([0 1]);
elseif strcmp(eventdata.Key, 'k')
self.make_roi_move([0 -1]);
end
end
% Control+[delete backspace] deletes the currently selected
% rois.
if self.gui.fen.is_key_down('control') ...
&& (strcmp(eventdata.Key, 'delete') || strcmp(eventdata.Key, 'backspace'))
self.delete_rois(self.selected_rois);
end
end
% Control+a selects all rois.
if self.gui.fen.is_key_down('control') && strcmp(eventdata.Key, 'a')
self.set_selected_rois(1:self.get_num_rois());
end
% h toggles show_inactive_rois
if strcmp(eventdata.Key, 'h')
self.toggle_show_inactive_rois();
end
% space moves to the next roi
if strcmp(eventdata.Key, 'space')
self.cycle_rois();
end
% Enable/disable editing
if self.gui.fen.is_key_down('control') && strcmp(eventdata.Key, 'e')
if self.is_editing_enabled
self.disable_roi_editing();
else
self.enable_roi_editing();
end
end
end
function image_button_down_cb(self, source_h, eventdata)
% First get the mouse position
point1 = get(self.h.im_axes, 'CurrentPoint');
fig_point = get(self.h.fh, 'CurrentPoint');
px1 = point1(1);
py1 = point1(3);
s = []; % the selected rois.
s = self.rois_at_pos([px1 py1]);
if self.gui.fen.is_key_down('shift')
% If the shift key is down, then we are adding/removing an
% roi to/from the selection list. If click was not in an
% roi and the shift key was down, nothing will happen.
self.toggle_selected_rois(s);
elseif ~isempty(s)
% The mouse is inside an roi, the shift key is not down.
% This means we are moving an roi, or clicking for
% selection.
self.animate_roi_move([px1, py1], s);
else
% The mouse is not inside an roi, so we are dragging to
% create one.
self.animate_roi_creation([px1, py1]);
end
end
function frame_slider_cb(self, source_h, eventdata)
% frame_slider_cb: callback of the slider. Gets the slider
% value and updates the gui.
cf = round(get(self.h.frame_slider, 'Value'));
self.set_current_frame(cf);
end
function level_scroll_cb(self, source_h, eventdata)
%disp('level_scroll_cb');
%self.update();
%notify(self, 'LevelsChanged');
end
function frame_edit_cb(self, source_h, eventdata)
cf = str2num(get(self.h.frame_edit, 'String'));
self.set_current_frame(cf);
end
function blob_mode_checkbox_cb(self, source_h, eventdata)
end
function move_mode_push_cb(self, source_h, eventdata)
% move_mode_push_cb: Callback of move_mode_push push button.
% This will cycle through move_modes.
self.cycle_move_mode();
end
function rotation_marker_cb(self, source_h, eventdata)
point1 = get(self.h.im_axes, 'CurrentPoint');
self.animate_roi_rotation([point1(1) point1(3)]);
end
function xresize_marker_cb(self, source_h, eventdata)
point1 = get(self.h.im_axes, 'CurrentPoint');
px1 = point1(1);
py1 = point1(3);
self.animate_roi_xresize([px1, py1]);
end
function yresize_marker_cb(self, source_h, eventdata)
point1 = get(self.h.im_axes, 'CurrentPoint');
px1 = point1(1);
py1 = point1(3);
self.animate_roi_yresize([px1, py1]);
end
function link_new_rois_cb(self, source_h, eventdata)
% Add the rois, but don't trigger the event.
self.add_rois(eventdata.data.rois, false);
end
function link_selection_changed_cb(self, source_h, eventdata)
self.set_selected_rois(source_h.selected_rois);
end
function link_frame_changed_cb(self, source_h, eventdata)
% What if the frames are different?
self.current_frame = source_h.current_frame;
if self.current_frame < 1 || self.current_frame > self.get_num_frames()
% Something is wrong.
disp('frame mismatch!');
end
end
function link_deleted_rois_cb(self, source_h, eventdata)
% Remove the rois, but don't trigger the event.
self.delete_rois(eventdata.data.roi_idxs, false);
end
function play_toggle_cb(self, source_h, eventdata)
v = get(self.h.play_toggle, 'Value');
if v
set(self.h.play_toggle, 'CData', self.gui.pause_button_im)
start(self.gui.play_timer);
else
set(self.h.play_toggle, 'CData', self.gui.play_button_im)
stop(self.gui.play_timer);
notify(self, 'FrameChanged');
end
end
function play_timer_cb(self, source_h, eventdata)
self.set_current_frame(self.current_frame + self.gui.frame_step, 0);
% self.current_frame = self.current_frame + self.gui.frame_step;
% if self.current_frame > size(self.im_data, 3)
% % Then start back at the beginning
% self.current_frame = 1;
% end
% self.update();
% notify(self, 'FrameChanged');
end
function ff_button_cb(self, source_h, eventdata)
self.play_settings([], self.gui.frame_step + 1);
end
function rew_button_cb(self, source_h, eventdata)
self.play_settings([], self.gui.frame_step - 1);
end
function frame_step_edit_cb(self, source_h, eventdata)
val = str2num(get(self.h.frame_step_edit, 'String'));
self.play_settings([], val);
end
%%%%%%%%%% Main Functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function update(self)
% update: main update function. Matches gui with internal
% state.
% @todo: checks on the state. Make sure there aren't errors.
%set(self.h.im, 'CData', repmat(norm_range(double(self.get_frame(self.current_frame))), [1 1 3]));
frame = self.get_nframe(self.current_frame);
if size(frame, 3) == 1
frame = repmat(frame, [1 1 3]);
end
set(self.h.im, 'CData', frame, 'XData', [self.im_x(1), self.im_x(end)], 'YData', [self.im_y(1), self.im_y(end)]);
set(self.h.frame_slider, 'Value', self.current_frame);
set(self.h.frame_edit, 'String', num2str(self.current_frame));
if ~isempty(self.rois.xyrra)
[rx, ry] = oval2xy(self.rois.xyrra(:, :, self.current_frame), [], 0);
set(self.h.rois, 'Visible', 'off');
set(self.h.rois, 'HitTest', 'off');
for i = 1:self.get_num_rois()
if i > length(self.h.rois)
% The handle does not exist so create it.
self.h.rois(i, 1) = plot(self.h.im_axes, rx(i,:), ry(i,:), 'LineWidth', 2);
else
set(self.h.rois(i), 'XData', rx(i, :), 'YData', ry(i, :), 'LineWidth', 2);
end
end
set(self.h.rois(self.selected_rois), 'Visible', 'on', 'LineWidth', 4);
if self.show_inactive_rois
set(self.h.rois(1:self.get_num_rois()), 'Visible', 'on');
end
if self.color_rois
set(self.h.rois, 'Color', 'b');
set(self.h.rois(self.selected_rois), 'Color', 'w');
end
else
set(self.h.rois, 'Visible', 'off', 'HitTest', 'off');
end
if length(self.selected_rois) == 1 && self.is_editing_enabled
% Make sure the markers are on top of the other lines.
ax_children = get(self.h.im_axes, 'Children');
ax_markers = ismember(ax_children, [self.h.rotation_marker, self.h.xresize_marker, self.h.yresize_marker]);
ax_sorted = [ax_children(ax_markers); ax_children(~ax_markers)];
set(self.h.im_axes, 'Children', ax_sorted);
% If there is only a single selected roi, then show the
% rotation and resize markers.
set(self.h.rotation_marker, 'Visible', 'on');
set(self.h.xresize_marker, 'Visible', 'on');
set(self.h.yresize_marker, 'Visible', 'on');
self.set_edit_marker_positions(self.rois.xyrra(self.selected_rois, :, self.current_frame));
else
set(self.h.rotation_marker, 'Visible', 'off');
set(self.h.xresize_marker, 'Visible', 'off');
set(self.h.yresize_marker, 'Visible', 'off');
end
self.update_move_mode_push();
drawnow;
end
function set_selected_rois(self, val)
if ~isempty(self.selected_rois) && isequal(self.selected_rois, val)
% The selected_rois haven't change, so don't do anything
disp('selection is the same');
return;
end
self.selected_rois = val;
self.update();
notify(self, 'SelectionChanged');
end
function val = set_current_frame(self, val, trigEvent)
% set_current_frame: sets the current frame. If the frame given
% is out of range, will set the frame to 1. Returns the frame.
% Make sure val is an int
val = round(val);
if val < 1 || val > self.get_num_frames()
% Then val is out of range.
%disp('val is out of range setting to 1.');
val = 1;
self.current_frame = val;
%val = self.current_frame;
return;
end
if ~isempty(self.current_frame) && all(self.current_frame == val)
% The current frame hasn't changed, so don't do anything
return;
end
if nargin < 3 || isempty(trigEvent)
trigEvent = 1;
end
self.current_frame = val;
if trigEvent
notify(self, 'FrameChanged');
end
self.update();
end
function roi_idxs = rois_at_pos(self, xy)
% rois_at_pos: takes an xy coordinate and detects if the point
% falls within any ROIs. Returns all ROIs that overlap the
% point.
%
% @param: xy [x y] the coordinates
% @return: roi_idxs the indices of the rois that overlap xy.
% if self.mode == 'xyrra'
if isempty(self.rois.xyrra)
roi_idxs = [];
return;
end
xc = self.rois.xyrra(:, 1, self.current_frame) - xy(1);
yc = self.rois.xyrra(:, 2, self.current_frame) - xy(2);
xr = cos(-self.rois.xyrra(:, 5, self.current_frame)) .* xc - sin(-self.rois.xyrra(:, 5, self.current_frame)) .* yc;
yr = sin(-self.rois.xyrra(:, 5, self.current_frame)) .* xc + cos(-self.rois.xyrra(:, 5, self.current_frame)) .* yc;
% The 0.5 is to get the radii, rois are the circle that
% inscribes the rectangle defined.
d = 0.5 * ((xr ./ self.rois.xyrra(:, 3, self.current_frame)) .^ 2 + (yr ./ self.rois.xyrra(:, 4, self.current_frame)) .^ 2);
roi_idxs = find(d <= 1);
end
function toggle_selected_rois(self, roi_idxs)
% toggle_selected_rois: changes the selected state of the rois given.
%
% @param: roi_idxs the rois to toggle. Default is all rois.
if nargin < 2
roi_idxs = 1:self.get_num_rois();
end
turn_off = ismember(roi_idxs, self.selected_rois);
members = ismember(self.selected_rois, roi_idxs);
sr = self.selected_rois;
sr(members) = [];
self.set_selected_rois([sr, roi_idxs(~turn_off)']);
self.update();
end
function add_rois(self, rois, trigEvent)
% add_rois: adds an roi to the set of rois.
%
% @param: rois an xyrra (Mx5) or blob roi. The roi can have a
% single frame, in which case it will be added to all frames.
% @param: trigEvent flag indicating whether or not to trigger
% the NewRois event. Default is 1.
disp('add_rois');
if ~self.is_editing_enabled
% Editing is disabled, so cannot add rois.
disp('Editing is disabled. Enable before adding new rois.');
return;
end
if nargin < 3
trigEvent = true;
end
if size(rois, 1) == size(self.im_data, 1) && size(rois, 2) == size(self.im_data, 2)
% Then the rois is a blob.
% @todo
else
if isempty(self.rois.xyrra)
% This is giving me problems. This should fix most
% empty issues.
self.rois.xyrra = nan(1,5,self.get_num_frames());
end
% Then the rois is xyrra
nidx = self.get_num_rois() + 1;
nidx = nidx:(nidx+size(rois, 1) - 1);
if ndims(rois) == 3
% Then we have an rois for each frames.
self.rois.xyrra(nidx, :, :) = rois;
else
% Then copy the rois to all frames.
self.rois.xyrra(nidx, :, :) = repmat(rois, [1 1 size(self.rois.xyrra, 3)]);
end
end
if trigEvent
d = struct('rois', rois, 'roi_idxs', nidx);
notify(self, 'NewRois', DataEvent(d));
end
% We must notify the add before changing the selection, in case
% there is a linked RoiEditor.
self.set_selected_rois(nidx);
self.update();
disp('end add');
end
function set_rois(self, rois, trigEvent)
% set_rois(self, rois, trigEvent): sets all of the rois.
disp('RoiEditor.set_rois');
if nargin < 3
trigEvent = true;
end
if size(rois, 1) == size(self.im_data, 1) && size(rois, 2) == size(self.im_data, 2)
% Then the rois is a blob.
% @todo
else
% Then the rois is xyrra
if size(rois, 3) == 1
% Then we will copy the rois to all frames.
self.rois.xyrra = repmat(rois, [1 1 self.get_num_frames()]);
elseif size(rois, 3) == self.get_num_frames()
% Then there is an roi for all frames.
self.rois.xyrra = rois;
else
error('rois dimensions mismatch image dimensions');
end
end
self.selected_rois = [];
if trigEvent
d = struct('roi_idxs', 1:self.get_num_rois());
notify(self, 'AlteredRois', DataEvent(d));
end
self.update();
disp('end RoiEditor.set_rois');
end
function shift_rois(self, roi_idxs, delta, frames, trigEvent)
% shift_rois: moves the rois specified in roi_idxs the amount
% specified in delta. This is a relative amount.
%
% @param: roi_idxs the index positions of the rois to move.
% @param: delta [x y] amount to move the rois.
% @param: frames the frames which to move the rois. Default is
% all frames.
% @param: trigEvent flag indicating whether or not to trigger
% AlteredRois event. Default true.
if ~self.is_editing_enabled
% Editing is disabled, so cannot shift rois.
disp('Editing is disabled. Enable before moving rois.');
return;
end
if nargin < 4
frames = 1:self.get_num_frames();
end
if nargin < 5
trigEvent = true;
end
% if xyrra
self.rois.xyrra(roi_idxs, 1, frames) = self.rois.xyrra(roi_idxs, 1, frames) + delta(1);
self.rois.xyrra(roi_idxs, 2, frames) = self.rois.xyrra(roi_idxs, 2, frames) + delta(2);
if trigEvent
a.roi_idxs = roi_idxs;
notify(self, 'AlteredRois', DataEvent(a));
end
self.update();
end
function move_rois(self, roi_idxs, locs, frames, trigEvent)
% move_rois: moves the rois specified in roi_idxs to the
% locations given.
%
% @param: roi_idxs the index positions of the rois to move.
% @param: locs [x y] the locations to put the new rois. This
% can be several forms:
% [x y]: all rois are moved to the location given
% N x 2: where N is the length of roi_idxs - each roi will
% be moved to the location specified by row. This will effect
% all frames given by frames.
% N x 2 x M: where M is the length of frames - the rois for
% each roi and each frame will be moved to the corresponding
% location.
% @param: frames the frames which to move the rois. Default is
% all frames.
% @param: trigEvent flag indicating whether or not to trigger
% AlteredRois event. Default true.
if ~self.is_editing_enabled
% Editing is disabled, so cannot move rois.
disp('Editing is disabled. Enable before moving rois.');
return;
end
if nargin < 4
% Default for frames is all frames
frames = 1:self.get_num_frames();
end
if nargin < 5
% Default for trigEvent is true.
trigEvent = true;
end
% if xyrra
if numel(locs) == 2
% Then we have just x, y coordinates.
self.rois.xyrra(roi_idxs, 1, frames) = locs(1);
self.rois.xyrra(roi_idxs, 2, frames) = locs(2);
elseif size(locs, 1) == length(roi_idxs)
if size(locs, 3) == length(frames)
% Then we have locations for every roi for every frame
self.rois.xyrra(roi_idxs, 1, frames) = locs(:, 1, :);
self.rois.xyrra(roi_idxs, 2, frames) = locs(:, 2, :);
elseif size(locs, 3) == 1
% Then we have locs for every roi, so change over all
% frames.
self.rois.xyrra(roi_idxs, 1, frames) = repmat(locs(:, 1), [1 1 length(frames)]);
self.rois.xyrra(roi_idxs, 2, frames) = repmat(locs(:, 2), [1 1 length(frames)]);
else
error('locs is not shaped correctly');
end
else
error('locs is not shaped correctly.');
end
if trigEvent
a.roi_idxs = roi_idxs;
notify(self, 'AlteredRois', DataEvent(a));
end
self.update();
end
function delete_rois(self, roi_idxs, trigEvent)
% delte_rois: deletes the rois given.
%
% @param: roi_idxs the indices of the rois to delete.
% @param: trigEvent flag indicating whether or not to trigger
% AlteredRois event. Default true.
disp('RoiEditor.delete_rois');
if ~self.is_editing_enabled
% Editing is disabled, so cannot dekete rois.
disp('Editing is disabled. Enable before deleting rois.');
return;
end
if isempty(roi_idxs)
% Nothing to delete
return;
end
if nargin < 3
% Default for trigEvent is true.
trigEvent = true;
end
% Keep the selected rois selected
is_selected = false(self.get_num_rois(), 1);
is_selected(self.selected_rois) = 1;
is_selected(roi_idxs) = [];
disp('before delete');
% Delete the rois
self.rois.xyrra(roi_idxs, :, :) = [];
% Delete the roi handles
delete(self.h.rois(roi_idxs));
disp('after delete');
self.h.rois(roi_idxs) = [];
if trigEvent
a.roi_idxs = roi_idxs;
notify(self, 'DeletedRois', DataEvent(a));
end
self.set_selected_rois = find(is_selected);
self.update();
disp('end RoiEditor.delete_rois');
end
function resize_rois(self, roi_idxs, new_rr, trigEvent)
% resize_rois: resizes the rois to the given size.
%
% @param: roi_idxs the rois to resize.
% @param: new_rr [xr, yr] the new size of the rois.
% @param: trigEvent flag indicating whether or not to trigger
% AlteredRois event. Default true.
if ~self.is_editing_enabled
% Editing is disabled, so cannot resize rois.
disp('Editing is disabled. Enable before resizing rois.');
return;
end
% @todo: input checks
if nargin < 5
% Default for trigEvent is true.
trigEvent = true;
end
if numel(new_rr, 2) == 2
self.rois.xyrra(roi_idxs, 3, :) = abs(new_rr(1));
self.rois.xyrra(roi_idxs, 4, :) = abs(new_rr(2));
elseif ndims(new_rr) == 3
self.rois.xyrra(roi_idxs, 3, :) = abs(new_rr(:, 1, :));
self.rois.xyrra(roi_idxs, 4, :) = abs(new_rr(:, 2, :));
elseif ndims(new_rr) == 2
self.rois.xyrra(roi_idxs, 3, :) = repmat(abs(new_rr(:, 1)), [1 1 size(self.rois.xyrra, 3)]);
self.rois.xyrra(roi_idxs, 4, :) = repmat(abs(new_rr(:, 2)), [1 1 size(self.rois.xyrra, 3)]);
else
error('new_rr shape not recognized');
end
if trigEvent
a.roi_idxs = roi_idxs;
notify(self, 'AlteredRois', DataEvent(a));
end
self.update();
end
function rotate_rois(self, roi_idxs, angle)
% rotate_rois: rotates the rois by the amount angle in radians.
%
% @param: roi_idxs the rois to rotate.
% @param: angle the amount to rotate the rois.
% @param: trigEvent flag indicating whether or not to trigger
% AlteredRois event. Default true.
if ~self.is_editing_enabled
% Editing is disabled, so cannot rotate rois.
disp('Editing is disabled. Enable before rotating rois.');
return;
end
if nargin < 5
% Default for trigEvent is true.
trigEvent = true;
end
self.rois.xyrra(roi_idxs, 5, :) = self.rois.xyrra(roi_idxs, 5, :) + angle;
if trigEvent
a.roi_idxs = roi_idxs;
notify(self, 'AlteredRois', DataEvent(a));
end
self.update();
end
function cycle_move_mode(self)
% cycle_move_mode: changes the move_mode to the next mode in
% the cycle.
if self.move_mode == RoiEditor.ShiftAfter
self.move_mode = RoiEditor.ShiftAll;
elseif self.move_mode == RoiEditor.ShiftAll
self.move_mode = RoiEditor.Each;
elseif self.move_mode == RoiEditor.Each
self.move_mode = RoiEditor.MoveAfter;
elseif self.move_mode == RoiEditor.MoveAfter
self.move_mode = RoiEditor.MoveAll;
elseif self.move_mode == RoiEditor.MoveAll;
self.move_mode = RoiEditor.ShiftAfter;
end
self.update();
end
function toggle_show_inactive_rois(self)
% toggle_show_inactive_rois: flips the state of the
% show_inactive_rois flag.
self.show_inactive_rois = ~self.show_inactive_rois;
self.update();
end
function cycle_rois(self)
% cycle_rois: selects the next roi.
if self.get_num_rois() == 0
% Then there are no rois to cycle through.
return;
end
if isempty(self.selected_rois)
s = 1;
else
s = max(self.selected_rois);
s = s+1;
end
if s > self.get_num_rois()
s = 1;
end
self.set_selected_rois(s);
self.update();
end
function link_roi_editors(self, roi_editor)
% link_roi_editors: links this roi_editor to another one, so
% that rois are created/modified/deleted together. Only the roi
% moves are seperate. This is useful for analyzing
% ratio-imaging data.
if self == roi_editor
error('Cannot link to self');
end
if ~isfield(self.gui, 'linked_roi_editors')
self.gui.linked_roi_editors = [];
end
if any(ismember(self.gui.linked_roi_editors, roi_editor))
% self is already linked to the other roi_editor.
return;
end
% Add the roi_editor to the list of linked roi_editors.
self.gui.linked_roi_editors = [self.gui.linked_roi_editors roi_editor];
addlistener(roi_editor, 'NewRois', @self.link_new_rois_cb);
addlistener(roi_editor, 'SelectionChanged', @self.link_selection_changed_cb);
addlistener(roi_editor, 'FrameChanged', @self.link_frame_changed_cb);
addlistener(roi_editor, 'DeletedRois', @self.link_deleted_rois_cb);
% Link the other editor to this one.
roi_editor.link_roi_editors(self);
end
function disable_roi_editing(self)
% disable_roi_editing: disables any changes to rois (no
% add, delete or alter). Rois can still be selected.
self.is_editing_enabled = false;
self.update();
end
function enable_roi_editing(self)
% enable_roi_editing: enables roi changes.
self.is_editing_enabled = true;