-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlyExtract.m
More file actions
executable file
·294 lines (255 loc) · 8.84 KB
/
FlyExtract.m
File metadata and controls
executable file
·294 lines (255 loc) · 8.84 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
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
287
288
289
290
291
292
293
294
function FlyExtract(type, frame1, frame2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%[Program Name]
% FlyExtract.m
%[Description]
% Extract Fly Body and Wings from Image Sequences
%[Usage]
% exp002: FlyExtract(0, 1, 1775)
% exp035: FlyExtract(0, 1, 223)
% exp083: FlyExtract(0, 1, 640)
% exp098: FlyExtract(0, 1, 569)
% exp101: FlyExtract(0, 1, 494)
%[Programmer]
% Atsushi Yamashita (Shizuoka University)
%[Development Environment]
% Matlab R2007a
%[Version]
% ver.1.0 06/08/2007 Initial Version (exp002)
% ver.1.1 06/12/2007 exp002, exp035, exp83, exp098, exp101
%[Input]
% type =0: All cameras (Camera 1-3)
% =1: Camera 1
% =2: Camera 2
% =3: Camera 3
% frame1 first file number
% frame2 final file number
%[Return]
% Nothing
%[Output]
% Results of fly extraction (jpeg files)
%[Comments]
% This is my first matlab program ...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%[variable Name]
% cam Camera number
% frame Frame number
% p_frame Frame number for iteration
% exp_name Experiment name
% input_filename Input file name
% dir_name Input and output directory name
% output_dir_name Output directory name
% output_filename Output file name
% level(cam) Threshold values for binarize
% Input(i,j,cam) Original (input) image
% Image(i,j,cam) Image after contrast enhancement
% Background(i,j,cam) Background image
% Subtract(i,j,cam) Subtract image
% TempImage(i,j,cam) Temporal image
% FlyAll(i,j,cam) Fly body and wings
% FlyBody(i,j,cam) Fly body
% FlyWing(i,j,cam) Fly wings
% Final1(i,j,color,cam) Image for generating final result 1
% Final2(i,j,color,cam) Image for generating final result 2
% Composite(i,j,color,cam) Composite image for final result
% PrevImage(i,j,cam,num) Previous image
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initial Setting
% Measure computation time (start)
tic;
% Input and output directory name (Version name)
dir_name = 'result_1_1';
% Experiment number
exp_name = 'exp002';
% exp_name = 'exp035';
% exp_name = 'exp083';
% exp_name = 'exp098';
% exp_name = 'exp101';
% Threshold value for binarize
LEVEL = 0.15;
% Generate directory for output images
output_dir_name = sprintf('%s/%s', dir_name, exp_name);
mkdir(output_dir_name);
mkdir(output_dir_name, 'results');
% Logical multiply number
if exp_name == 'exp002'
and_number = 16;
elseif exp_name == 'exp035'
and_number = 0;
elseif exp_name == 'exp083'
and_number = 0;
elseif exp_name == 'exp098'
and_number = 0;
elseif exp_name == 'exp101'
and_number = 0;
else
error('Incorrect input ... exp_name %s', exp_name);
end
% Initialize
PrevImage = zeros(512,512,and_number,3);
% All cameras
if type == 0
cam1 = 1;
cam2 = 3;
sprintf('Camera 1-3 ... Generating background images')
mkdir(output_dir_name, 'cam-all');
% Camera 1
elseif type == 1
cam1 = 1;
cam2 = 1;
sprintf('Camera 1 ... Generating background image')
mkdir(output_dir_name, 'cam1');
% Camera 2
elseif type == 2
cam1 = 2;
cam2 = 2;
sprintf('Camera 2 ... Generating background image')
mkdir(output_dir_name, 'cam2');
% Camera 3
elseif type == 3
cam1 = 3;
cam2 = 3;
sprintf('Camera 3 ... Generating background image')
mkdir(output_dir_name, 'cam3');
% Incorrect Input
else
error('Incorrect Input');
end
% Background image generation
Background = MakeBackground(dir_name, exp_name);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Iteration from frame1 to frame2
for frame=frame1:frame2
% Display frame number
sprintf('Frame [%04d]', frame)
% Load images
for cam=cam1:cam2
input_filename = sprintf('./../0Data/FlyTracker/%s/cam%03d/%s%06d.bmp', ...
exp_name, cam, exp_name, frame);
Input(:,:,cam) = imread(input_filename);
end
% Contrast enhancement
for cam=cam1:cam2
Image(:,:,cam) = imadjust(Input(:,:,cam));
end
% Image substraction (background removal)
for cam=cam1:cam2
Subtract(:,:,cam) = imsubtract(Background(:,:,cam), Image(:,:,cam));
end
% Contrast enhancement
for cam=cam1:cam2
TempImage(:,:,cam) = imadjust(Subtract(:,:,cam));
Subtract(:,:,cam) = TempImage(:,:,cam);
end
% Convert images to binary images
for cam=cam1:cam2
% level(cam) = graythresh(Subtract(:,:,cam));
% FlyAll(:,:,cam) = im2bw(Subtract(:,:,cam), level(cam)*0.35);
FlyAll(:,:,cam) = im2bw(Subtract(:,:,cam), LEVEL);
end
% Noise removal (Median filtering)
for cam=cam1:cam2
TempImage(:,:,cam) = medfilt2(FlyAll(:,:,cam), [3 3]);
FlyAll(:,:,cam) = TempImage(:,:,cam);
end
% Noise removal
if exp_name == 'exp002'
% Bottom platform in camera 2 images
FlyAll(450:512, 1:512, 2) = 0;
elseif exp_name == 'exp035'
% Glass in camera 2 image
FlyAll(1:512, 365:512, 2) = 0;
% Noise in camera 3 image
if frame > 120
FlyAll(1:512, 300:512, 3) = 0;
end
elseif exp_name == 'exp101'
% Bottom platform in camera 2 images
% FlyAll(440:512, 1:512, 2) = 0;
end
% Fly body and wing separation
for cam=cam1:cam2
FlyBody(:,:,cam) = FlyAll(:,:,cam);
for p_frame=1:and_number
TempImage(:,:,cam) = FlyBody(:,:,cam) & PrevImage(:,:,p_frame,cam);
FlyBody(:,:,cam) = TempImage(:,:,cam);
end
FlyWing(:,:,cam) = imsubtract(FlyBody(:,:,cam), FlyAll(:,:,cam));
end
% Noise removal (Median filtering)
for cam=cam1:cam2
TempImage(:,:,cam) = medfilt2(~FlyWing(:,:,cam), [3 3]);
FlyWing(:,:,cam) = ~TempImage(:,:,cam);
end
% Generation of final results
for cam=cam1:cam2
for color=1:3
Final1(:,:,color,cam) = Input(:,:,cam);
Final2_1(:,:,color,cam) = zeros(512,512);
Final2_2(:,:,color,cam) = zeros(512,512);
end
% Blue body & red wings
Final2_1(:,:,1,cam) = FlyBody(:,:,cam) .* 255;
Final2_1(:,:,2,cam) = FlyBody(:,:,cam) .* 255;
Final2_2(:,:,2,cam) = FlyWing(:,:,cam) .* 255;
Final2_2(:,:,3,cam) = FlyWing(:,:,cam) .* 255;
Final2(:,:,:,cam) = ones(512,512,3) .* 255 - Final2_1(:,:,:,cam) - Final2_2(:,:,:,cam);
end
% Camera 1 or 2 or 3
if (type ==1) | (type == 2) | (type == 3)
% Image composition
for cam=cam1:cam2
Composition(:,:,:,cam) = [Final1(:,:,:,cam) Final2(:,:,:,cam)];
end
% Save results as jpeg files
for cam=cam1:cam2
output_filename = sprintf('./%s/%s/cam%01d/extract%04d.jpg', ...
dir_name, exp_name, cam, frame);
imwrite(Composition(:,:,:,cam), output_filename, 'jpg');
end
% All cameras
else
% Image composition
Composition(:,:,:,cam) = [Final1(:,:,:,1) Final1(:,:,:,2) Final1(:,:,:,3); ...
Final2(:,:,:,1) Final2(:,:,:,2) Final2(:,:,:,3)];
% Save results as jpeg files
output_filename = sprintf('./%s/%s/cam-all/extract%04d.jpg', ...
dir_name, exp_name, frame);
imwrite(Composition(:,:,:,cam), output_filename, 'jpg');
end
% Reserve results for next frame processing
for cam=cam1:cam2
for p_frame =and_number:-1:2
PrevImage(:,:,p_frame,cam) = PrevImage(:,:,p_frame-1,cam);
end
PrevImage(:,:,1,cam) = FlyAll(:,:,cam);
end
end
% Measure computation time (end)
toc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generate avi file
if type == 0
if exp_name == 'exp035'
if frame2 > 178
frame2 = 178;
end
elseif exp_name == 'exp083'
if frame1 < 350
frame1 = 350;
end
elseif exp_name == 'exp098'
if frame1 < 160
frame1 = 160;
end
if frame2 > 530
frame2 = 530;
end
end
% Generate avi file
elapsed_time = MakeAvi(exp_name, 1, frame1+and_number, frame2);
sprintf('Computation time for generating avi is %d [sec]', elapsed_time)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%