forked from justinblaber/ncorr_2D_matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncorr_gui_setrois.m
448 lines (380 loc) · 16.9 KB
/
ncorr_gui_setrois.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
function [rois,outstate] = ncorr_gui_setrois(imgs,pos_parent,params_init)
% This is a GUI for setting the ROIs for either the reference or current
% image(s).
%
% Inputs -----------------------------------------------------------------%
% imgs - ncorr_class_img; the input images(s); Generally, it's a single
% image, although multiple image are supported.
% pos_parent - integer array; this is the position of the parent figure
% which determines where to position this figure
% params_init - ncorr_class_roi; contains ROI(s) if they have been set
% before; otherwise it's empty. Number of input ROI(s) must be the same
% length as imgs.
%
% Outputs ----------------------------------------------------------------%
% rois - ncorr_class_roi; contains the ROI(s) for the analysis
% outstate - integer; returns either out.cancelled, out.failed, or
% out.success.
% Data ---------------------------------------------------------------%
% Initialize outputs
outstate = out.cancelled;
rois = ncorr_class_roi.empty;
% Get GUI handles
handles_gui = init_gui();
% Run c-tor
feval(ncorr_util_wrapcallbacktrycatch(@constructor,handles_gui.figure));
% Callbacks and functions --------------------------------------------%
function constructor()
% Initialize buffers
if (isempty(params_init))
rois_prelim(1:length(imgs)) = ncorr_class_roi;
list_rois = false(1,length(imgs)); % Used to keep track of which rois are set
else
rois_prelim = params_init;
list_rois = true(1,length(imgs)); % Used to keep track of which rois are set
end
% Set Data
setappdata(handles_gui.figure,'num_img',length(imgs)-1);
setappdata(handles_gui.figure,'rois_prelim',rois_prelim);
setappdata(handles_gui.figure,'list_rois',list_rois);
% Update
update_axes('set');
update_sidemenu();
% Format window
set(handles_gui.figure,'Visible','on');
end
function callback_edit_imgnum(hObject,eventdata) %#ok<INUSD>
freeze_menu();
% Get data
num_img = getappdata(handles_gui.figure,'num_img');
% Get img num
num_img_prelim = str2double(get(handles_gui.edit_imgnum,'string'));
if (ncorr_util_isintbb(num_img_prelim,1,length(imgs),'Image number') == out.success)
% num_img_prelim will be zero based indexed
num_img = num_img_prelim-1;
end
% Set data
setappdata(handles_gui.figure,'num_img',num_img);
% Update
update_axes('set');
update_sidemenu();
unfreeze_menu();
end
function callback_button_loadroi(hObject,evendata) %#ok<INUSD>
freeze_menu();
% Get Data
num_img = getappdata(handles_gui.figure,'num_img');
rois_prelim = getappdata(handles_gui.figure,'rois_prelim');
list_rois = getappdata(handles_gui.figure,'list_rois');
% Get ROI
[mask_img_prelim,outstate_roi] = ncorr_util_loadimgs(false);
if (outstate_roi == out.success)
% Convert to binary mask
mask_load_prelim = im2bw(mask_img_prelim.get_gs()); %#ok<IM2BW>
% Make sure ROI and image are the same size and that the ROI
% is nonempty:
if (isequal(size(mask_load_prelim),[imgs(num_img+1).height imgs(num_img+1).width]) && ~isempty(find(mask_load_prelim,1)))
% Form roi_load_prelim
roi_load_prelim = ncorr_class_roi;
roi_load_prelim.set_roi('load',struct('mask',mask_load_prelim,'cutoff',2000));
if (roi_load_prelim.get_fullregions() > 0)
% At this point, roi_load_prelim fits the critera for a
% ROI. Display it to the user.
outstate_roi = ncorr_gui_loadroi(imgs(num_img+1), ...
roi_load_prelim, ...
get(handles_gui.figure,'OuterPosition'));
if (outstate_roi == out.success)
% Set ROI
rois_prelim(num_img+1) = roi_load_prelim;
list_rois(num_img+1) = true;
end
else
h_error = errordlg('ROI must contain a large contiguous region. Small regions are filtered out by default.','Error','modal');
uiwait(h_error);
end
else
h_error = errordlg('ROI was invalid - either not the same size as the reference images or empty.','Error','modal');
uiwait(h_error);
end
end
% Set Data
setappdata(handles_gui.figure,'rois_prelim',rois_prelim);
setappdata(handles_gui.figure,'list_rois',list_rois);
% Update
update_axes('set');
update_sidemenu();
unfreeze_menu();
end
function callback_button_drawroi(hObject,evendata) %#ok<INUSD>
freeze_menu();
% Get Data
num_img = getappdata(handles_gui.figure,'num_img');
rois_prelim = getappdata(handles_gui.figure,'rois_prelim');
list_rois = getappdata(handles_gui.figure,'list_rois');
% Check if ROI was drawn before
params_init_sub = []; % must use sub postfix to prevent overwriting of params_init input
if (list_rois(num_img+1) && strcmp(rois_prelim(num_img+1).type,'draw'))
params_init_sub = rois_prelim(num_img+1).data.drawobjects;
end
[roi_draw_prelim,outstate_roi] = ncorr_gui_drawroi(imgs(num_img+1), ...
get(handles_gui.figure,'OuterPosition'), ...
params_init_sub);
if (outstate_roi == out.success)
% Set ROI
rois_prelim(num_img+1) = roi_draw_prelim;
list_rois(num_img+1) = true;
end
% Set Data
setappdata(handles_gui.figure,'rois_prelim',rois_prelim);
setappdata(handles_gui.figure,'list_rois',list_rois);
% Update
update_axes('set');
update_sidemenu();
unfreeze_menu();
end
function callback_button_finish(hObject,evendata) %#ok<INUSD>
% Get Data
rois_prelim = getappdata(handles_gui.figure,'rois_prelim');
% Set outputs
for i = 0:length(imgs)-1
rois(i+1) = rois_prelim(i+1);
end
outstate = out.success;
% Exit
close(handles_gui.figure);
end
function callback_button_cancel(hObject,evendata) %#ok<INUSD>
close(handles_gui.figure);
end
function callback_button_left(hObject,evendata) %#ok<INUSD>
freeze_menu();
% Get Data
num_img = getappdata(handles_gui.figure,'num_img');
% Check for overshoot
if (num_img > 0)
num_img = num_img-1;
end
% Set Data
setappdata(handles_gui.figure,'num_img',num_img);
% Update
update_axes('set');
update_sidemenu();
unfreeze_menu();
end
function callback_button_right(hObject,evendata) %#ok<INUSD>
freeze_menu();
% Get Data
num_img = getappdata(handles_gui.figure,'num_img');
% Check for overshoot
if (num_img < length(imgs)-1)
num_img = num_img+1;
end
% Set Data
setappdata(handles_gui.figure,'num_img',num_img);
% Update
update_axes('set');
update_sidemenu();
unfreeze_menu();
end
function freeze_menu()
set(handles_gui.button_loadroi,'Enable','off');
set(handles_gui.button_drawroi,'Enable','off');
set(handles_gui.button_cancel,'Enable','off');
set(handles_gui.button_finish,'Enable','off');
end
function unfreeze_menu()
set(handles_gui.button_loadroi,'Enable','on');
set(handles_gui.button_drawroi,'Enable','on');
set(handles_gui.button_cancel,'Enable','on');
end
function update_sidemenu()
% Get data
list_rois = getappdata(handles_gui.figure,'list_rois');
% Enable finish button if all ROIs have been set
if (all(list_rois))
set(handles_gui.button_finish,'Enable','on');
end
% Update text
set(handles_gui.text_menu,'String',[num2str(sum(list_rois)) ' of ' num2str(length(list_rois)) ' ROIs set.']);
end
function update_axes(action)
% Get Data
num_img = getappdata(handles_gui.figure,'num_img');
rois_prelim = getappdata(handles_gui.figure,'rois_prelim');
list_rois = getappdata(handles_gui.figure,'list_rois');
if (strcmp(action,'set'))
% Show preview
if (list_rois(num_img+1))
% Paint ROI over images
preview_roi = imgs(num_img+1).get_gs();
preview_roi(rois_prelim(num_img+1).mask) = preview_roi(rois_prelim(num_img+1).mask)+imgs(num_img+1).max_gs;
imshow(preview_roi,[imgs(num_img+1).min_gs 2*imgs(num_img+1).max_gs],'Parent',handles_gui.axes_preview);
set(handles_gui.axes_preview,'Visible','off');
else
% Paint only images
imshow(imgs(num_img+1).get_gs(),[imgs(num_img+1).min_gs 2*imgs(num_img+1).max_gs],'Parent',handles_gui.axes_preview);
set(handles_gui.axes_preview,'Visible','off');
end
% Format/show text
set(handles_gui.text_name,'String',['Name: ' imgs(num_img+1).name(1:min(end,22))]);
% Set buttons/editboxes
set(handles_gui.edit_imgnum,'String',num2str(num_img+1));
if (length(imgs) == 1)
set(handles_gui.button_left,'Enable','off');
set(handles_gui.button_right,'Enable','off');
set(handles_gui.edit_imgnum,'Enable','off');
elseif (num_img == 0)
set(handles_gui.button_left,'Enable','off');
set(handles_gui.button_right,'Enable','on');
set(handles_gui.edit_imgnum,'Enable','on');
elseif (num_img == length(imgs)-1)
set(handles_gui.button_left,'Enable','on');
set(handles_gui.button_right,'Enable','off');
set(handles_gui.edit_imgnum,'Enable','on');
else
set(handles_gui.button_left,'Enable','on');
set(handles_gui.button_right,'Enable','on');
set(handles_gui.edit_imgnum,'Enable','on');
end
end
end
function handles_gui = init_gui()
% GUI controls -------------------------------------------------------%
% Figure
handles_gui.figure = figure( ...
'Tag', 'figure', ...
'Units', 'characters', ...
'Position', ncorr_util_figpos(pos_parent,[35 126.7]), ...
'Name', 'Draw ROI', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Color', get(0,'DefaultUicontrolBackgroundColor'), ...
'handlevisibility','off', ...
'DockControls','off', ...
'Resize','off', ...
'WindowStyle','modal', ...
'Visible','off', ...
'IntegerHandle','off', ...
'Interruptible','off');
% Panels
handles_gui.group_roioptions = uipanel( ...
'Parent', handles_gui.figure, ...
'Tag', 'group_roioptions', ...
'Units', 'characters', ...
'Position', [2 26.1 35 8.3], ...
'Title', 'ROI Options', ...
'Interruptible','off');
handles_gui.group_menu = uipanel( ...
'Parent', handles_gui.figure, ...
'Tag', 'group_menu', ...
'Units', 'characters', ...
'Position', [2 18.6 35 6.7], ...
'Title', 'Menu', ...
'Interruptible','off');
handles_gui.group_preview = uipanel( ...
'Parent', handles_gui.figure, ...
'Tag', 'group_preview', ...
'Units', 'characters', ...
'Position', [38.7 0.75 85.8 33.6], ...
'Title', 'ROI', ...
'Interruptible','off');
% Axes
handles_gui.axes_preview = axes( ...
'Parent', handles_gui.group_preview, ...
'Tag', 'axes_preview', ...
'Units', 'characters', ...
'Position', [2.4 4 79.7 27.6]', ...
'Visible','off', ...
'Interruptible','off');
% Static Texts
handles_gui.text_menu = uicontrol( ...
'Parent', handles_gui.group_roioptions, ...
'Tag', 'text_menu', ...
'Style', 'text', ...
'Units', 'characters', ...
'Position', [2.3 5.0 23.5 1.3], ...
'String', ['0 of ' num2str(length(imgs)) ' ROIs set.'], ...
'HorizontalAlignment', 'left', ...
'Interruptible','off');
handles_gui.text_name = uicontrol( ...
'Parent', handles_gui.group_preview, ...
'Tag', 'text_name', ...
'Style', 'text', ...
'Units', 'characters', ...
'Position', [4 1.3 56.9 1.3], ...
'String', 'Name: ', ...
'HorizontalAlignment', 'left', ...
'Interruptible','off');
% Editbox
handles_gui.edit_imgnum = uicontrol( ...
'Parent', handles_gui.group_preview, ...
'Tag', 'edit_imgnum', ...
'Style', 'edit', ...
'Units', 'characters', ...
'Position', [68.1 1.3 7 1.6], ...
'String', '', ...
'Callback', ncorr_util_wrapcallbacktrycatch(@callback_edit_imgnum,handles_gui.figure), ...
'Enable', 'off', ...
'Interruptible','off');
% Push Buttons
handles_gui.button_loadroi = uicontrol( ...
'Parent', handles_gui.group_roioptions, ...
'Tag', 'loadroi', ...
'Style', 'pushbutton', ...
'Units', 'characters', ...
'Position', [2.1 3 29.7 1.7], ...
'String', 'Load ROI', ...
'Callback', ncorr_util_wrapcallbacktrycatch(@callback_button_loadroi,handles_gui.figure), ...
'Interruptible','off');
handles_gui.button_drawroi = uicontrol( ...
'Parent', handles_gui.group_roioptions, ...
'Tag', 'draw', ...
'Style', 'pushbutton', ...
'Units', 'characters', ...
'Position', [2.1 1 29.7 1.7], ...
'String', 'Draw ROI', ...
'Callback', ncorr_util_wrapcallbacktrycatch(@callback_button_drawroi,handles_gui.figure), ...
'Interruptible','off');
handles_gui.button_finish = uicontrol( ...
'Parent', handles_gui.group_menu, ...
'Tag', 'finish', ...
'Style', 'pushbutton', ...
'Units', 'characters', ...
'Position', [2.1 3 29.7 1.7], ...
'String', 'Finish', ...
'Enable', 'off', ...
'Callback', ncorr_util_wrapcallbacktrycatch(@callback_button_finish,handles_gui.figure), ...
'Interruptible','off');
handles_gui.button_cancel = uicontrol( ...
'Parent', handles_gui.group_menu, ...
'Tag', 'draw', ...
'Style', 'pushbutton', ...
'Units', 'characters', ...
'Position', [2.1 1 29.7 1.7], ...
'String', 'Cancel', ...
'Callback', ncorr_util_wrapcallbacktrycatch(@callback_button_cancel,handles_gui.figure), ...
'Interruptible','off');
handles_gui.button_left = uicontrol( ...
'Parent', handles_gui.group_preview, ...
'Tag', 'button_left', ...
'Style', 'pushbutton', ...
'Units', 'characters', ...
'Position', [61.1 1.2 6 1.8], ...
'String', '<', ...
'Callback', ncorr_util_wrapcallbacktrycatch(@callback_button_left,handles_gui.figure), ...
'Interruptible','off', ...
'Enable', 'off');
handles_gui.button_right = uicontrol( ...
'Parent', handles_gui.group_preview, ...
'Tag', 'button_right', ...
'Style', 'pushbutton', ...
'Units', 'characters', ...
'Position', [76.1 1.2 6 1.8], ...
'String', '>', ...
'Callback', ncorr_util_wrapcallbacktrycatch(@callback_button_right,handles_gui.figure), ...
'Interruptible','off', ...
'Enable', 'off');
end
% Pause until figure is closed ---------------------------------------%
waitfor(handles_gui.figure);
end