forked from mktiede/GetContours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DotsPlace.m
288 lines (250 loc) · 8.59 KB
/
DotsPlace.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
function [p,ih] = DotsPlace(src, varargin)
%DOTSPLACE - place tracking locations on image
%
% usage: [p,ih] = DotsPlace(src, ...)
%
% use this procedure in tandem with DOTSTRACK to track image features of interest
% through an image sequence
%
% for a new set of points:
% click to add a new point (origin is ULC)
% click and drag an existing point to change its position
% right click on an existing point to change its name
% double-click on an existing point to delete
%
% for an existing set of points:
% click and drag on the existing points to adjust their location
% red points have failed tracking threshold
%
% the procedure blocks the command line until the "CONTINUE" button is clicked
%
% image SRC can be any one of the following:
% movie filename (e.g. 'foo.mov')
% movieHandle specifying an open movie handle
% [nRows x nCols (xRGB)] image array
%
% optional supported 'NAME',VALUE pairs:
% EDIT - allow editing of existing point array if 'T' (default 'F')
% IH - image handle to reuse (default opens a new figure sized to SRC)
% FRAME - 1-based frame number to load from a movie sequence (default 1)
% P0 - array of existing points ([1 x nPoints]; default none);
% XFORM - conditioning function applied to each frame (e.g., @(img) 255-uint8(mean(img,3)))
%
% returns updated points array
% optionally returns image handle IH
%
% points array is an array-of-structs [1 x nPoints] with fields
% POS - [X,Y] (pixel coordinates, relative to ULC origin)
% LABEL - point id string
% FRAME - 1-based frame number of fitted image
% TIME - 0-based offset of frame from beginning of movie in seconds
% STATUS - 0: valid, 1: failed tracking step; 2: user flagged invalid point (tracking skipped)
% CONF - tracking confidence value
%
% see also DOTSTRACK, DOTSPLOT
% mkt 07/15
% mkt 10/15 support for names and structured points arrays
%----- branch by action
if nargin > 2 && ~ischar(varargin{1}),
action = varargin{2};
else,
action = 'INIT';
end;
switch action,
%----- DOWN - mouse down handler
case 'DOWN',
newPts = get(gca,'UserData'); % nonzero: permit creation, deletion
% clicked on image: make new point
if strcmpi(get(src,'Type'),'image'),
if ~newPts, return; end; % if only point moves allowed
xy = get(gca,'CurrentPoint');
xy = floor(xy(1,1:2));
lh = line(xy(1),xy(2),'marker','+','color','g','tag','DPLC','userdata',[],'buttonDownFcn',{@DotsPlace,'DOWN'});
% clicked on existing point
else,
if strcmp(get(src,'type'),'text'),
th = src;
lh = get(th,'userdata');
else,
lh = src;
th = get(lh,'userdata');
end;
switch get(gcbf,'SelectionType'),
case 'open', % double-click
if newPts,
if ~isempty(th), delete(th); end;
delete(lh); % delete existing point
end;
return;
case 'normal', % no mod -- fall thru
otherwise, % change label name
if newPts,
name = get(th,'string');
name(1:2) = [];
set(th,'string',[' ',GetName(name)]);
return;
end;
end;
end;
set(gcbf, ... % set motion handlers
'WindowButtonMotionFcn', {@DotsPlace,'MOVE',lh}, ...
'WindowButtonUpFcn', {@DotsPlace,'UP',lh}, ...
'Pointer', 'crosshair');
%----- INITialze
case 'INIT',
if nargin < 1, eval('help DotsPlace'); return; end
% parse args
ih = []; frame = []; p0 = []; allowEdit = 0; xform = [];
for ai = 2 : 2 : length(varargin),
switch upper(varargin{ai-1}),
case 'EDIT', allowEdit = strcmpi(varargin{ai}(1),'T');
case 'IH', ih = varargin{ai};
case 'FRAME', frame = varargin{ai};
case 'P0', p0 = varargin{ai};
case 'XFORM', xform = varargin{ai};
otherwise, error('unrecognized parameter (%s)', varargin{ai-1});
end;
end;
if isempty(frame), frame = 1; end;
% load and display image
if ischar(src), % movie filename
try, mh = VideoReader(src); catch, error('unable to open movie file %s', src); end;
img = GetMovieFrame(mh, frame);
if ~isempty(xform), f = xform{1}; a = xform(2:end); img = f(img,a{:}); end;
t = (frame-1)/mh.FrameRate;
elseif ishandle(src), % open movie handle
img = GetMovieFrame(src, frame);
if ~isempty(xform), f = xform{1}; a = xform(2:end); img = f(img,a{:}); end;
t = (frame-1)/mh.FrameRate;
else, % explicit image
img = src;
if ~isempty(xform), f = xform{1}; a = xform(2:end); img = f(img,a{:}); end;
t = 0;
end;
if isempty(ih), % existing image handle
[~,ih] = implot(img);
else, % construct new figure
set(ih,'cdata',img);
end;
ah = get(ih,'Parent'); % "get" provides backwards compatibility
fh = get(ah,'Parent');
% add buttons
bh = uicontrol(fh,'style','pushbutton','position',[5 5 60 20],'String','CONTINUE','callback','uiresume(gcbf)');
% init buttonDown event handling
if isempty(p0), % no previous points: allow create, move, delete, rename
allowEdit = 1;
set(ih,'buttonDownFcn',{@DotsPlace,'DOWN'});
set(ah,'userData',1); % flag point creation, deletion allowed
bh(2) = uicontrol(fh,'style','pushbutton','position',[5 25 60 20],'String','CLRALL','callback','delete(findobj(gca,''tag'',''DPLC''))');
else, % previous points
set(ah,'userData',allowEdit); % flag mvt only unless editing permitted
if allowEdit, set(ih,'buttonDownFcn',{@DotsPlace,'DOWN'}); end;
col = 'grkm'; % good, bad track, invalid, bad+invalid
for li = 1 : length(p0),
if isempty(p0(li).STATUS), c = 1; else, c = p0(li).STATUS+1; end;
lh = line(p0(li).POS(1),p0(li).POS(2),'color',col(c),'marker','+','tag','DPLC','buttonDownFcn',{@DotsPlace,'DOWN'});
if allowEdit, s = sprintf(' %s',p0(li).LABEL); else, s = sprintf(' %d %s',li,p0(li).LABEL); end;
th = text(p0(li).POS(1),p0(li).POS(2),s,'color','w','userdata',lh,'tag','DPLC','interpreter','none','buttonDownFcn',{@DotsPlace,'DOWN'});
set(lh,'userdata',th);
end;
end;
% block command line until "CONTINUE" clicked
uiwait(fh);
if ~ishandle(fh), p = p0; return; end; % closing window returns original points
% update points
lh = flipud(findobj(gca,'marker','+','tag','DPLC'));
if length(lh) && (isempty(p0) || allowEdit),
p(1,length(lh)) = struct('POS',[],'LABEL',[],'FRAME',[],'TIME',t,'STATUS',[],'CONF',[]);
else,
p = p0;
end;
for li = 1 : length(lh),
x = get(lh(li),'XData');
y = get(lh(li),'YData');
p(li).POS = [x,y];
if allowEdit,
s = get(get(lh(li),'userdata'),'string');
if length(s)>2, s(1:2) = []; end;
p(li).LABEL = s;
end;
end;
% clean up and exit
delete(bh);
delete(findobj(gca,'tag','DPLC'));
return;
%----- MOVE - mouse mvt handler
case 'MOVE',
xy = get(gca, 'CurrentPoint');
xy = floor(xy(1,1:2));
lh = varargin{3};
if ~ishandle(lh), return; end;
x = get(lh,'XData');
y = get(lh,'YData');
[~,k] = min(sqrt((x-xy(1)).^2+(y-xy(2)).^2));
if x(k) ~= xy(1) || y(k) ~= xy(2),
x(k) = xy(1); y(k) = xy(2);
set(lh, 'XData',x, 'YData',y);
th = get(lh,'userdata');
if ~isempty(th), set(th,'position',[x y 0]); end;
end;
%----- UP - mouse up handler
case 'UP',
set(gcbf, ... % clear motion handlers
'WindowButtonMotionFcn', '', ...
'WindowButtonUpFcn', '', ...
'Pointer', 'arrow');
lh = varargin{3};
th = get(lh,'userdata');
if get(gca,'UserData') && isempty(th), % add label if new point
x = get(lh,'XData'); y = get(lh,'YData');
name = GetName('');
th = text(x,y,[' ',name],'color','w','tag','DPLC','userdata',lh,'buttonDownFcn',{@DotsPlace,'DOWN'});
set(lh,'userdata',th);
end;
%----- error
otherwise,
error('DOTSPLACE: unknown parameter (%s)', action);
end;
%=============================================================================
% GETNAME - edit dot name
function name = GetName(name)
width = 250;
height = 100;
ts = 'Enter Point Name';
fh = gcf;
units = get(fh, 'units');
if strcmpi(units,'pixels'),
units = [];
else,
set(fh, 'units','pixels');
end;
fPos = get(fh, 'position');
if ~isempty(units), set(fh, 'units',units); end;
pos = [fPos(1)+(fPos(3)-width)/2 , fPos(2)+(fPos(4)-height)/2 , width , height];
cfg = dialog('Name', ts, ...
'menubar', 'none', ...
'Position', pos, ...
'UserData', 0);
% name field
eh = uicontrol(cfg, ...
'Position', [20 60 width-40 25], ...
'Style', 'edit', ...
'HorizontalAlignment', 'left', ...
'String', name);
% OK, cancel buttons
uicontrol(cfg, ... % buttons
'Position',[width/2-70 15 60 25], ...
'String','OK', ...
'Callback','set(gcbf,''UserData'',3);uiresume');
uicontrol(cfg, ...
'Position',[width/2+10 15 60 25], ...
'String','Cancel', ...
'Callback','uiresume');
% wait for input
drawnow;
uicontrol(eh);
uiwait(cfg);
if get(cfg, 'UserData'),
name = get(eh, 'string');
end;
delete(cfg);