forked from tsumpf/arrShow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
asMarkerClass.m
530 lines (449 loc) · 20.1 KB
/
asMarkerClass.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
classdef asMarkerClass < handle
% The class manages and draws "pixel markers", i.e. it marks specific
% pixel positions in different slices. Its main intention is to
% highlight critical image regions which may contain automatically
% detected image reconstruction errors etc. It can also be used to
% highlight general pixels of interest. The markers are automatically
% redrawn if the selected image changes.
%
% Pixel markers can be added for either all slices (by calling the .set
% or .add method with a matrix of pixel positions) or for individual
% slices (by calling the .set or .add method with a cell array of
% matrices of pixel positions, or by calling the .setInCurrentFrames
% method)
%
properties (Access = private)
pos = []; % marker positions
axesHandles = [];
ignoredDimensions = []; % dimensions where the position cell vector is 1
markerHandles = {}; % (its probably actually marker "objects" since matlab 2014b)
selection = []; % the asSelectionCass (used to determine selected frames)
color = 'yellow'; % default color
style = 1; % 1 = impoint, 2 = rectange
uiMenuHandle = []; % context menu handle from the main arrShow window
apply2allCb = []; % send to all relatives callback
end
methods (Access = public)
function obj = asMarkerClass(selection, markerPositions, uiMenuBase, apply2allCb)
% constructor
obj.selection = selection;
if nargin > 1 && ~isempty(markerPositions)
obj.pos = obj.parsePos(markerPositions);
end
obj.apply2allCb = apply2allCb;
% populate ui menu
obj.uiMenuHandle.base = uiMenuBase;
% Show / hide
obj.uiMenuHandle.showMarker = uimenu(uiMenuBase,'Label','Show' ,...
'callback',@(src,evnt)obj.toggleVisibility(),...
'checked','on');
% Style
obj.uiMenuHandle.style = uimenu(uiMenuBase,'Label','Style');
uimenu(obj.uiMenuHandle.style,'Label','Impoint' ,...
'callback',@(src,evnt)obj.setStyle(1));
uimenu(obj.uiMenuHandle.style,'Label','Rectangle' ,...
'callback',@(src,evnt)obj.setStyle(2));
% add...
uimenu(uiMenuBase,'Label','Add global' ,...
'callback',@(src,evnt)obj.add(),'separator','on');
uimenu(uiMenuBase,'Label','Add to current frames' ,...
'callback',@(src,evnt)obj.addToCurrentFrames());
% send...
uimenu(uiMenuBase,'Label','Send all' ,...
'callback',@(src,evnt)obj.send('all'),'separator','on');
uimenu(uiMenuBase,'Label','Send current' ,...
'callback',@(src,evnt)obj.send('current'));
% clear
uimenu(uiMenuBase,'Label','Clear' ,...
'callback',@(src,evnt)obj.clear(),'separator','on');
end
function updateAxesHandles(obj, axesHandles)
obj.axesHandles = axesHandles;
obj.draw();
end
function add(obj, pos)
% adds markers at positions pos.
% pos can be either a matrix of positions for all frames or a
% cell array of matrices with positions for individual frames.
% The cell array must have the same number of dimensions as
% the arrayShow data array. The dimensions sizes must be either
% equal or 1. Dimensions of size 1 are ment to be used for
% image or colon dimensions.
%
% E.g.: for a stack of images or an image matrix of
% 256 x 256 x 20 x 15 pixels, a cell array of size
% 1 x 1 x 20 x 1 can be used to hold pixel markers that are
% equal for the 4th dimension and different for the 3rd
% dimension.
% if no position vector is given, open input dialog
if nargin < 2 || isempty(pos)
pos = asMarkerClass.getPosFromUi();
if isempty(pos)
return;
end
end
pos = obj.parsePos(pos);
if iscell(obj.pos)
if iscell(pos)
% if the new and the present positions are cell vectors
% of the same size: just combine them
if length(pos) ~= length(obj.pos)
error('lalala');
end
for i = 1 : length(pos)
obj.pos{i} = [obj.pos{i}, pos{i}];
end
else
% if the present positions are cells and the new are
% not, add the new positions to all frames
for i = 1 : length(obj.pos)
obj.pos{i} = [obj.pos{i}, pos];
end
end
else
% ..the present positions are not cells
if iscell(pos)
for i = 1 : length(pos)
pos{i} = [obj.pos, pos{i}];
end
obj.pos = pos;
else
obj.pos = [obj.pos, pos];
end
end
obj.redraw();
end
function bool = getVisibility(obj)
bool = arrShow.onOffToBool(get(obj.uiMenuHandle.showMarker,'checked'));
end
function toggleVisibility(obj)
bool = obj.getVisibility;
obj.setVisibility(~bool);
end
function setVisibility(obj, toggle)
set(obj.uiMenuHandle.showMarker,'checked', arrShow.boolToOnOff(toggle));
if toggle
obj.draw();
else
obj.deleteMarkers();
end
end
function pos = get(obj)
% get pixel marer positions
pos = obj.pos;
end
function addToCurrentFrames(obj, newPos)
% if no position vector is given, open input dialog
if nargin < 2 || isempty(newPos)
newPos = asMarkerClass.getPosFromUi();
if isempty(newPos)
return;
end
end
% assure that obj.pos is initialized as a cell array
if ~iscell(obj.pos) || isempty(obj.pos)
obj.initPosCellArray();
end
% get the current positions in the current frames
currPos = obj.getInCurrentFrames();
% assure that the new positions are in a legal format
expectedNumel = length(currPos);
newPos = obj.parsePos(newPos, expectedNumel);
if iscell(newPos)
if length(currPos) ~= length(newPos)
disp('Length of the position cell vector has to match the number of selected frames');
return;
end
% add the new positions to all selected frames
for i = 1 : length(currPos)
currPos{i} = [currPos{i}, newPos{i}];
end
else
% add the the same new positions to all selected frames
for i = 1 : length(currPos)
currPos{i} = [currPos{i}, newPos];
end
end
% set the new positions
obj.setInCurrentFrames(currPos, false);
end
function setInCurrentFrames(obj, newPos, parsePos)
if nargin < 3 || isempty(parsePos)
parsePos = true;
end
% assure that obj.pos is initialized as a cell array
if ~iscell(obj.pos) || isempty(obj.pos)
obj.initPosCellArray();
end
% get number of selected frames
if parsePos
expectedNumel = numel(obj.getInCurrentFrames());
newPos = obj.parsePos(newPos, expectedNumel);
end
if ~iscell(newPos)
newPos = {newPos};
end
% get the subscripts for the selected frames
S.subs = obj.selection.getValueAsCell(false);
S.type = '()';
% set selection in the ignored dimensions to 1
S.subs(obj.ignoredDimensions) = repmat({1},[1,length(obj.ignoredDimensions)]);
% update the marker positions for the selected frames
obj.pos = subsasgn(obj.pos,S,newPos);
% "re-draw"
obj.redraw();
end
function pos = getInCurrentFrames(obj)
if iscell(obj.pos)
% get selected frames
S.subs = obj.selection.getValueAsCell(false);
S.type = '()';
% set selection in the ignored dimensions to 1
S.subs(obj.ignoredDimensions) = repmat({1},[1,length(obj.ignoredDimensions)]);
% get the marker positions for the selected frames
pos = squeeze(subsref(obj.pos,S));
else
pos = obj.pos;
end
end
function markerHandle = getHandles(obj)
markerHandle = obj.markerHandles;
end
function set(obj, pos)
if nargin < 2
pos = [];
end
% parse and store positions in the object properties
obj.pos = obj.parsePos(pos);
% redraw markers
obj.redraw();
end
function clear(obj)
% permanently removes all marker objects and positions
obj.deleteMarkers();
obj.pos = [];
end
function draw(obj)
if isempty(obj.pos) || obj.getVisibility == false
return;
end
if iscell(obj.pos)
% get the marker positions for the selected frames
selPos = obj.getInCurrentFrames();
if length(selPos) ~= length(obj.axesHandles)
disp('Cannot show markers for the current dimensions');
else
% loop over all axes and create the markers
nAxes = length(obj.axesHandles);
obj.markerHandles = cell(nAxes,1);
for i = 1 : nAxes
obj.markerHandles{i} = obj.drawAtAxes(obj.axesHandles(i), selPos{i});
end
end
else
% we have positions for a single frame. Apply it on all
% axes...
nAxes = length(obj.axesHandles);
obj.markerHandles = cell(nAxes);
% ...loop over availables axes
for i = 1 : nAxes
ah = obj.axesHandles(i);
obj.markerHandles{i} = obj.drawAtAxes(ah, obj.pos);
end
end
end
function setStyle(obj, style)
% style can currently be either impoint or rectangle
if isempty(style)
return;
end
switch style
case {1, 'impoint', 'point'}
obj.style = 1;
case {2, 'rect', 'rectangle'}
obj.style = 2;
otherwise
error('asMarkerClass:setStyle','Unsupported marker style');
end
obj.redraw();
end
function style = getStyle(obj, returnAsNumber)
if nargin < 2
returnAsNumber = false;
end
if returnAsNumber
style = obj.style;
else
switch(obj.style)
case 1
style = 'impoint';
case 2
style = 'rectangle';
end
end
end
function setColor(obj, col)
if nargin < 2 || isempty(col) || strcmp(col,obj.color)
% if the given color already equals the color in the
% object properties, we probably don't need update anything
% and we can just return
return;
end
obj.color = col;
obj.redraw();
end
function send(obj, allOrCurrent)
if nargin < 2 || isempty(allOrCurrent)
allOrCurrent = 'all';
end
switch allOrCurrent
case 'all'
obj.apply2allCb('markers.add',false,obj.get());
case 'current'
obj.apply2allCb('markers.addToCurrentFrames',false,obj.getInCurrentFrames());
end
end
end
% methods (Access = protected)
% end
methods (Access = private)
function initPosCellArray(obj)
% try to initialize a position cell array assuming the colon
% dimensions as "image dimensions" and all other dimensions as
% frames
if iscell(obj.pos) && ~isempty(obj.pos)
error('obj.pos already seems to be initialized');
end
% get the data size
dataDims = obj.selection.getDimensions;
% ignore the colon dimension by default
obj.ignoredDimensions = obj.selection.getColonDims;
dataDims(obj.ignoredDimensions) = 1;
if isempty(obj.pos)
obj.pos = cell(dataDims);
else
% duplicate the position to all cell entries
tmpBackup = obj.pos;
obj.pos = cell(dataDims);
[obj.pos{:}] = deal(tmpBackup);
end
end
function markerHandles = drawAtAxes(obj, ah, pos)
nMarkersPerAxes = size(pos,2);
markerHandles = cell(nMarkersPerAxes,1);
for i = 1 : nMarkersPerAxes
P = pos(:,i);
switch obj.style
case 1
% impoint
markerHandles{i} = impoint(ah,P(2),P(1));
markerHandles{i}.setColor(obj.color);
case 2
% rectangle
markerHandles{i} = rectangle('Parent',ah,'Position',[P(2)-.5, P(1)-.5, 1,1],'Curvature',[0,0],...
'HitTest','off','EdgeColor',obj.color);
otherwise
error('asMarkerClass:drawAtAxes','Unsupported marker style');
end
end
end
function deleteMarkers(obj)
for i = 1 : length(obj.markerHandles)
cellfun(@delete,obj.markerHandles{i});
end
obj.markerHandles = {};
end
function redraw(obj)
obj.deleteMarkers()
obj.draw();
end
function pos = parsePos(obj, pos, expectedNumel)
% checks if the position vector matches the expected format. If
% pos is a cell array and expectedNumel is empty, the array is
% checked to match the data dimension. If expectedNumel is
% given, the pos array is just checkt for the correct number of
% elements.
%
% If pos is a cell vector, its length is checked against the
% size of the different data dimensions and reshaped to fit the
% first match. E.g. If the data dimensions are 128x128x20x30
% and size(pos) = 20x1 or 1x20, pos is reshaped to 1x1x20x1.
if iscell(pos)
if nargin > 2 && ~isempty(expectedNumel)
if numel(pos) ~= expectedNumel
error('asMarkerClass:invalidDimensions','Number of elements in the new position vector are not valid');
end
else
% check the size of the cell array
dataDims = obj.selection.getDimensions;
siPos = size(pos);
lPos = length(siPos);
lDat = length(dataDims);
if lPos < lDat
% ok, the dimensions of the data and the position cell
% array are not equal. Check, if pos is a vector
% and can be matched with any of the data
% dimensions
if isvector(pos)
matchingDims = find(dataDims == numel(pos));
if isempty(matchingDims)
error('asMarkerClass:invalidDimensions','Marker position vector has invalid dimensions');
end
% define a new position matrix size with the
% elements put into the first matching dim
siPos = ones(1,lDat);
siPos(matchingDims(1)) = numel(pos);
pos = reshape(pos,siPos);
else
% pos is neither a vector nor does it have the
% same number of dimensions as the data dims.
% Try to padd all non given trailing dims with
% 1 to get equal siPos und dataDims vector size
siPos = [siPos, ones(1,lDat - lPos)];
end
end
% define all dimensions with size == 1 as
% "ignoredDimensions"
obj.ignoredDimensions = find(siPos == 1);
% check for unequal dimensions
if length(siPos) == length(dataDims)
unequalDims = find(siPos ~= dataDims);
else
error('asMarkerClass:invalidDimensions','Marker position vector has invalid dimensions');
end
% check, if the cell entries in the unequal dimensions
% are 1
if length(unequalDims) > length(obj.ignoredDimensions)||...
any(unequalDims ~= obj.ignoredDimensions)
error('asMarkerClass:invalidDimensions','Marker position vector has invalid dimensions');
end
end
% assure that all antries are column vectors
for i = 1 : length(pos)
if isrow(pos{i})
pos{i} = pos{i}';
end
end
else
if isrow(pos)
pos = pos';
end
end
end
end
methods(Static)
function pos = getPosFromUi()
initVal = '0,0';
pos = mydlg('Enter selection string','Selection input dlg',initVal);
if isempty(pos)
return;
end
try
pos = str2num(pos); %#ok<ST2NM>
catch
pos = [];
fprintf('Invalid position vector\n');
end
end
end
end