-
Notifications
You must be signed in to change notification settings - Fork 1
/
imtool3DROI_line.m
282 lines (217 loc) · 10.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
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
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};
end
%get the parent axis handle
parent = get(imageHandle,'Parent');
%compute the pheight
pheight = min(size(get(imageHandle,'CData')));
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','Delete'};
menuFunction = @contextMenuCallback;
%create the ROI object from the superclass
ROI@imtool3DROI(imageHandle,graphicsHandles,menuLabels,menuFunction);
%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');
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 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]);
end
function [stats, x ,y] = getMeasurements(ROI)
%get the position
position = ROI.position;
%get the image data
im = get(ROI.imageHandle,'CData');
%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));
%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');
%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
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)
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 '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
end
end