forked from tsumpf/arrShow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasRoiClass.m
302 lines (247 loc) · 10.6 KB
/
asRoiClass.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
classdef asRoiClass < impoly
properties (Access = private)
parentAxesHandle = 0; % parent axes handle
parentImageHandle = 0; % parent image handle
parentFigureHandle = 0;
textHandle = [];
textContextMenuHandle = [];
precision = '%g'; % standard notation is compact
filterStr = '' % WARNING: test
guiHandle = struct('figure','text')
cmenuHandle = struct('sendPosition','ignoreZeros');
sendPositionCallback = [];
sendPositionCallbackId = [];
fullStrToggle = true; % show full string or just compact form
end
properties (Access = public)
end
methods
function obj = asRoiClass(parentAxesHandle, roiPos, sendPositionCallback)
if nargin < 2
roiPos = [];
if nargin < 1
parentAxesHandle = gca;
end
end
obj = obj@impoly(parentAxesHandle, roiPos);
obj.setColor('green')
obj.parentAxesHandle = parentAxesHandle;
obj.parentImageHandle = findall(get(parentAxesHandle,'Children'),'Type','image');
obj.parentFigureHandle = get(get(obj.parentAxesHandle,'Parent'),'Parent');
if nargin == 3
obj.sendPositionCallback = sendPositionCallback;
end
obj.updateImpolyContextMenu;
obj.createTextContextMenu;
% addNewPositionCallback(obj,@(pos)obj.showRoiGui);
% obj.showRoiGui;
addNewPositionCallback(obj,@(pos)obj.updateRoiString);
obj.updateRoiString;
end
function roi = getRoiData(obj)
ud = get(obj.parentAxesHandle,'UserData');
if isempty(ud)
refImg = get(obj.parentImageHandle,'CData');
else
if isfield(ud,'isComplex') && ud.isComplex
refImg = ud.cplxImg;
else
refImg = get(obj.parentImageHandle,'CData');
end
end
mask = obj.createMask;
roi = refImg(mask == 1);
if obj.getIgnoreZerosToggle
roi = roi(roi~=0);
end
if ~isempty(obj.filterStr)
eval(['roi = roi(roi',obj.filterStr,');']);
end
end
function copyPosition(obj)
clipboard('copy',asRoiClass.pos2lineStr(obj.getPosition()));
end
function [m, s] = getMeanAndStd(obj)
roi = obj.getRoiData;
if ~isempty(roi)
m = mean(roi);
s = std(roi);
else
m = 0;
s = 0;
end
end
function N = getN(obj)
roi = obj.getRoiData;
N = numel(roi);
end
function mini = getMin(obj)
mini = min(obj.getRoiData);
end
function maxi = getMax(obj)
maxi = max(obj.getRoiData);
end
function updateRoiString(obj)
if obj.fullStrToggle
obj.drawFullString;
else
obj.drawMeanAndStdString;
end
end
function str = getMeanAndStdString(obj)
[m, s] = obj.getMeanAndStd;
str = [num2str(m,obj.precision), ' +- ', num2str(s,obj.precision)];
end
function drawMeanAndStdString(obj)
% writes roi statistics as a text in the image window
if ~isempty(obj.textHandle)
delete(obj.textHandle);
end
obj.textHandle = text(.015,.99,obj.getMeanAndStdString,'Units','normalized','parent',obj.parentAxesHandle,...
'Color','green','BackgroundColor','Black',...
'VerticalAlignment','top',...
'UIContextMenu',obj.textContextMenuHandle);
end
function drawFullString(obj)
% writes roi statistics as a text in the image window
if ~isempty(obj.textHandle)
delete(obj.textHandle);
end
str = strvcat(obj.getMeanAndStdString,num2str(obj.getN),num2str(obj.getMin,obj.precision),num2str(obj.getMax,obj.precision));
obj.textHandle = text(.015,.98,str,'Units','normalized','parent',obj.parentAxesHandle,...
'Color','green','BackgroundColor','Black',...
'VerticalAlignment','top',...
'UIContextMenu',obj.textContextMenuHandle);
end
function h = getTextHandle(obj)
h = obj.textHandle;
end
function bool = guiIsPresent(obj)
bool = false;
if ishandle(obj.guiHandle.figure)
if strcmp(get(obj.guiHandle.figure,'Tag') ,'roiGui')
bool = true;
end
end
end
function showRoiGui(obj)
if obj.guiIsPresent
set(obj.guiHandle.text,'String',obj.getMeanAndStdString);
else
pos = [800, 800, 40, 40];
str = obj.getMeanAndStdString;
obj.guiHandle.figure = figure('MenuBar','none','Toolbar','none',...
'Position',pos, 'Tag', 'roiGui');
obj.guiHandle.text = uicontrol('Style','Text','String',str,'HorizontalAlignment','left',...
'Units','normalized','pos',[0 0 1 1],...
'parent',obj.guiHandle.figure,'HandleVisibility','on',...
'FontUnits','normalized','FontSize',.35);
end
end
function delete(obj)
if obj.guiIsPresent
close(obj.guiHandle.figure);
end
if ~isempty(obj.textHandle) && ishandle(obj.textHandle)
delete(obj.textHandle);
end
end
function toggle = getSendPositionToggle(obj)
switch get(obj.cmenuHandle.sendPosition,'Checked')
case 'on'
toggle = true;
case 'off'
toggle = false;
end
end
function toggle = getIgnoreZerosToggle(obj)
switch get(obj.cmenuHandle.ignoreZeros,'Checked')
case 'on'
toggle = true;
case 'off'
toggle = false;
end
end
function setSendPositionToggle(obj, toggle)
if nargin < 2
toggle = ~obj.getSendPositionToggle;
end
switch toggle
case 1
set(obj.cmenuHandle.sendPosition,'Checked','on');
obj.sendPositionCallbackId = addNewPositionCallback(obj,obj.sendPositionCallback);
obj.callSendPositionCallback(); % execute callback once
case 0
set(obj.cmenuHandle.sendPosition,'Checked','off');
removeNewPositionCallback(obj,obj.sendPositionCallbackId);
end
end
function addFilterString(obj,str)
% WARNING, INCOMPLETE IMPLEMENTATION
obj.filterStr = str;
obj.updateRoiString;
end
function callSendPositionCallback(obj)
obj.sendPositionCallback(obj.getPosition);
end
function setIgnoreZerosToggle(obj, toggle)
if nargin < 2
toggle = ~obj.getIgnoreZerosToggle;
end
switch toggle
case 1
set(obj.cmenuHandle.ignoreZeros,'Checked','on');
case 0
set(obj.cmenuHandle.ignoreZeros,'Checked','off');
end
obj.updateRoiString;
end
end
methods (Access = private)
function updateImpolyContextMenu(obj)
% add some features to the impoly context menu
cmh = obj.getContextMenu;
uimenu(cmh,'Label','Delete ROI' ,...
'callback',@(src,evnt)obj.delete);
uimenu(cmh,'Label','Delete all ROIs' ,...
'callback',@(src,evnt)evalin('base','asDeleteAllRois'));
if ~isempty(obj.sendPositionCallback)
obj.cmenuHandle.sendPosition = uimenu(cmh,'Label','Send Position' ,...
'callback',@(src,evnt)obj.setSendPositionToggle);
end
obj.cmenuHandle.ignoreZeros = uimenu(cmh,'Label','Ignore Zeros' ,...
'callback',@(src,evnt)obj.setIgnoreZerosToggle,...
'Checked','on');
end
function createTextContextMenu(obj)
% create a context menu to choose between different notations
% in the image text
if isempty(obj.textContextMenuHandle)
obj.textContextMenuHandle = uicontextmenu('Parent',obj.parentFigureHandle);
uimenu(obj.textContextMenuHandle,'Label','Decimal notation' ,...
'callback',@(src,evnt)obj.setNotation('%d'));
uimenu(obj.textContextMenuHandle,'Label','Fixed-point notation' ,...
'callback',@(src,evnt)obj.setNotation('%2.2f'));
uimenu(obj.textContextMenuHandle,'Label','Exponential notation' ,...
'callback',@(src,evnt)obj.setNotation('%2.2e'));
uimenu(obj.textContextMenuHandle,'Label','Compact exponential notation' ,...
'callback',@(src,evnt)obj.setNotation('%g'));
end
end
function setNotation(obj, precision)
obj.precision = precision;
obj.updateRoiString % update text
end
end
methods (Static, Access = private)
function str = pos2lineStr(pos)
% needed to copy the position array to clipboard
ncols = size(pos,1);
str = num2str(pos(1,:));
for i = 2 : ncols
curr = num2str(pos(i,:));
str = [str,';',curr];
end
end
end
end