-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_new_gp_track.m
More file actions
235 lines (170 loc) · 5.31 KB
/
test_new_gp_track.m
File metadata and controls
235 lines (170 loc) · 5.31 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
clear
clc
close all
%% Get camera
alt_lab = false;
imaqreset;
if ~alt_lab
vid = videoinput('pointgrey', 1, 'F7_BayerRG8_752x480_Mode0');
src = getselectedsource(vid);
src.WhiteBalanceRBMode = 'manual';
src.WhiteBalanceRB = [419 719];
src.BrightnessMode = 'Manual';
src.Brightness = 186;
src.Gain = 0.527;
src.Exposure = 33;
src.Gamma = 0;
src.FrameRateMode = 'manual';
src.FrameRate = 30;
else
vid = videoinput('winvideo', 1, 'RGB24_1280x960');
src = getselectedsource(vid);
vid = videoinput('winvideo', 1, 'RGB24_1280x960');
src = getselectedsource(vid);
vid.FramesPerTrigger = 1;
triggerconfig(vid, 'manual');
% TriggerRepeat is zero based and is always one
% less than the number of triggers.
vid.TriggerRepeat = Inf;
%start(vid);
end
%configure camera parameters
flushdata(vid);
vid.FramesPerTrigger = inf;
drawnow;
% start(vid);
%% configure color model
%default model looks for green
% Base_Dir = pwd;
% load(fullfile(Base_Dir,'color_model_main.mat'),...
% 'color_model','gr_mdl','num_clust','num_shade');
%
% colors = [[210 240 210];[220 250 220]; color_model;gr_mdl]; %doesn't do so hot with bright white
% color_labels = (num_clust+(1:num_shade));
% cl_thresh = min(color_labels); %indicates in label matrix where GP colors begin
figure(1)
clf(1)
set(gca,'xtick',[]);
set(gca,'xticklabel',{});
set(gca,'ytick',[]);
set(gca,'yticklabel',{});
drawnow;
animal_name = 'test';
base_dir = pwd;
color_dir = 'Color_Data';
color_save = fullfile(base_dir,color_dir);
if ~exist(color_save,'dir')
mkdir(color_save);
end
color_model_file = fullfile(color_save,sprintf('color_model_%s*.mat',animal_name));
file_list = dir(color_model_file);
if ~isempty(file_list)
model = load(fullfile(color_save,file_list(1).name),'color_model','cl_thresh');
color_model = single(model.color_model);
cl_thresh = single(model.cl_thresh);
TRAIN_MODEL = false;
else
TRAIN_MODEL = true;
color_model = [];
cl_thresh = [];
end
SAVE_VIDS = false;
start_time = tic;
tot_frames = 1000;
control_var = true;
frame_iter = 1;
while control_var
%% acquire new data for training
if TRAIN_MODEL
%% run tracker
start(vid);
%
drawnow;
% flushdata(vid);
% drawnow;
if alt_lab
trigger(vid);
pause(1.5);
flushdata(vid);
drawnow;
end
%
while ~vid.FramesAvailable
end
%get image
im = getdata(vid);
im = im(:,:,:,end);
%im = imresize(im,0.5);
stop(vid);
imshow(im);
drawnow;
uiwait(msgbox('Click on background'));
[x_back,y_back] = getpts(figure(1));
back_model = [];
for idx = 1:length(x_back)
back_model(idx,:) = im(floor(y_back(idx)),floor(x_back(idx)),:);
end
uiwait(msgbox('Click on guinea pig colors'));
[x_gp,y_gp] = getpts(figure(1));
gp_model = [];
for idx = 1:length(x_gp)
gp_model(idx,:) = im(floor(y_gp(idx)),floor(x_gp(idx)),:);
end
%model parameters
color_model = [back_model; gp_model];
cl_thresh = min(length(x_back)+(1:length(x_gp)));
%find color
gp_pts = find_animal(im,color_model,cl_thresh);
%% recolor image
im_marked = insert_color_markers(im,gp_pts);
[xgp,ygp] = find(gp_pts==1);
im_marked = insertMarker(im_marked,median([ygp xgp]),...
'Marker','x-mark','Color','r');
clf(1);
imshow(im_marked);
drawnow;
resp = inputdlg('Enter "bad" to retry','Track accuracy',[1 35],{'bad'});
if cellfun(@(x)~contains(lower(x),'bad'),resp,'uniformoutput',true)
control_var = false;
color_model_file = strrep(color_model_file,'*',datestr(now,'_mmm-dd-yyyy'));
save(color_model_file,'color_model','cl_thresh');
end
else
%% run tracker
if ~isrunning(vid)
start(vid);
flushdata(vid);
end
if alt_lab
trigger(vid);
pause(1.5)
flushdata(vid);
drawnow;
end
%flushdata(vid);
%get image
while ~vid.FramesAvailable
end
im = getdata(vid,1);
im = im(:,:,:,end);
im = imresize(im,0.5);
frame_iter = frame_iter + 1;
%find color
gp_pts = find_animal(im,color_model,cl_thresh);
%% recolor image
im = insert_color_markers(im,gp_pts);
[xgp,ygp] = find(gp_pts==1);
%get rid of outliers from centroid?
im = insertMarker(im,median([ygp xgp]),...
'Marker','x-mark','Color','r');
clf(1);
imshow(im);
drawnow;
if frame_iter >= tot_frames
control_var = false;
end
end
end
if isrunning(vid)
stop(vid);
end