-
Notifications
You must be signed in to change notification settings - Fork 10
/
imtool3DROI_line.m
407 lines (337 loc) · 15.5 KB
/
imtool3DROI_line.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
classdef imtool3DROI_line < imtool3DROI
properties
end
properties (SetAccess = protected, GetAccess = protected)
position %nx2 matrix that defines the vertices of the polygon [x y]
tbuff = 10; %amount of space (in pixels) to place the text above the line
pbuff = 10; %amount of space (in pixels) to place the line profile above the line
pheight %height of the line profile drawn above the ROI line. Default sets it to a percentage of the size of the image
end
methods
%Contstructor
function ROI = imtool3DROI_line(varargin)
switch nargin
case 0 %use the current figure
%let the user draw the line
h = imline;
%get the parent axes
ha = get(h,'Parent');
%get the handle of the image
hi = imhandles(ha);
if length(hi)>1
for i=1:length(hi)
if ndims(get(hi(i),'CData'))<3
imageHandle = hi(i);
end
end
else
imageHandle = hi;
end
%get the position
position = getPosition(h);
%make sure the second point is different than the first
if position(1,1) == position(1,2) && position(2,1) == position(2,2)
position(2,:) = position(2,:)+5;
end
%delete the imroi object
delete(h);
case 1 %user inputs only the handle to the image
imageHandle = varargin{1};
parent = get(imageHandle,'Parent');
h = imline(parent);
position = getPosition(h);
if position(1,1) == position(1,2) && position(2,1) == position(2,2)
position(2,:) = position(2,:)+5;
end
delete(h);
case 2 %user inputs both the parent handle and a position
imageHandle = varargin{1};
position = varargin{2};
case 3
imageHandle = varargin{1};
position = varargin{2};
if isempty(position)
parent = get(imageHandle,'Parent');
h = imline(parent);
position = getPosition(h);
if position(1,1) == position(1,2) && position(2,1) == position(2,2)
position(2,:) = position(2,:)+5;
end
delete(h);
end
tool = varargin{3};
end
%get the parent axis handle
parent = get(imageHandle,'Parent');
%compute the pheight
pheight = min([size(get(imageHandle,'CData'),1) size(get(imageHandle,'CData'),2)]);
pheight = pheight/10;
%Draw the graphics
nextPlot = get(parent,'NextPlot'); %make sure the new graphics don't delete the old ones
set(parent,'NextPlot','add');
graphicsHandles(1) = plot(position(:,1),position(:,2),'-r','LineWidth',1.5,'Parent',parent);
graphicsHandles(2) = plot(position(:,1),position(:,2),'sr','LineWidth',1.5,'MarkerSize',8,'Parent',parent,'MarkerFaceColor','r');
set(graphicsHandles,'Clipping','off')
set(parent,'NextPlot',nextPlot);
%Define the context menu options (i.e., what happens when you
%right click on the ROI)
menuLabels = {'Export stats','Plot profile','Hide Text','Hide profile', 'Delete', 'Symmetrize mask (current slice)', 'Symmetrize mask (all slices)', 'Split mask (current slice)', 'Split mask (all slices)'};
if ~exist('tool','var') || isempty(tool), menuLabels(6:end) = []; tool = []; end
menuFunction = @contextMenuCallback;
%create the ROI object from the superclass
ROI@imtool3DROI(imageHandle,graphicsHandles,menuLabels,menuFunction,tool);
%create the text object and line profile object
[x,y] = findTextPosition(position,ROI.tbuff);
ROI.textHandle = text(x,y,'text','Parent',parent,'Color','w','FontSize',10,'EdgeColor','w','BackgroundColor','k','HorizontalAlignment','Left','VerticalAlignment','top','Clipping','on');
hold on
graphicsHandles(3) = plot(position(:,1),position(:,2)-ROI.pbuff,'-r','LineWidth',1.5,'Parent',parent);
set(parent,'NextPlot',nextPlot);
ROI.graphicsHandles = graphicsHandles;
%Set the position and pheight properties of the ROI
ROI.position = position;
ROI.pheight = pheight;
%add a listener for changes in the image. This automatically
%updates the ROI text when the image changes
ROI.listenerHandle = addlistener(ROI.imageHandle,'CData','PostSet',@ROI.handlePropEvents);
%Set the button down functions of the graphics
for i=1:length(graphicsHandles)
fun = @(hObject,evnt) ButtonDownFunction(hObject,evnt,ROI,i); set(graphicsHandles(i),'ButtonDownFcn',fun);
end
%update the text
newPosition(ROI,position)
end
function position = getPosition(ROI)
position = ROI.position;
end
function profileVisible(ROI,visible)
if visible
set(ROI.graphicsHandles(3),'Visible','on')
else
set(ROI.graphicsHandles(3),'Visible','off');
end
end
function newPosition(ROI,position)
%set the position property of the ROI
ROI.position = position;
%get the graphics handles
graphicsHandles = ROI.graphicsHandles;
%set the new position of the line
set(graphicsHandles(1),'XData',position(:,1),'YData',position(:,2));
set(graphicsHandles(2),'XData',position(:,1),'YData',position(:,2));
%get the ROI measurements
[stats, x , y] = getMeasurements(ROI);
%Set position of the line profile
set(graphicsHandles(3),'XData',x,'YData',y);
%set the textbox
[x,y] = findTextPosition(position,ROI.tbuff);
str = ['Length: ' num2str(stats.distance,'%+.2f')];
set(ROI.textHandle,'String',str,'Position',[x y]);
notify(ROI,'newROIPosition');
end
function [stats, x ,y] = getMeasurements(ROI)
%get the position
position = ROI.position;
%get the image data
im = get(ROI.imageHandle,'CData');
if size(im,3) == 3
im = rgb2gray(im);
end
%get the distance
stats.distance = norm(position(1,:)-position(2,:));
%get the line profile data
[cx,cy,pv] = improfile(ROI.imageHandle.XData,ROI.imageHandle.YData,im,position(:,1),position(:,2));
if ndims(pv)>1, pv = mean(pv,3); end % RGB
%get the stats
stats.mean = mean(pv);
stats.STD = std(pv);
stats.min = min(pv);
stats.max = max(pv);
stats.profile = pv;
stats.cx = cx;
stats.cy = cy;
stats.position = position;
%find the positions of the line profile
%get the vector pointing in the direction of the profile
dir = (position(2,:)-position(1,:))./stats.distance;
%get a normal vector
perp = [dir(2) -dir(1)];
%move the line profile by pbuff in the direction of perp
x = cx + ROI.pbuff*perp(1);
y = cy + ROI.pbuff*perp(2);
%get a vector of distances away from this line
d = mat2gray(pv); d = d*ROI.pheight;
%adjust the x and y values
x = x + d.*perp(1);
y = y + d.*perp(2);
end
function handlePropEvents(ROI,src,evnt)
position = getPosition(ROI);
newPosition(ROI,position);
end
end
end
function ButtonDownFunction(hObject,evnt,ROI,n)
%get the parent figure handle
fig = ROI.figureHandle;
%get the current button motion and button up functions of the figure
WBMF_old = get(fig,'WindowButtonMotionFcn');
WBUF_old = get(fig,'WindowButtonUpFcn');
%get the original point that was clicked
op = get(ROI.axesHandle,'CurrentPoint'); op=[op(1,1) op(1,2)];
%get the original position of the ROI
position_old = getPosition(ROI);
%get the type of click
click = get(ROI.figureHandle,'SelectionType');
if strcmp(click,'normal')
%get the point that was clicked
dist = sqrt((position_old(:,1)-op(1)).^2 + (position_old(:,2)-op(2)).^2 );
[~, ind] = min(dist);
%set the new window button motion function and button up function of the figure
fun = @(src,evnt) ButtonMotionFunction(src,evnt,ROI,n,op,position_old,ind);
fun2=@(src,evnt) ButtonUpFunction(src,evnt,ROI,WBMF_old,WBUF_old);
set(fig,'WindowButtonMotionFcn',fun,'WindowButtonUpFcn',fun2);
end
end
function ButtonMotionFunction(src,evnt,ROI,n,op,position_old,ind)
cp = get(ROI.axesHandle,'CurrentPoint'); cp=[cp(1,1) cp(1,2)];
switch n
case 2 %user clicked on a vertice. Will move just that vertice
position = getPosition(ROI);
position(ind,1) = cp(1);
position(ind,2) = cp(2);
case 1 %User clicked on the line. Entire ROI will move
d = cp - op;
position(:,1) = position_old(:,1)+d(1);
position(:,2) = position_old(:,2)+d(2);
otherwise %location remains unchanged (happens when a vertice is removed and the user keeps the button pressed while moving the mouse)
position = getPosition(ROI);
end
newPosition(ROI,position);
end
function ButtonUpFunction(src,evnt,ROI,WBMF_old,WBUF_old)
fig = ROI.figureHandle;
set(fig,'WindowButtonMotionFcn',WBMF_old,'WindowButtonUpFcn',WBUF_old);
end
function [x,y] = findTextPosition(position,tbuff)
%This finds the postion of the text box given the polygon vertices
x = min(position(:,1)); y = max(position(:,2))+tbuff;
end
function contextMenuCallback(source,callbackdata,ROI,tool,islct)
switch get(source,'Label')
case 'Delete'
delete(ROI);
case 'Export stats'
[stats, ~,~] = getMeasurements(ROI);
name = inputdlg('Enter variable name');
if isempty(name), return; end
name=name{1};
assignin('base', name, stats)
case 'Plot profile'
[stats, ~,~] = getMeasurements(ROI);
%get distance from the first point
x = stats.cx(1); y = stats.cy(1);
d = sqrt((stats.cx-x).^2+(stats.cy-y).^2);
%make plot
figure;
plot(d,stats.profile);
xlabel('Distance (px)');
ylabel('Pixel Value');
case 'Hide Text'
switch get(source,'Checked')
case 'off'
set(source,'Checked','on');
ROI.textVisible = false;
case 'on'
set(source,'Checked','off');
ROI.textVisible = true;
end
case 'Hide profile'
switch get(source,'Checked')
case 'off'
set(source,'Checked','on');
profileVisible(ROI,0)
case 'on'
set(source,'Checked','off');
profileVisible(ROI,1)
end
case 'Symmetrize mask (current slice)'
Pos = ROI.getPosition;
Mask = tool.getCurrentMaskSlice;
% symetrize Mask using axis Pos
[v,u] = find(tool.getCurrentMaskSlice);
UV = [u,v]- repmat(Pos(1,:),[length(u) 1]);
symAxis = Pos(2,:)-Pos(1,:);
symAxis = symAxis/norm(symAxis);
UV_Sym = UV - 2*(UV-repmat(UV*symAxis',[1 2]).*repmat(symAxis,[length(u) 1]));
UV_Sym = UV_Sym + repmat(Pos(1,:),[length(u) 1]);
% save
ind = sub2ind(size(Mask),min(size(Mask,1),max(1,round(UV_Sym(:,2)))),min(size(Mask,2),max(1,round(UV_Sym(:,1)))));
Mask(ind)=true;
tool.setCurrentMaskSlice(Mask);
notify(tool,'maskChanged')
case 'Symmetrize mask (all slices)'
% todo: implement all slices in a clean way (maybe with tilted plane)
set(tool(1).getHandles.fig,'Pointer','watch')
drawnow;
set(source,'Label','Symmetrize mask (current slice)');
zinit = tool.getCurrentSlice;
S = tool.getImageSize(1);
for iz=1:S(3)
tool.setCurrentSlice(iz)
contextMenuCallback(source,[],ROI,tool);
end
tool.setCurrentSlice(zinit)
set(source,'Label','Symmetrize mask (all slices)');
set(tool(1).getHandles.fig,'Pointer','arrow')
case 'Split mask (current slice)'
Pos = ROI.getPosition;
Mask = tool.getCurrentMaskSlice;
% find side
[v,u] = find(tool.getCurrentMaskSlice);
UV = [u,v]- repmat(Pos(1,:),[length(u) 1]);
symAxis = Pos(2,:)-Pos(1,:);
symAxis = symAxis/norm(symAxis);
side = sign(UV*[-symAxis(2) symAxis(1)]');
ind = sub2ind(size(Mask),v(side>0),u(side>0));
val = tool.getmaskSelected;
if ~exist('islct','var')
islct = inputdlg('Left side value');
if isempty(islct) || isempty(str2num(islct{1}))
return;
else
islct = str2num(islct{1});
islct = floor(islct(1));
end
end
tool.setmaskSelected(islct);
newMask = tool.getCurrentMaskSlice;
newMask(ind) = 1;
tool.setCurrentMaskSlice(newMask);
tool.setlockMask; % switch if lock
tool.setCurrentMaskSlice(newMask);
tool.setlockMask; % set back
tool.setmaskSelected(val);
notify(tool,'maskChanged')
case 'Split mask (all slices)'
% todo: implement all slices in a clean way (maybe with tilted plane)
islct = inputdlg('Left side value');
if isempty(islct) || isempty(str2num(islct{1}))
return;
else
islct = str2num(islct{1});
islct = floor(islct(1));
end
set(tool(1).getHandles.fig,'Pointer','watch')
drawnow;
set(source,'Label','Split mask (current slice)');
zinit = tool.getCurrentSlice;
S = tool.getImageSize(1);
for iz=1:S(3)
tool.setCurrentSlice(iz)
contextMenuCallback(source,[],ROI,tool,islct);
end
tool.setCurrentSlice(zinit)
set(source,'Label','Split mask (all slices)');
set(tool(1).getHandles.fig,'Pointer','arrow')
end
end