-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIm2P_CreateFilesForFicTrac.m
261 lines (219 loc) · 8.25 KB
/
Im2P_CreateFilesForFicTrac.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
%Im2P_CreateFilesForFicTrac checks if the las mask is correct, and if not - asks the
%user to create a new mask. Create a new folder for FicTrac input/output
%file for this date
%Nore: is CheckMask=0, the last modified *MASK* file in FicTracPath will be
%used, so do CheckMask=0 only if you are SURE that this mask is good.
%Any doubt? Do CheckMask = 1
%%To fix:
%It adds line to the config line each time a new one is created...
%Normalize VFOV?
function Im2P_CreateFilesForFicTrac(CheckMask)
%Parameters
FicTracPath = 'E:\FicTracWin64';
vfov = 2;%need to calibrate with ball radius!
%CheckMask must be 0 or 1
if ~(CheckMask == 0 || CheckMask == 1)
disp('CheckMask must be 0 or 1. Returning')
return
end
%FicTrac main folder path
CurrDir = cd(FicTracPath);
%Upload the last mask and snap files
%MASK
MASKfiles = dir('*mask*.png');
DateModified = zeros(1,size(MASKfiles,1));
for nMaskFile = 1:size(MASKfiles,1)
DateModified(nMaskFile) = datenum(MASKfiles(nMaskFile).date);
end
MaskFile = fullfile(pwd,MASKfiles(DateModified == max(DateModified)).name);
MASK_filename = MaskFile;%use the last mask modified in FicTracPath (default, if not changed later)
MASK = imread(MaskFile);
if size(MASK,3) == 3%if the image file is a true RGB image file, convert
MASK = rgb2gray(MASK);
end
if CheckMask
%SNAP
SNAPfiles = dir('*snap*');
DateModified = zeros(1,size(SNAPfiles,1));
for nSnapFile = 1:size(SNAPfiles,1)
DateModified(nSnapFile) = datenum(SNAPfiles(nSnapFile).date);
end
SnapFile = fullfile(pwd,SNAPfiles(DateModified == max(DateModified)).name);
SNAP = rgb2gray(imread(SnapFile));
%show snap with transparent mask overlaid
figure(1); clf
colormap(gray)
C = imfuse(SNAP,MASK,'blend');
imshow(C)
%Updated mask by user if needed
prompt = 'Mask OK? Y/N [Y]: ';
while 1
IsOK = input(prompt,'s');
if isempty(IsOK)
IsOK = 'Y';
end
if strcmpi(IsOK,'N') || strcmpi(IsOK,'Y'), break, end
disp('Wrong input value, Y/N only')
end
else
IsOK = 'Y';
end
%create new mask
if strcmpi(IsOK,'N')
MaskOK = 0;
imageSizeX = size(SNAP,2);
imageSizeY = size(SNAP,1);
[columnsInImage, rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
while 1
%user defines ball circle
disp('Define ball borders')
fig = figure(1); clf(fig)
imshow(SNAP)
[x, y] = getpts(fig);
[xc,yc,R,~] = circfit(x,y);
config_shpere_centre_px = ['sphere_centre_px ',num2str(round(xc,2)),' ',num2str(round(yc,2))];
config_sphere_radius_px = ['sphere_radius_px ',num2str(round(R,2))];
disp(config_shpere_centre_px)
disp(config_sphere_radius_px)
BallPixels = (rowsInImage - yc).^2 ...
+ (columnsInImage - xc).^2 <= R.^2;
BallMASK = BallPixels;% circlePixels is white - mask on
%user defines glare circle
disp('Define glare borders')
fig = figure(1); clf(fig)
imshow(SNAP)
[x, y] = getpts(fig);
[xc,yc,R,~] = circfit(x,y);
glarePixels = (rowsInImage - yc).^2 ...
+ (columnsInImage - xc).^2 <= R.^2; % circlePixels is a 2D "logical" array.
GlareMASK = 1 - glarePixels;%so that the glare is black - mask off
%user defines ball holder
while 1
disp('Define holder rectangle (4 points)')
fig = figure(1); clf(fig)
imshow(SNAP)
[x, y] = getpts(fig);
if length(x) == 4
break
else
disp('4 points please..')
end
end
%fix x,y such that:
%(1) straight line under the ball (in any image orientation)
%(2) black all the way to the bottom (just in case the bottom was marked wrongly)
if all(x>xc)%air stream axis: left
[~,idx] = sort(x);
x(idx(1:2)) = min(x(idx(1:2)));
x(idx(3:4)) = size(SNAP,2);
elseif all(x<xc)%air stream axis: right
[~,idx] = sort(x);
x(idx(3:4)) = max(x(idx(3:4)));
x(idx(1:2)) = 1;
elseif all(y<yc)%air stream axis: up
[~,idx] = sort(y);
y(idx(1:2)) = min(y(idx(1:2)));
y(idx(3:4)) = size(SNAP,1);
elseif all(y>yc)%air stream axis: down
[~,idx] = sort(y);
y(idx(3:4)) = max(y(idx(3:4)));
y(idx(1:2)) = 1;
else%don't change coordinates, but possibly something's wrong
disp('ball orientation unclear, cant fix ball holder coordinates.')
end
BallHolder = poly2mask(x, y, size(SNAP,1), size(SNAP,2));
BallHolderMASK = 1 - BallHolder; %so that the ball holder is black - mask off
%make mask
MASK = BallMASK & GlareMASK & BallHolderMASK;
fig = figure(1); clf(fig)
C = imfuse(SNAP,MASK,'blend');
imshow(C)
prompt = 'Mask OK? Y/N [Y]: ';
while 1
IsOK = input(prompt,'s');
if isempty(IsOK)
IsOK = 'Y';
end
if strcmpi(IsOK,'N') || strcmpi(IsOK,'Y'), break, end
disp('Wrong input value, Y/N only')
end
if strcmpi(IsOK,'y'), MaskOK = 1; end
if MaskOK == 1
MASK_filename = fullfile(pwd,'online_MASK.png');%will be updated and used for FicTrac online tracking
imwrite(MASK,MASK_filename)
break
end
end
end
%create a folder for the current date and session
prompt = 'Session number? ';
while 1
SessionNumber = input(prompt,'s');%I do it this way to prevent error for non numeric input
SessionNumber = str2double(SessionNumber);
if ~isnan(SessionNumber), break, end
disp('Input must be a number')
end
DATE = char(datetime('now','Format','yyMMdd'));
Folder = [DATE,'_',num2str(SessionNumber)];
if isdir(Folder)
prompt = ['Folder ',Folder,' already exist,override? Y/N [Y]: '];
while 1
IsContinue = input(prompt,'s');
if isempty(IsContinue)
IsContinue = 'Y';
end
if strcmpi(IsContinue,'N') || strcmpi(IsContinue,'Y'), break, end
disp('Wrong input value - Y/N only')
end
if strcmpi(IsContinue,'N'), return, end
else
mkdir(Folder)
end
%At this point the mask is updated and a folder was created for the session
config_filename = fullfile(pwd,'configOnline.txt');%will be updated and used for FicTrac online tracking
copyof_config_filename = fullfile(pwd,Folder,['configOnline_',DATE,'_',num2str(SessionNumber),'.txt']);%will be saved in the session folder
%MASK_filename is defined earlier as MASK_filename = fullfile(pwd,'online_MASK.png');%will be updated and used for FicTrac online tracking
copyof_MASK_filename = fullfile(pwd,Folder,['MASK_',DATE,'_',num2str(SessionNumber),'.png']);%will be saved in the session folder
calibration_filename = fullfile(pwd,'calibration-transform.dat');%will be updated and used for FicTrac online tracking
copyof_calibration_filename = fullfile(pwd,Folder,'calibration-transform.dat');%will be saved in the session folder
%modify config file
output_fn = ['.\output_',DATE,'_',num2str(SessionNumber)];
mask_fn = ['.\MASK_',DATE,'_',num2str(SessionNumber),'.png'];
transform_fn = '.\calibration-transform.dat';
%previous file: read
fid = fopen(config_filename);
%new file: right
new_fid = fopen(copyof_config_filename,'wt');
while 1
tline = fgets(fid);
if ~isempty(strfind(tline,'output_fn'))
newline = ['output_fn ',output_fn];
fprintf(new_fid, '%s\n',newline);
elseif ~isempty(strfind(tline,'mask_fn'))
newline = ['mask_fn ',mask_fn];
fprintf(new_fid, '%s\n',newline);
elseif ~isempty(strfind(tline,'transform_fn'))
newline = ['transform_fn ',transform_fn];
fprintf(new_fid, '%s\n',newline);
elseif (~isempty(strfind(tline,'vfov')) && exist('config_shpere_centre_px','var'))%MASK updated
newline = ['vfov ',num2str(vfov)];
fprintf(new_fid, '%s\n',newline);
newline = config_shpere_centre_px;
fprintf(new_fid, '%s\n',newline);
newline = config_sphere_radius_px;
fprintf(new_fid, '%s\n',newline);
elseif ~isempty(strfind(tline,'do_socket_out'))
newline = 'do_socket_out 0';
fprintf(new_fid, '%s\n',newline);
break
else
fprintf(new_fid, '%s',tline);
end
end
fclose(fid);
fclose(new_fid);
%copy files
copyfile(MASK_filename,copyof_MASK_filename)
copyfile(calibration_filename,copyof_calibration_filename)
cd(CurrDir)
end