-
Notifications
You must be signed in to change notification settings - Fork 1
/
imtool3DROI_rect.m
490 lines (394 loc) · 17.5 KB
/
imtool3DROI_rect.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
classdef imtool3DROI_rect < imtool3DROI
properties
fixedAspectRatio = false;
end
properties (SetAccess = protected, GetAccess = protected)
position %defines the center of the box and its width/height [cx cy width height]
tbuff %amount of space (in pixels) to place the text above the ROI
end
properties (Dependent = true, Access = public)
aspectRatio
end
methods
%constructor
function ROI = imtool3DROI_rect(varargin)
switch nargin
case 0 %use the current figure
%let the user draw the ROI
h = imrect;
%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
end
%get the position
pos = getPosition(h);
position = [pos(1)+pos(3)/2 pos(2)+pos(4)/2 pos(3) pos(4)];
%delete the imroi object
delete(h);
case 1 %user inputs only the handle to the image
imageHandle = varargin{1};
parent = get(imageHandle,'Parent');
h = imrect(parent);
pos = getPosition(h);
position = [pos(1)+pos(3)/2 pos(2)+pos(4)/2 pos(3) pos(4)];
delete(h);
case 2 %user inputs both the parent handle and a position
imageHandle = varargin{1};
position = varargin{2};
end
%get the parent axis handle
parent = get(imageHandle,'Parent');
%find the top left corner of the box
pos = [position(1)-position(3)/2 position(2)-position(4)/2 position(3) position(4)];
%Draw the rectangle at the desired spot
graphicsHandles(1) = rectangle('Position',pos,'Parent',parent,'EdgeColor','r','LineWidth',1.5,'Curvature',[0 0]);
%Draw a cross at the center of the box and sqaures on the sides
nextPlot = get(parent,'NextPlot'); %make sure the new graphics don't delete the old ones
set(parent,'NextPlot','add');
graphicsHandles(2) = plot(position(1),position(2),'+r','MarkerSize',12,'Parent',parent); %middle cross
graphicsHandles(3) = plot(pos(1),pos(2),'sr','MarkerSize',8,'Parent',parent,'MarkerFaceColor','r'); %top left corner
graphicsHandles(4) = plot(pos(1)+pos(3),pos(2),'sr','MarkerSize',8,'Parent',parent,'MarkerFaceColor','r'); %top right corner
graphicsHandles(5) = plot(pos(1),pos(2)+pos(4),'sr','MarkerSize',8,'Parent',parent,'MarkerFaceColor','r'); %bottom left corner
graphicsHandles(6) = plot(pos(1)+pos(3),pos(2)+pos(4),'sr','MarkerSize',8,'Parent',parent,'MarkerFaceColor','r'); %bottom right corner
graphicsHandles(7) = plot(pos(1),position(2),'sr','MarkerSize',8,'Parent',parent,'MarkerFaceColor','r'); %left
graphicsHandles(8) = plot(pos(1)+pos(3),position(2),'sr','MarkerSize',8,'Parent',parent,'MarkerFaceColor','r'); %right
graphicsHandles(9) = plot(position(1),pos(2),'sr','MarkerSize',8,'Parent',parent,'MarkerFaceColor','r'); %top
graphicsHandles(10) = plot(position(1),pos(2)+pos(4),'sr','MarkerSize',8,'Parent',parent,'MarkerFaceColor','r'); %bottom
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','Fix Aspect Ratio','Hide Text','Delete'};
menuFunction = @contextMenuCallback;
%create the ROI object from the superclass
ROI@imtool3DROI(imageHandle,graphicsHandles,menuLabels,menuFunction);
%Create the text box
I = get(ROI.imageHandle,'CData');
ROI.tbuff = .02*size(I,1);
ROI.textHandle = text(pos(1),pos(2)-ROI.tbuff,'text','Parent',parent,'Color','w','FontSize',10,'EdgeColor','w','BackgroundColor','k','HorizontalAlignment','Left','VerticalAlignment','bottom','Clipping','on');
%Set the position property of the ROI
ROI.position = position;
%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
%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);
%update the text
newPosition(ROI,position)
end
function position = getPosition(ROI)
position = ROI.position;
end
function newPosition(ROI,position)
%Adjust the position if the aspect ratio should be fixed
if ROI.fixedAspectRatio
end
%set the position property of the ROI
ROI.position = position;
%find the top left corner of the box
pos = [position(1)-position(3)/2 position(2)-position(4)/2 position(3) position(4)];
%get the graphics handles
graphicsHandles = ROI.graphicsHandles;
%set the new position of the rectangle and other graphics
%objects
set(graphicsHandles(1),'Position',pos);
set(graphicsHandles(2),'Xdata',position(1),'Ydata',position(2));
set(graphicsHandles(3),'Xdata',pos(1),'Ydata',pos(2));
set(graphicsHandles(4),'Xdata',pos(1)+pos(3),'Ydata',pos(2));
set(graphicsHandles(5),'Xdata',pos(1),'Ydata',pos(2)+pos(4));
set(graphicsHandles(6),'Xdata',pos(1)+pos(3),'Ydata',pos(2)+pos(4));
set(graphicsHandles(7),'Xdata',pos(1),'Ydata',position(2));
set(graphicsHandles(8),'Xdata',pos(1)+pos(3),'Ydata',position(2));
set(graphicsHandles(9),'Xdata',position(1),'Ydata',pos(2));
set(graphicsHandles(10),'Xdata',position(1),'Ydata',pos(2)+pos(4));
%get the ROI measurements
stats = getMeasurements(ROI);
%set the textbox
str = {['Mean: ' num2str(stats.mean,'%+.2f')], ['STD: ' num2str(stats.STD,'%.2f')]};
set(ROI.textHandle,'String',str,'Position',[pos(1) pos(2)-ROI.tbuff]);
%notify a new position
notify(ROI,'newROIPosition');
end
function newPositionSameSize(ROI,pos)
position = ROI.position;
position(1:2)=pos;
newPosition(ROI,position);
end
function stats = getMeasurements(ROI)
%get the position
position = ROI.position;
%find the top left corner of the box
pos = [position(1)-position(3)/2 position(2)-position(3)/2 position(3) position(4)];
%make the polygon
x = [pos(1) pos(1)+pos(3) pos(1)+pos(3) pos(1) pos(1)];
y = [pos(2) pos(2) pos(2)+pos(4) pos(2)+pos(4) pos(2)];
im = double(get(ROI.imageHandle,'CData'));
m = size(im,1);
n = size(im,2);
%Scale the polygon to match the size of the displayed image (in
%the case that the displayed image is being upsampled to match
%the screen resolution).
x = x*n/ROI.imageHandle.XData(2);
y = y*m/ROI.imageHandle.YData(2);
mask = poly2mask(x,y,m,n);
im = im(mask);
stats.mean = mean(im);
stats.STD = std(im);
stats.min = min(im);
stats.max = max(im);
stats.mask = mask;
stats.position = position;
end
function handlePropEvents(ROI,src,evnt)
position = getPosition(ROI);
newPosition(ROI,position);
end
function set.fixedAspectRatio(ROI,fixedAspectRatio)
%Set the fixed aspect ratio property
ROI.fixedAspectRatio = fixedAspectRatio;
%update the context menu
for i=1:length(ROI.menuHandles)
if strcmp(get(ROI.menuHandles(i),'Label'),'Fix Aspect Ratio')
if fixedAspectRatio
set(ROI.menuHandles(i),'Check','on')
else
set(ROI.menuHandles(i),'Check','off')
end
end
end
end
function aspectRatio = get.aspectRatio(ROI)
position = ROI.position;
aspectRatio = position(3)/position(4);
end
function BB = getBoundingBox(ROI)
%BB = [rowMin rowMax; colMin ColMax];
lims = size(get(ROI.imageHandle,'CData'));
BB=zeros(2);
rowMin = floor(ROI.position(2) - ROI.position(4)/2);
if rowMin < 1
rowMin = 1;
end
colMin = floor(ROI.position(1) - ROI.position(3)/2);
if colMin < 1
colMin = 1;
end
rowMax = floor(ROI.position(2) + ROI.position(4)/2 - 1);
if rowMax > lims(1)
rowMax = lims(1);
end
colMax = floor(ROI.position(1) + ROI.position(3)/2 - 1);
if colMax > lims(2)
colMax = lims(2);
end
BB = [rowMin rowMax; colMin colMax];
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');
%set the new window button motion function and button up function of the figure
fun = @(src,evnt) ButtonMotionFunction(src,evnt,ROI,n);
fun2=@(src,evnt) ButtonUpFunction(src,evnt,ROI,WBMF_old,WBUF_old);
set(fig,'WindowButtonMotionFcn',fun,'WindowButtonUpFcn',fun2);
end
function ButtonMotionFunction(src,evnt,ROI,n)
cp = get(ROI.axesHandle,'CurrentPoint'); cp=[cp(1,1) cp(1,2)];
position = getPosition(ROI);
switch n
case 2 %middle cross
position(1) = cp(1); position(2) = cp(2);
case 3 %top left corner
%find the bottom edge
bottom = position(2)+position(4)/2;
%get the new height
height = bottom - cp(2);
if height>1
cy = cp(2)+height/2;
position(2) = cy;
position(4) = height;
%Adjust cp(1) if you want to fix the aspect ratio
if ROI.fixedAspectRatio
cp(1) = position(1)+position(3)/2-height*ROI.aspectRatio;
end
end
%find the right edge
right = position(1)+position(3)/2;
%find the new width
width = right-cp(1);
if width>1
%find the new center
cx = cp(1)+width/2;
position(1) = cx;
position(3) = width;
end
case 4 %top right corner
%find the bottom edge
bottom = position(2)+position(4)/2;
%get the new height
height = bottom - cp(2);
if height>1
cy = cp(2)+height/2;
position(2) = cy;
position(4) = height;
%Adjust cp(1) if you want to fix the aspect ratio
if ROI.fixedAspectRatio
cp(1) = position(1)-position(3)/2+height*ROI.aspectRatio;
end
end
%find the left edge
left = position(1)-position(3)/2;
%find the new width
width = cp(1) - left;
if width>1
cx = cp(1)-width/2;
position(1) = cx;
position(3) = width;
end
case 5 %bottom left corner
%find the top edge
top = position(2)-position(4)/2;
%get the new height
height = cp(2) - top;
if height>1
cy = cp(2)-height/2;
position(2) = cy;
position(4) = height;
%Adjust cp(1) if you want to fix the aspect ratio
if ROI.fixedAspectRatio
cp(1) = position(1)+position(3)/2+-height*ROI.aspectRatio;
end
end
%find the right edge
right = position(1)+position(3)/2;
%find the new width
width = right-cp(1);
if width>1
%find the new center
cx = cp(1)+width/2;
position(1) = cx;
position(3) = width;
end
case 6 %bottom right corner
%find the top edge
top = position(2)-position(4)/2;
%get the new height
height = cp(2) - top;
if height>1
cy = cp(2)-height/2;
position(2) = cy;
position(4) = height;
%Adjust cp(1) if you want to fix the aspect ratio
if ROI.fixedAspectRatio
cp(1) = position(1)-position(3)/2+height*ROI.aspectRatio;
end
end
%find the left edge
left = position(1)-position(3)/2;
%find the new width
width = cp(1) - left;
if width>1
cx = cp(1)-width/2;
position(1) = cx;
position(3) = width;
end
case 7 %left
%find the right edge
right = position(1)+position(3)/2;
%find the new width
width = right-cp(1);
if width>1
%find the new center
cx = cp(1)+width/2;
position(1) = cx;
position(3) = width;
end
if ROI.fixedAspectRatio
position(4) = width/ROI.aspectRatio;
end
case 8 %right
%find the left edge
left = position(1)-position(3)/2;
%find the new width
width = cp(1) - left;
if width>1
cx = cp(1)-width/2;
position(1) = cx;
position(3) = width;
end
if ROI.fixedAspectRatio
position(4) = width/ROI.aspectRatio;
end
case 9 %top
%find the bottom edge
bottom = position(2)+position(4)/2;
%get the new height
height = bottom - cp(2);
if height>1
cy = cp(2)+height/2;
position(2) = cy;
position(4) = height;
end
if ROI.fixedAspectRatio
position(3) = height*ROI.aspectRatio;
end
case 10 %bottom
%find the top edge
top = position(2)-position(4)/2;
%get the new height
height = cp(2) - top;
if height>1
cy = cp(2)-height/2;
position(2) = cy;
position(4) = height;
end
if ROI.fixedAspectRatio
position(3) = height*ROI.aspectRatio;
end
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 contextMenuCallback(source,callbackdata,ROI)
switch get(source,'Label')
case 'Delete'
delete(ROI);
case 'Export stats'
stats = getMeasurements(ROI);
name = inputdlg('Enter variable name');
name=name{1};
assignin('base', name, stats)
case 'Fix Aspect Ratio'
switch get(source,'Checked')
case 'off'
ROI.fixedAspectRatio=true;
case 'on'
ROI.fixedAspectRatio=false;
end
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
end
end