-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathimtool3DROI_ellipse.m
518 lines (432 loc) · 18 KB
/
imtool3DROI_ellipse.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
classdef imtool3DROI_ellipse < imtool3DROI_rect
properties (SetAccess = protected, GetAccess = protected)
nPoints = 20; %number of points to use to define the polygon that makes the elliptical mask
end
methods
%constructor
function ROI = imtool3DROI_ellipse(varargin)
switch nargin
case 0 %use the current figure
%let the user draw the ROI
h = imellipse;
%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
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 = imellipse(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};
case 3
imageHandle = varargin{1};
position = varargin{2};
if isempty(position)
parent = get(imageHandle,'Parent');
h = imellipse(parent);
pos = getPosition(h);
position = [pos(1)+pos(3)/2 pos(2)+pos(4)/2 pos(3) pos(4)];
delete(h);
end
tool = varargin{3};
end
%contruct the rect ROI
if ~exist('tool','var'), tool=[]; end
ROI@imtool3DROI_rect(imageHandle,position, tool)
%make the rectangle an ellipse
set(ROI.graphicsHandles(1),'Curvature',[1 1]);
%update the position
newPosition(ROI,position)
%Set the button down functions of the graphics
for i=1:length(ROI.graphicsHandles)
fun = @(hObject,evnt) ButtonDownFunction(hObject,evnt,ROI,i); set(ROI.graphicsHandles(i),'ButtonDownFcn',fun);
end
end
function newPosition(ROI,position)
%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;
%get the corner positions
t = pi/4:pi/2:2*pi-pi/4;
[x,y] = getEllipsePoints(position,t,'');
%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',x(3),'Ydata',y(3));
set(graphicsHandles(4),'Xdata',x(4),'Ydata',y(4));
set(graphicsHandles(5),'Xdata',x(2),'Ydata',y(2));
set(graphicsHandles(6),'Xdata',x(1),'Ydata',y(1));
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
V = get(gca,'View');
if V(1)==-90
x = pos(1) + pos(3) + ROI.tbuff;
else
x = pos(1);
end
y = pos(2)-ROI.tbuff;
str = {['Mean: ' num2str(stats.mean,'%+.2f')], ...
['STD: ' num2str(stats.STD,'%.2f')], ...
['Area: ' num2str(stats.area,'%.2f')]};
set(ROI.textHandle,'String',str,'Position',[x y]);
%notify a new position
notify(ROI,'newROIPosition');
end
function [x, y] = getPoly(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,y] = getEllipsePoints(position,ROI.nPoints,'nPoints');
end
function stats = getMeasurements(ROI)
[x, y] = getPoly(ROI);
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.area = sum(mask(:));
stats.position = ROI.position;
end
function varargout = autoCenterROI(ROI,varargin)
Tforeground = .2;
Tbackground = .8;
pos=ROI.position(1:2);
switch nargin
case 1
weighting = 'pixels';
mode = 'normal';
case 2
weighting = varargin{1}; %Weighting can be 'pixels' or 'mask'
mode ='normal'; %can be 'normal' or 'force positive contrast';
case 3
weighting = varargin{1};
mode = varargin{2};
end
%Get the image
im = double(get(ROI.imageHandle,'CData'));
%Get coordinate system
[X, Y]=meshgrid((1:size(im,2))-pos(1),(1:size(im,1))-pos(2));
R=sqrt((X).^2 +(Y.^2));
%get the mask
stats = getMeasurements(ROI);
mask=stats.mask;
amin=min(im(mask));
amax=max(im(mask));
sim=mat2gray(im,[amin amax]);
t=graythresh(sim(mask)); %Otsu threshold
bw=im2bw(sim,t);
bw=bw & mask;
%bw=bwareaopen(bw,100);
switch mode
case 'force positive contrast'
obj=mean(im(bw));
bkg=mean(im(~bw & mask));
case 'normal'
bkg=mean(im(mask & R>quantile(R(mask),Tbackground)));
obj=mean(im(mask & R<quantile(R(mask),Tforeground)));
otherwise
bkg=mean(im(mask & R>quantile(R(mask),Tbackground)));
obj=mean(im(mask & R<quantile(R(mask),Tforeground)));
end
contrast=obj-bkg;
if contrast<0
bw=~bw & mask;
end
if any(bw(:))
success = true;
%compute the centroid of the rod
center=zeros(1,2);
switch weighting
case 'pixels'
center(1) = sum(X(bw).*im(bw))/sum(im(bw));
center(2) = sum(Y(bw).*im(bw))/sum(im(bw));
case 'mask'
center(1) = sum(X(bw).*bw(bw))/sum(bw(bw));
center(2) = sum(Y(bw).*bw(bw))/sum(bw(bw));
otherwise
center(1) = sum(X(bw).*im(bw))/sum(im(bw));
center(2) = sum(Y(bw).*im(bw))/sum(im(bw));
end
position = [center 0 0];
position=position+ROI.position;
newPosition(ROI,position);
else
success = false;
end
switch nargout
case 1
varargout{1} = success;
end
end
function success = autoCenterROIFindCircleMethod(ROI,circleRange)
rescale=4;
%Get the image
im = double(get(ROI.imageHandle,'CData'));
stats = getMeasurements(ROI);
mask=stats.mask;
%Crop the image
[Yind Xind]=ind2sub(size(im),find(mask(:)));
im=im(min(Yind):max(Yind),min(Xind):max(Xind));
%Upsample the image
im=imresize(im,rescale);
%Find the circles
warning('off','images:imfindcircles:warnForLargeRadiusRange')
center = imfindcircles(im, round(rescale*circleRange),'Sensitivity',.95);
warning('on','images:imfindcircles:warnForLargeRadiusRange')
%move the ROI
if ~isempty(center)
center=center(1,:);
success=true;
center=center/rescale;
center=center + [min(Xind) min(Yind)];
position = [center ROI.position(3:4)];
newPosition(ROI,position);
else
success=false;
end
end
end
end
function [x,y] = getEllipsePoints(position,t,mode)
%This function returns a list of vertices of an elliptical polygon with
%nPoints number of vertices;
if strcmp(mode,'nPoints')
t=linspace(0,2*pi,t); %elliptical equation is parameterized by t
end
a = position(3)/2;
b = position(4)/2;
x = a*cos(t); x = x+position(1);
y = b*sin(t); y = y+position(2);
end
function ButtonDownFunction(hObject,evnt,ROI,n)
%get the parent figure handle
fig = ROI.figureHandle;
%get the type of click
click = get(fig,'SelectionType');
if strcmp(click,'normal')
%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
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 x and y for the current position
[x,y] = getEllipsePoints(position,5*pi/4,'');
dx = x-cp(1); dy = y-cp(2);
cp(1) = position(1)-(position(3)/2+dx); cp(2) = position(2)-(position(4)/2+dy);
%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 x and y for the current position
[x,y] = getEllipsePoints(position,7*pi/4,'');
dx = cp(1)-x; dy = y-cp(2);
cp(1) = position(1)+(position(3)/2+dx); cp(2) = position(2)-(position(4)/2+dy);
%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 x and y for the current position
[x,y] = getEllipsePoints(position,3*pi/4,'');
dx = x-cp(1); dy = cp(2)-y;
cp(1) = position(1)-(position(3)/2+dx); cp(2) = position(2)+(position(4)/2+dy);
%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 x and y for the current position
[x,y] = getEllipsePoints(position,pi/4,'');
dx = cp(1)-x; dy = cp(2)-y;
cp(1) = position(1)+(position(3)/2+dx); cp(2) = position(2)+(position(4)/2+dy);
%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