-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsphereAnnotationTool.m
More file actions
374 lines (323 loc) · 16.9 KB
/
sphereAnnotationTool.m
File metadata and controls
374 lines (323 loc) · 16.9 KB
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
classdef sphereAnnotationTool < handle
properties
Figure
Axis
PlaneHandle
PlaneIndex
PlaneLabel
NPlanes
Volume
Dialog
LowerThreshold
UpperThreshold
LowerThresholdSlider
UpperThresholdSlider
ClassIndex
HLineHandle
VLineHandle
Center
XSlider
YSlider
RSlider
SliderZ
XMarkers
YMarkers
Radius
MaxRadius
SphereID
Spheres
Circles
CirclesHandle
SecondChannel
ShowingFirstChannel
MouseIsDown
Row0
Col0
end
methods
function tool = sphereAnnotationTool(V,maxRadius,nClasses,varargin)
% see annotateSpheres.m for a demo
tool.ShowingFirstChannel = true;
if nargin > 3
tool.SecondChannel = varargin{1};
end
tool.Volume = V;
tool.NPlanes = size(V,3);
tool.PlaneIndex = 1;
tool.ClassIndex = 1;
tool.LowerThreshold = 0;
tool.UpperThreshold = 1;
tool.Radius = 10;
tool.MaxRadius = maxRadius;
tool.SphereID = 0;
tool.Spheres = [];
tool.Circles = cell(1,tool.NPlanes);
% z
tool.Figure = figure('Name',sprintf('Frame %d',tool.PlaneIndex),...
'NumberTitle','off','CloseRequestFcn',@tool.closeTool,'KeyPressFcn',@tool.keyPressed,...
'WindowButtonMotionFcn', @tool.mouseMove, 'WindowButtonDownFcn', @tool.mouseDown, 'WindowButtonUpFcn', @tool.mouseUp);%, ...
% 'windowscrollWheelFcn', @tool.mouseScroll);
tool.Axis = axes('Parent',tool.Figure,'Position',[0 0 1 1]);
I = tool.Volume(:,:,tool.PlaneIndex);
tool.PlaneHandle = imshow(tool.applyThresholds(I)); hold on;
tool.Center = [size(V,2)/2 size(V,1)/2]; % x,y
tool.HLineHandle = plot([1 size(V,2)],tool.Center(2)*[1 1],'r');
tool.XMarkers{1} = plot((tool.Center(1)-tool.Radius)*[1 1],tool.Center(2)+[-5 5],'b');
tool.XMarkers{2} = plot((tool.Center(1)+tool.Radius)*[1 1],tool.Center(2)+[-5 5],'b');
tool.VLineHandle = plot(tool.Center(1)*[1 1],[1 size(V,1)],'r');
tool.YMarkers{1} = plot(tool.Center(1)+[-5 5],(tool.Center(2)-tool.Radius)*[1 1],'b');
tool.YMarkers{2} = plot(tool.Center(1)+[-5 5],(tool.Center(2)+tool.Radius)*[1 1],'b');
dwidth = 500;
dborder = 10;
cwidth = dwidth-2*dborder;
cheight = 20;
tool.Dialog = dialog('WindowStyle', 'normal', 'Resize', 'on',...
'Name', 'SphereAnnotationTool',...
'CloseRequestFcn', @tool.closeTool,...
'Position',[100 100 dwidth 12.5*dborder+11.5*cheight],...
'KeyPressFcn',@tool.keyPressed);
% class popup
labels = cell(1,nClasses);
for i = 1:nClasses
labels{i} = sprintf('Class %d',i);
end
uicontrol('Parent',tool.Dialog,'Style','popupmenu','String',labels,'Position', [dborder+20 11.5*dborder+10.5*cheight cwidth-20 cheight],'Callback',@tool.popupManage);
% x slider
uicontrol('Parent',tool.Dialog,'Style','text','String','x','Position',[dborder 10*dborder+9*cheight 20 cheight]);
tool.XSlider = uicontrol('Parent',tool.Dialog,'Style','slider','Min',1,'Max',size(V,2),'Value',tool.Center(1),'Position',[dborder+20 10*dborder+9*cheight cwidth-20 cheight],'Tag','xs');
addlistener(tool.XSlider,'Value','PostSet',@tool.continuousSliderManage);
% y slider
uicontrol('Parent',tool.Dialog,'Style','text','String','y','Position',[dborder 9*dborder+8*cheight 20 cheight]);
tool.YSlider = uicontrol('Parent',tool.Dialog,'Style','slider','Min',1,'Max',size(V,1),'Value',tool.Center(2),'Position',[dborder+20 9*dborder+8*cheight cwidth-20 cheight],'Tag','ys');
addlistener(tool.YSlider,'Value','PostSet',@tool.continuousSliderManage);
% r slider
uicontrol('Parent',tool.Dialog,'Style','text','String','r','Position',[dborder 8*dborder+7*cheight 20 cheight]);
tool.RSlider = uicontrol('Parent',tool.Dialog,'Style','slider','Min',1,'Max',tool.MaxRadius,'Value',tool.Radius,'Position',[dborder+20 8*dborder+7*cheight cwidth-20 cheight],'Tag','rs');
addlistener(tool.RSlider,'Value','PostSet',@tool.continuousSliderManage);
% add/remove buttons
uicontrol('Parent',tool.Dialog,'Style','pushbutton','String','add','Position',[dborder+20 7*dborder+6*cheight (cwidth-20)/2-10 cheight],'Callback',@tool.buttonPushed);
uicontrol('Parent',tool.Dialog,'Style','pushbutton','String','rem','Position',[dborder+20+(cwidth-20)/2+10 7*dborder+6*cheight (cwidth-20)/2-10 cheight],'Callback',@tool.buttonPushed);
% z slider
uicontrol('Parent',tool.Dialog,'Style','text','String','f','Position',[dborder 5*dborder+4*cheight 20 cheight]);
tool.PlaneLabel = uicontrol('Parent',tool.Dialog,'Style','text','String',sprintf('%d',tool.PlaneIndex),'Position',[dwidth-dborder-40 5*dborder+3*cheight 40 cheight],'HorizontalAlignment','Right');
tool.SliderZ = uicontrol('Parent',tool.Dialog,'Style','slider','Min',1,'Max',tool.NPlanes,'Value',tool.PlaneIndex,'Position',[dborder+20 5*dborder+4*cheight cwidth-20 cheight],'Tag','zs');
addlistener(tool.SliderZ,'Value','PostSet',@tool.continuousSliderManage);
% lower threshold slider
uicontrol('Parent',tool.Dialog,'Style','text','String','_t','Position',[dborder 4*dborder+2*cheight 20 cheight]);
tool.LowerThresholdSlider = uicontrol('Parent',tool.Dialog,'Style','slider','Min',0,'Max',1,'Value',tool.LowerThreshold,'Position',[dborder+20 4*dborder+2*cheight cwidth-20 cheight],'Tag','lts');
addlistener(tool.LowerThresholdSlider,'Value','PostSet',@tool.continuousSliderManage);
% upper threshold slider
uicontrol('Parent',tool.Dialog,'Style','text','String','^t','Position',[dborder 3*dborder+cheight 20 cheight]);
tool.UpperThresholdSlider = uicontrol('Parent',tool.Dialog,'Style','slider','Min',0,'Max',1,'Value',tool.UpperThreshold,'Position',[dborder+20 3*dborder+cheight cwidth-20 cheight],'Tag','uts');
addlistener(tool.UpperThresholdSlider,'Value','PostSet',@tool.continuousSliderManage);
% uiwait(tool.Dialog)
% done button
uicontrol('Parent',tool.Dialog,'Style','pushbutton','String','Done','Position',[dborder dborder cwidth cheight],'Callback',@tool.buttonDonePushed);
tool.MouseIsDown = false;
end
function mouseScroll(tool,src,callbackdata)
vsc = callbackdata.VerticalScrollCount;
if strcmp(src.Name(1:5),'Frame')
if (vsc == -1 && tool.SliderZ.Value > tool.SliderZ.Min) || ...
(vsc == 1 && tool.SliderZ.Value < tool.SliderZ.Max)
tool.SliderZ.Value = tool.SliderZ.Value+vsc;
end
tag = 'zs';
val = tool.SliderZ.Value;
end
callbackdata = [];
callbackdata.AffectedObject.Tag = tag;
callbackdata.AffectedObject.Value = val;
continuousSliderManage(tool,0,callbackdata);
end
function mouseDown(tool,~,~)
p = tool.Axis.CurrentPoint;
col = round(p(1,1));
row = round(p(1,2));
if row >= 1 && row <= size(tool.Volume,1) && col >= 1 && col <= size(tool.Volume,2)
tool.Row0 = row;
tool.Col0 = col;
tool.RSlider.Value = 1;
tool.MouseIsDown = true;
end
end
function mouseUp(tool,~,~)
if tool.MouseIsDown
src.String = 'add';
buttonPushed(tool,src,[]);
tool.RSlider.Value = 10;
tool.MouseIsDown = false;
end
end
function mouseMove(tool,~,~)
p = tool.Axis.CurrentPoint;
col = round(p(1,1));
row = round(p(1,2));
if tool.MouseIsDown
if row >= 1 && row <= size(tool.Volume,1) && col >= 1 && col <= size(tool.Volume,2)
d = sqrt((row-tool.Row0)^2+(col-tool.Col0)^2);
if d >= 1 && d <= tool.MaxRadius
tool.RSlider.Value = d;
end
end
else
if row >= 1 && row <= size(tool.Volume,1) && col >= 1 && col <= size(tool.Volume,2)
tool.XSlider.Value = col;
tool.YSlider.Value = row;
end
end
end
function keyPressed(tool,~,event)
if strcmp(event.Key,'space')
if ~isempty(tool.SecondChannel)
if tool.ShowingFirstChannel
I = tool.SecondChannel(:,:,tool.PlaneIndex);
tool.PlaneHandle.CData = tool.applyThresholds(I);
tool.ShowingFirstChannel = false;
else
I = tool.Volume(:,:,tool.PlaneIndex);
tool.PlaneHandle.CData = tool.applyThresholds(I);
tool.ShowingFirstChannel = true;
end
end
end
addToZ = 0;
if strcmp(event.Key,'add') || strcmp(event.Key,'equal') || strcmp(event.Key,'rightarrow') || strcmp(event.Key,'uparrow')
addToZ = 1;
elseif strcmp(event.Key,'subtract') || strcmp(event.Key,'hyphen') || strcmp(event.Key,'leftarrow') || strcmp(event.Key,'downarrow')
addToZ = -1;
end
if addToZ ~= 0
candidatePlaneIndex = tool.PlaneIndex+addToZ;
% fprintf('...\ncurrent z: %d, candidate z: %d\n', tool.PlaneIndex, candidatePlaneIndex);
if candidatePlaneIndex >= 1 && candidatePlaneIndex <= tool.NPlanes
tool.PlaneIndex = candidatePlaneIndex;
I = tool.Volume(:,:,tool.PlaneIndex);
tool.PlaneHandle.CData = tool.applyThresholds(I);
tool.SliderZ.Value = tool.PlaneIndex;
end
% fprintf('current z: %d\n', tool.PlaneIndex);
end
end
function buttonPushed(tool,src,~)
if strcmp(src.String,'add')
cz = tool.PlaneIndex;
r = tool.Radius;
nz = tool.NPlanes;
tool.SphereID = tool.SphereID+1;
tool.Spheres = [tool.Spheres; [tool.SphereID tool.ClassIndex tool.Center cz r]];
for iPlane = max(cz-r+1,1):min(cz+r-1,nz)
radForPlane = sqrt(r^2-(iPlane-cz)^2);
circlesForPlane = [tool.Circles{iPlane}; [tool.SphereID tool.ClassIndex tool.Center radForPlane]];
tool.Circles{iPlane} = circlesForPlane;
end
redrawCircles(tool);
disp('added sphere')
disp(tool.Spheres)
elseif strcmp(src.String,'rem')
validSpheres = tool.Spheres(tool.Spheres(:,2) == tool.ClassIndex,:);
if ~isempty(validSpheres)
cx = tool.Center(1); cy = tool.Center(2); cz = tool.PlaneIndex;
centers = validSpheres(:,3:5);
distances = sqrt(sum((repmat([cx cy cz],[size(centers,1) 1])-centers).^2,2));
[dm,im] = min(distances);
r = validSpheres(im(1),6);
if dm(1) < r
id = validSpheres(im(1),1);
cz = validSpheres(im(1),5);
tool.Spheres(tool.Spheres(:,1) == id,:) = [];
for iPlane = max(cz-r+1,1):min(cz+r-1,tool.NPlanes)
circlesForPlane = tool.Circles{iPlane};
circlesForPlane(circlesForPlane(:,1) == id,:) = [];
tool.Circles{iPlane} = circlesForPlane;
end
end
redrawCircles(tool);
disp('removed sphere')
disp(tool.Spheres)
end
end
end
function redrawCircles(tool)
circles = tool.Circles{tool.PlaneIndex};
axes(tool.Axis);
delete(tool.CirclesHandle);
if ~isempty(circles)
circles = circles(circles(:,2) == tool.ClassIndex,:);
tool.CirclesHandle = viscircles(circles(:,3:4),circles(:,5),'Color','blue','LineWidth',1);
end
end
function popupManage(tool,src,~)
tool.ClassIndex = src.Value;
redrawCircles(tool);
end
function continuousSliderManage(tool,~,callbackdata)
tag = callbackdata.AffectedObject.Tag;
value = callbackdata.AffectedObject.Value;
if strcmp(tag,'uts') || strcmp(tag,'lts')
if strcmp(tag,'uts')
tool.UpperThreshold = value;
elseif strcmp(tag,'lts')
tool.LowerThreshold = value;
end
if isempty(tool.SecondChannel) || tool.ShowingFirstChannel
I = tool.Volume(:,:,tool.PlaneIndex);
tool.PlaneHandle.CData = tool.applyThresholds(I);
else
I = tool.SecondChannel(:,:,tool.PlaneIndex);
tool.PlaneHandle.CData = tool.applyThresholds(I);
end
elseif strcmp(tag,'zs')
tool.PlaneIndex = round(value);
tool.PlaneLabel.String = sprintf('%d',tool.PlaneIndex);
tool.Figure.Name = sprintf('Frame %d',tool.PlaneIndex);
if isempty(tool.SecondChannel) || tool.ShowingFirstChannel
I = tool.Volume(:,:,tool.PlaneIndex);
tool.PlaneHandle.CData = tool.applyThresholds(I);
else
I = tool.SecondChannel(:,:,tool.PlaneIndex);
tool.PlaneHandle.CData = tool.applyThresholds(I);
end
redrawCircles(tool);
elseif strcmp(tag,'xs')
tool.Center(1) = round(value);
tool.VLineHandle.XData = tool.Center(1)*[1 1];
tool.YMarkers{1}.XData = tool.Center(1)+[-5 5];
tool.YMarkers{2}.XData = tool.Center(1)+[-5 5];
tool.XMarkers{1}.XData = (tool.Center(1)-tool.Radius)*[1 1];
tool.XMarkers{2}.XData = (tool.Center(1)+tool.Radius)*[1 1];
elseif strcmp(tag,'ys')
tool.Center(2) = round(value);
tool.HLineHandle.YData = tool.Center(2)*[1 1];
tool.XMarkers{1}.YData = tool.Center(2)+[-5 5];
tool.XMarkers{2}.YData = tool.Center(2)+[-5 5];
tool.YMarkers{1}.YData = (tool.Center(2)-tool.Radius)*[1 1];
tool.YMarkers{2}.YData = (tool.Center(2)+tool.Radius)*[1 1];
elseif strcmp(tag,'rs')
tool.Radius = round(value);
tool.XMarkers{1}.XData = (tool.Center(1)-tool.Radius)*[1 1];
tool.XMarkers{1}.YData = tool.Center(2)+[-5 5];
tool.XMarkers{2}.XData = (tool.Center(1)+tool.Radius)*[1 1];
tool.XMarkers{2}.YData = tool.Center(2)+[-5 5];
tool.YMarkers{1}.XData = tool.Center(1)+[-5 5];
tool.YMarkers{1}.YData = (tool.Center(2)-tool.Radius)*[1 1];
tool.YMarkers{2}.XData = tool.Center(1)+[-5 5];
tool.YMarkers{2}.YData = (tool.Center(2)+tool.Radius)*[1 1];
end
end
function T = applyThresholds(tool,I)
T = I;
T(T < tool.LowerThreshold) = tool.LowerThreshold;
T(T > tool.UpperThreshold) = tool.UpperThreshold;
T = T-min(T(:));
T = T/max(T(:));
end
function closeTool(tool,~,~)
delete(tool.Figure)
delete(tool.Dialog);
end
function buttonDonePushed(tool,~,~)
tool.closeTool();
end
end
end