-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimageAnnotationTool.m
More file actions
261 lines (228 loc) · 11.8 KB
/
imageAnnotationTool.m
File metadata and controls
261 lines (228 loc) · 11.8 KB
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
classdef imageAnnotationTool < handle
properties
Figure
Axis
ImageHandle
TransparencyHandle
Image
NLabels
LabelIndex
LabelMasks
MouseIsDown
PenSize
PenSizeText
Dialog
RadioDraw
RadioErase
Slider
SliderMin
SliderMax
LowerThreshold
UpperThreshold
LowerThresholdBox
UpperThresholdBox
LowerThresholdSlider
UpperThresholdSlider
SecondChannel
ShowingFirstChannel
end
methods
function tool = imageAnnotationTool(I,nLabels,varargin)
% see annotate2D.m for a demo
tool.ShowingFirstChannel = true;
if nargin > 2
tool.SecondChannel = varargin{1};
end
tool.LabelIndex = 1;
tool.Image = I;
J = ones(size(tool.Image));
J = cat(3,zeros(size(I,1),size(I,2),2),J);
tool.NLabels = nLabels;
tool.Figure = figure(...%'MenuBar','none', ...
'NumberTitle','off', ...
'Name','Image', ...
'CloseRequestFcn',@tool.closeFigure, ...
'WindowButtonMotionFcn', @tool.mouseMove, ...
'WindowButtonDownFcn', @tool.mouseDown, ...
'WindowButtonUpFcn', @tool.mouseUp, ...
'KeyPressFcn',@tool.keyPressed, ...
'Resize','on');
tool.Axis = axes('Parent',tool.Figure,'Position',[0 0 1 1]);
tool.ImageHandle = imshow(tool.Image);
hold on
tool.TransparencyHandle = imshow(J);
hold off
tool.LabelMasks = zeros(size(tool.Image,1),size(tool.Image,2),tool.NLabels);
tool.LabelIndex = 1;
set(tool.TransparencyHandle, 'AlphaData', zeros(size(tool.Image)));
tool.MouseIsDown = false;
dwidth = 300;
dborder = 10;
cwidth = dwidth-2*dborder;
cheight = 20;
tool.Dialog = dialog('WindowStyle', 'normal',...
'Name', 'ImageAnnotationTool',...
'CloseRequestFcn', @tool.closeDialog,...
'KeyPressFcn',@tool.keyPressed,...
'Position',[100 100 dwidth 11*dborder+11*cheight]);
labels = cell(1,nLabels);
for i = 1:nLabels
labels{i} = sprintf('Class %d',i);
end
uicontrol('Parent',tool.Dialog,'Style','text','String','Lower/Upper Thresholds','Position',[dborder 10*dborder+10*cheight cwidth cheight],'HorizontalAlignment','left');
% lower threshold slider
tool.LowerThreshold = 0;
tool.LowerThresholdSlider = uicontrol('Parent',tool.Dialog,'Style','slider','Min',0,'Max',1,'Value',tool.LowerThreshold,'Position',[dborder 9*dborder+8*cheight cwidth cheight],'Tag','lts');
addlistener(tool.LowerThresholdSlider,'Value','PostSet',@tool.continuousSliderManage);
tool.LowerThresholdBox = uicontrol('Parent',tool.Dialog,'Style','edit','String','0.0000','Position',[dborder 9*dborder+9*cheight 70 cheight],'HorizontalAlignment','left','Tag','ltb');
% upper threshold slider
tool.UpperThreshold = 1;
tool.UpperThresholdSlider = uicontrol('Parent',tool.Dialog,'Style','slider','Min',0,'Max',1,'Value',tool.UpperThreshold,'Position',[dborder 8*dborder+7*cheight cwidth cheight],'Tag','uts');
addlistener(tool.UpperThresholdSlider,'Value','PostSet',@tool.continuousSliderManage);
tool.UpperThresholdBox = uicontrol('Parent',tool.Dialog,'Style','edit','String','1.0000','Position',[dborder+cwidth-70 8*dborder+6*cheight 70 cheight],'HorizontalAlignment','right','Tag','utb');
% erase/draw
tool.RadioDraw = uicontrol('Parent',tool.Dialog,'Style','radiobutton','Position',[dborder 5*dborder+6*cheight cwidth cheight],'String','Draw','Callback',@tool.radioDraw);
tool.RadioErase = uicontrol('Parent',tool.Dialog,'Style','radiobutton','Position',[dborder 5*dborder+5*cheight cwidth cheight],'String','Erase','Callback',@tool.radioErase);
tool.RadioDraw.Value = 1;
% pencil/eraser slider
uicontrol('Parent',tool.Dialog,'Style','text','String','Pencil/Eraser Size','Position',[dborder 4*dborder+4*cheight cwidth cheight],'HorizontalAlignment','left');
tool.PenSizeText = uicontrol('Parent',tool.Dialog,'Style','text','String','5','Position',[dborder+cwidth/2-25 3*dborder+3*cheight 50 cheight],'HorizontalAlignment','center');
uicontrol('Parent',tool.Dialog,'Style','edit','String','10','Position',[dborder+cwidth-50 3*dborder+3*cheight 50 cheight],'HorizontalAlignment','right','Callback',@tool.changeSliderRange,'Tag','sliderMax');
uicontrol('Parent',tool.Dialog,'Style','edit','String','1','Position',[dborder 3*dborder+3*cheight 50 cheight],'HorizontalAlignment','left','Callback',@tool.changeSliderRange,'Tag','sliderMin');
tool.PenSize = 5;
tool.Slider = uicontrol('Parent',tool.Dialog,'Style','slider','Min',1,'Max',10,'Value',tool.PenSize,'Position',[dborder 3*dborder+2*cheight cwidth cheight],'Callback',@tool.sliderManage,'Tag','pss');
addlistener(tool.Slider,'Value','PostSet',@tool.continuousSliderManage);
% class popup
uicontrol('Parent',tool.Dialog,'Style','popupmenu','String',labels,'Position', [dborder 2*dborder+cheight cwidth cheight],'Callback',@tool.popupManage);
% done button
doneButtonLabel = 'Done';
uicontrol('Parent',tool.Dialog,'Style','pushbutton','String',doneButtonLabel,'Position',[dborder dborder cwidth cheight],'Callback',@tool.buttonDonePushed);
uiwait(tool.Dialog)
end
function keyPressed(tool,~,event)
if strcmp(event.Key,'space')
if ~isempty(tool.SecondChannel)
if tool.ShowingFirstChannel
I = tool.SecondChannel;
tool.ImageHandle.CData = tool.applyThresholds(I);
tool.ShowingFirstChannel = false;
else
I = tool.Image;
tool.ImageHandle.CData = tool.applyThresholds(I);
tool.ShowingFirstChannel = true;
end
end
end
end
function T = applyThresholds(tool,I)
T = I;
T(T < tool.LowerThreshold) = tool.LowerThreshold;
T(T > tool.UpperThreshold) = tool.UpperThreshold;
T = T-min(T(:));
T = T/max(T(:));
end
function changeSliderRange(tool,src,~)
value = str2double(src.String);
if strcmp(src.Tag,'sliderMin')
tool.Slider.Min = value;
tool.Slider.Value = value;
tool.PenSize = value;
tool.PenSizeText.String = sprintf('%d',value);
elseif strcmp(src.Tag,'sliderMax')
tool.Slider.Max = value;
tool.Slider.Value = value;
tool.PenSize = value;
tool.PenSizeText.String = sprintf('%d',value);
end
end
function radioDraw(tool,src,~)
tool.RadioErase.Value = 1-src.Value;
end
function radioErase(tool,src,~)
tool.RadioDraw.Value = 1-src.Value;
end
function continuousSliderManage(tool,~,callbackdata)
tag = callbackdata.AffectedObject.Tag;
if strcmp(tag,'pss')
tool.PenSize = round(callbackdata.AffectedObject.Value);
ps = tool.PenSize;
tool.PenSizeText.String = sprintf('%d',ps);
[Y,X] = meshgrid(-ps:ps,-ps:ps);
Mask = sqrt(X.^2+Y.^2) < ps;
r1 = ceil(tool.Axis.YLim(1));
r2 = floor(tool.Axis.YLim(2));
c1 = ceil(tool.Axis.XLim(1));
c2 = floor(tool.Axis.XLim(2));
rM = round(mean(tool.Axis.YLim));
cM = round(mean(tool.Axis.XLim));
tool.TransparencyHandle.AlphaData(max(1,r1):min(size(tool.Image,1),r2),max(1,c1):min(size(tool.Image,2),c2)) = 0;
if r1 >= 1 && r2 <= size(tool.Image,1) && c1 >= 1 && c2 <= size(tool.Image,2) ...
&& rM-ps >= 1 && rM+ps <= size(tool.Image,1) && cM-ps >=1 && cM+ps <= size(tool.Image,2)
tool.TransparencyHandle.AlphaData(rM-ps:rM+ps,cM-ps:cM+ps) = Mask;
end
else
value = callbackdata.AffectedObject.Value;
if strcmp(tag,'uts')
tool.UpperThreshold = value;
tool.UpperThresholdBox.String = sprintf('%.04f', value);
elseif strcmp(tag,'lts')
tool.LowerThreshold = value;
tool.LowerThresholdBox.String = sprintf('%.04f', value);
end
if isempty(tool.SecondChannel) || tool.ShowingFirstChannel
I = tool.Image;
tool.ImageHandle.CData = tool.applyThresholds(I);
else
I = tool.SecondChannel;
tool.ImageHandle.CData = tool.applyThresholds(I);
end
end
end
function sliderManage(tool,src,~)
tool.PenSize = round(src.Value);
set(tool.TransparencyHandle, 'AlphaData', 0.5*tool.LabelMasks(:,:,tool.LabelIndex));
end
function popupManage(tool,src,~)
tool.LabelIndex = src.Value;
set(tool.TransparencyHandle, 'AlphaData', 0.5*tool.LabelMasks(:,:,tool.LabelIndex));
end
function closeDialog(tool,~,~)
delete(tool.Dialog);
delete(tool.Figure);
end
function buttonDonePushed(tool,~,~)
NoOverlap = sum(tool.LabelMasks,3) <= 1;
tool.LabelMasks = tool.LabelMasks.*repmat(NoOverlap,[1 1 tool.NLabels]);
delete(tool.Dialog);
delete(tool.Figure);
end
function closeFigure(tool,~,~)
delete(tool.Figure);
delete(tool.Dialog);
end
function mouseMove(tool,~,~)
p = tool.Axis.CurrentPoint;
col = round(p(1,1));
row = round(p(1,2));
ps = tool.PenSize;
if row > ps && row <= size(tool.Image,1)-ps && col > ps && col <= size(tool.Image,2)-ps && tool.MouseIsDown
[Y,X] = meshgrid(-ps:ps,-ps:ps);
Curr = tool.LabelMasks(row-ps:row+ps,col-ps:col+ps,tool.LabelIndex);
Mask = sqrt(X.^2+Y.^2) < ps;
if tool.RadioDraw.Value == 1
tool.LabelMasks(row-ps:row+ps,col-ps:col+ps,tool.LabelIndex) = max(Curr,Mask);
tool.TransparencyHandle.AlphaData(row-ps:row+ps,col-ps:col+ps) = 0.5*max(Curr,Mask);
elseif tool.RadioErase.Value == 1
tool.LabelMasks(row-ps:row+ps,col-ps:col+ps,tool.LabelIndex) = min(Curr,1-Mask);
tool.TransparencyHandle.AlphaData(row-ps:row+ps,col-ps:col+ps) = min(Curr,0.5*(1-Mask));
end
end
end
function mouseDown(tool,~,~)
tool.MouseIsDown = true;
end
function mouseUp(tool,~,~)
tool.MouseIsDown = false;
end
end
end