-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathannotationpoint.m
423 lines (346 loc) · 10.5 KB
/
annotationpoint.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
function varargout = annotationpoint(varargin)
% ANNOTATIONPOINT
% Functions to place and modify annotation points.
% Moved out from segment_main.m by Nisse Lundahl
%Invoke subfunction
macro_helper(varargin{:}); %future macro recording use
if (nargout)
[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
else
feval(varargin{:}); % FEVAL switchyard
end
%--------------------------------
function pointclearall_helper(no)
%--------------------------------
%Helperfunction to clear all points
global SET
SET(no).Point = [];
SET(no).Point.X = [];
SET(no).Point.Y = [];
SET(no).Point.T = [];
SET(no).Point.Z = [];
SET(no).Point.Label = {};
%----------------------
function pointclearall
%----------------------
%Clear all points.
global DATA SET NO
%Use to point to mag data set
no = NO;
if ~isempty(SET(NO).Parent)
no = SET(NO).Parent;
end
tools('enableundo',no);
%Update in 2D
for p = 1:length(DATA.ViewPanels)
viewfunctions('updatedrawlist',p)
drawfunctions('drawpanel',p)
end
try
if segment3dp.isviewportalive
DATA.LevelSet.ViewPort.setpoints([],[],[])
end
catch
end
pointclearall_helper(no);
%-------------------------------
function pointexportall_Callback %#ok<DEFNU>
%-------------------------------
%Export all point data
global DATA SET NO
%Use to point to mag data set
no = NO;
if ~isempty(SET(NO).Parent)
no = SET(NO).Parent;
end
if isempty(SET(no).Point)
pointclearall;
myfailed('No points marked',DATA.GUI.Segment);
return;
end
if length(SET(no).Point.X)<1
myfailed('No points marked',DATA.GUI.Segment);
return;
end
outdata = cell(10,10);
outdata{1,1} = 'Filename';
outdata{1,2} = SET(no).FileName;
%Non time resolved points
if sum(isnan(SET(no).Point.T))>0
outdata{3,1} = 'Non time resolved points';
outdata{4,1} = 'Label';
outdata{4,2} = 'X';
outdata{4,3} = 'Y';
outdata{4,4} = 'Z';
pos = find(isnan(SET(no).Point.T));
offset = 2+length(pos)+1;
for loop=1:length(pos)
outdata{4+loop,1} = SET(no).Point.Label{pos(loop)};
outdata{4+loop,2} = SET(no).Point.X(pos(loop))*SET(no).ResolutionX;
outdata{4+loop,3} = SET(no).Point.Y(pos(loop))*SET(no).ResolutionY;
outdata{4+loop,4} = SET(no).Point.Z(pos(loop))*(SET(no).SliceThickness+SET(no).SliceGap);
end
else
offset = 0;
end
%--- Time resolved points
if sum(not(isnan(SET(no).Point.T)))>0
%Basic titles
outdata{3+offset,1} = 'Time resolved points';
outdata{4+offset,1} = 'Time[ms]';
for tloop=1:SET(no).TSize
outdata{4+offset+tloop,1} = (tloop-1)*SET(no).TIncr*1000;
end
%extract data
ind = not(isnan(SET(no).Point.T));
labels = SET(no).Point.Label(ind);
%Find unique names
labelstodo = unique(labels);
for loop=1:length(labelstodo)
outdata{4+offset,2+(loop-1)*4} = 'Label';
outdata{4+offset,3+(loop-1)*4} = 'X';
outdata{4+offset,4+(loop-1)*4} = 'Y';
outdata{4+offset,5+(loop-1)*4} = 'Z';
end
for lloop=1:length(labelstodo)
for loop=1:length(SET(no).Point.X)
if not(isnan(SET(no).Point.T(loop)))&&isequal(labelstodo{lloop},SET(no).Point.Label{loop})
if not(isempty(outdata{4+offset+SET(no).Point.T(loop),2+(lloop-1)*4}))
mywarning(dprintf('Multiple points with same label, ambigous result. Timeframe %d, slice %d ',...
SET(no).Point.T(loop),SET(no).Point.Z(loop)),DATA.GUI.Segment);
end
outdata{4+offset+SET(no).Point.T(loop),2+(lloop-1)*4} = SET(no).Point.Label{loop};
outdata{4+offset+SET(no).Point.T(loop),3+(lloop-1)*4} = SET(no).Point.X(loop)*SET(no).ResolutionX;
outdata{4+offset+SET(no).Point.T(loop),4+(lloop-1)*4} = SET(no).Point.Y(loop)*SET(no).ResolutionY;
outdata{4+offset+SET(no).Point.T(loop),5+(lloop-1)*4} = SET(no).Point.Z(loop)*(SET(no).SliceThickness+SET(no).SliceGap);
end
end
end
end
segment('cell2clipboard',outdata);
%------------------------------------
function pointcleartemplate_Callback %#ok<DEFNU>
%------------------------------------
%Clear points using naming template.
global DATA SET NO
%Use to point to mag data set
no = NO;
if ~isempty(SET(NO).Parent)
no = SET(NO).Parent;
end
if isempty(SET(no).Point)||isempty(SET(no).Point.X)
myfailed('No points to delete.',DATA.GUI.Segment)
return;
end
s = myinputdlg({'Delete points labeled as:'},'Template',1,{''});
if isempty(s)
myfailed('Invalid template.',DATA.GUI.Segment);
return;
else
s = s{1};
end
tools('enableundo',no);
ind = true(1,length(SET(no).Point.Z));
for loop=1:length(SET(no).Point.Z)
if isequal(SET(no).Point.Label{loop},s)
ind(loop) = false;
end
end
if ~yesno(dprintf('Deleting %d points are you sure?',sum(not(ind))),[],DATA.GUI.Segment)
return;
end
SET(no).Point.X = SET(no).Point.X(ind);
SET(no).Point.Y = SET(no).Point.Y(ind);
SET(no).Point.T = SET(no).Point.T(ind);
SET(no).Point.Z = SET(no).Point.Z(ind);
SET(no).Point.Label = SET(no).Point.Label(ind);
viewfunctions('setview'); %drawfunctions('drawimageno');
%------------------------------------
function pointrenametemplate_Callback %#ok<DEFNU>
%------------------------------------
%Rename points according to a renaming template.
global DATA SET NO
%Use to point to mag data set
no = NO;
if ~isempty(SET(NO).Parent)
no = SET(NO).Parent;
end
if isempty(SET(no).Point)||isempty(SET(no).Point.X)
pointclearall;
myfailed('No points exist.',DATA.GUI.Segment);
return;
end
s = myinputdlg({'Rename points labeled as:'},'Template',1,{''});
if isempty(s)
myfailed('Invalid template.',DATA.GUI.Segment);
return;
else
s = s{1};
end
stri = myinputdlg({'New name:'},'Newname',1,{''});
if isempty(stri)
myfailed('Invalid new name.',DATA.GUI.Segment);
return;
else
stri = stri{1};
end
tools('enableundo',no);
for loop=1:length(SET(no).Point.Z)
if isequal(SET(no).Point.Label{loop},s)
SET(no).Point.Label{loop} = stri;
end
end
viewfunctions('setview'); %drawfunctions('drawimageno');
%----------------------------
function filterpoints_Callback %#ok<DEFNU>
%----------------------------
%Filter point in time using a Kalman filter
%Einar Heiberg
global SET NO
no = NO;
%Find points to filter
labels = SET(no).Point.Label;
labels = union(labels,{}); %Remove duplicates.
c = mymenu('Select point to filter',labels{:});
if isequal(c,0)
% myfailed('Aborted.');
return;
end
name = labels{c}; %This is the name to be found.
%---Extract coordinates.
%Find points
ind = false(1,length(SET(no).Point.X));
for loop = 1:length(SET(no).Point.X)
if isequal(SET(no).Point.Label{loop},name)
ind(loop) = true;
end
end
%Extract
x = SET(no).Point.X;
y = SET(no).Point.Y;
x = x(ind);
y = y(ind);
%Check
if ~isequal(length(x),SET(no).TSize)
myfailed('Number of points is not equal to number of timeframes. Duplicated point names?');
return;
end
%Ask for standard deviation
s.Noise = 0.3;
[s,ok] = inputstruct(s,'Enter Noise estimate');
if ~ok
myfailed('Aborted or illegal value.');
return;
end
%Extract it
objectnoise = s.Noise;
%Apply filter
xnew = kalman([x x x],objectnoise);
ynew = kalman([y y y],objectnoise);
%Plot to ask user
figure(99);
t = [SET(no).TimeVector SET(no).TimeVector+SET(no).TimeVector(end) SET(no).TimeVector+2*SET(no).TimeVector(end)];
clf;
%X coordinate
subplot(2,1,1);
plot(t,[x x x],'b.');
hold on;
plot(t,xnew,'b-');
hold off;
title(dprintf('X-coordinate.'));
xlabel(dprintf('Time [s]'));
ylabel(dprintf('Position (pixel)'));
%X coordinate
subplot(2,1,2);
plot(t,[y y y],'b.');
hold on;
plot(t,ynew,'b-');
hold off;
title(dprintf('Y-coordinate.'));
xlabel(dprintf('Time [s]'));
ylabel(dprintf('Position (pixel)'));
set(99,'numbertitle','off','name',dprintf('Position over time'));
%Crop it back from triplicate
xnew = xnew((length(x)+1):2*length(x));
ynew = ynew((length(y)+1):2*length(y));
if yesno('Do you want to apply filter?')
%Prepare to store by backup...
tools('enableundo',no);
%Store the result
SET(no).Point.X(ind) = xnew;
SET(no).Point.Y(ind) = ynew;
mymsgbox('Filter applied.');
end
close(99);
%----------------------------------------------
function importpoint_Callback(no) %#ok<DEFNU>
%----------------------------------------------
%Import point from another image stack.
%Imports to current image stack NO from no or if called with no input
%arguments user is asked.
global DATA SET NO
tools('enableundo');
if length(SET)<2
myfailed('Only one image stack in memory, import from file instead (under File menu).',DATA.GUI.Segment);
return;
end
if nargin==0
%Find what imagestack
menuitems = cell(1,numel(SET)-1);
impstacks = setdiff(1:numel(SET),NO);
for n = 1:numel(impstacks)
nn = impstacks(n);
menuitems{n} = sprintf('%d. %s',nn,[SET(nn).ImageType ' / ' SET(nn).ImageViewPlane]);
end
s = mymenu('Select which stack to import from',menuitems);
if s == 0 %operation cancelled
return;
else
no = impstacks(s);
end
end
if no==NO
myfailed('Cannot import from same image stack.',DATA.GUI.Segment);
return;
end
if (no>length(SET))||(no<1)
myfailed('Invalid image stack selected.',DATA.GUI.Segment);
return;
end
%Check what to do - later optionally take from input arguments.
if ~isempty(SET(no).Point)
importpointhelper(NO,no);
else
return;
end
segment('updatevolume');
viewfunctions('setview');
%----------------------------------------------------------------------------------
function importpointhelper(tono,fromno)
%----------------------------------------------------------------------------------
%Helper function to segmentimportsegmention Callback.
%- tono is destination of segmentation.
%- fromno is source.
global SET
x = SET(fromno).Point.X;
y = SET(fromno).Point.Y;
z = SET(fromno).Point.Z;
%Convert coordinates
pos = calcfunctions('xyz2rlapfh',fromno,x,y,z);
pos = calcfunctions('rlapfh2xyz',tono,pos(:,1),pos(:,2),pos(:,3));
%Copy the points
for loop = 1:size(pos,2)
x = pos(1,loop);
y = pos(2,loop);
z = pos(3,loop);
if ...
(x>0) && (x<=SET(tono).XSize) && ...
(y>0) && (y<=SET(tono).YSize) && ...
(z>0) && (z<=SET(tono).ZSize)
SET(tono).Point.X(end+1) = x;
SET(tono).Point.Y(end+1) = y;
SET(tono).Point.Z(end+1) = z;
SET(tono).Point.T(end+1) = SET(fromno).Point.T(loop);
SET(tono).Point.Label{end+1} = SET(fromno).Point.Label{loop};
end
end