forked from ElTinmar/ClassifyMvt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyzeBehaviorVideo.m
258 lines (207 loc) · 6.97 KB
/
AnalyzeBehaviorVideo.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
clear all; close all; clc
% initialize pseudo-random number generator to always get same sequence
rng();
%% STEP 0 : Get movie and timestasmp files
%--------------------------------------------------------------------------
[file, path] = uigetfile('*.avi','Select movie file');
movieFile = fullfile(path,file);
[file, path] = uigetfile('*.txt','Select timestamps file');
timestampsFile = fullfile(path,file);
[framenum, timestamps] = readvars(timestampsFile);
%% STEP 1 : Backround extraction
%--------------------------------------------------------------------------
mov = VideoReader(movieFile);
% WARNING this takes a long time (has to go through the whole video)
numFrames = mov.NumFrames;
height = mov.Height;
width = mov.Width;
method = 0;
% 0: very slow but should be more reliable
% 1: fast and dirty, but may work as well
if method == 0
% WARNING: random access to frames in h264 encoded video is very slow
% tune samplingPeriodAvg to not get too many frames and store video
% on a SSD
% get equally spaced frame samples from the whole recording
numSamplesAvg = 100;
indAvg = round(linspace(1,numFrames-1,numSamplesAvg));
elseif method == 1
% alternatively, don't do the average but use only the first frame
numSamplesAvg = 1;
indAvg = 1;
end
avgImg = zeros(height,width,'single');
current_frame = 0;
for i=1:numel(indAvg)
i
% NOTE
% using mov.read(index) starts at the beginning of video each time
% using mov.readFrame() like bellow allows to read sequentially (faster)
while (current_frame ~= indAvg(i))
img = mov.readFrame();
current_frame = current_frame+1;
end
imgGrayScaled = im2single(img(:,:,1));
avgImg = avgImg+imgGrayScaled/numSamplesAvg;
end
% select the outlines of the larva to remove it from the picture of the
% background
f = figure
bckg = roifill(avgImg);
close(f)
% select start and end of the tail
f = figure
imshow(avgImg)
[x,y] = ginput(2);
xOrigin = x(1);
yOrigin = y(1);
Len = sqrt((x(2)-x(1))^2+(y(2)-y(1))^2);
close(f)
% test threshold values
threshBW = 0.025; % pixel intensity cutoff
threshSize = 400; % min size of the object (surface) in pixels
gamma = 1.5; % non linearity, improve contrast
sigmaXY = 5; % size of gaussian filter in pixels
radius = 10; % typical size (pixels) used for closing gaps in the tail
% check that the tail os properly extracted at different times in the
% video, update thresholds if necessary
N = 100;
if method == 0
% read random image WARNING SLOW
% sort images to read sequentially (faster)
ids = sort(randi(numFrames,N,1));
elseif method == 1
% read random image at the beginning of the video
% WARNING: may not be representative if lighting conditions
% change accross the video
ids = sort(randi([2 1000],N,1));
end
mov = VideoReader(movieFile); % rewind at the beginning of the movie
h=fspecial('gaussian',sigmaXY,sigmaXY);
se = strel('disk',radius,0);
f = figure;
current_frame = 0;
for i = 1:numel(ids)
% read image
while (current_frame ~= ids(i))
img = mov.readFrame();
current_frame = current_frame+1;
end
imgGrayScaled = im2single(img(:,:,1));
% remove background abd apply an exponent
noback = (abs(imgGrayScaled-bckg)).^gamma;
% spatial gaussian filer to smooth the image
noback = imfilter(noback,h,'replicate');
% binarize and keep only big blobs
BW = (noback>threshBW);
BW = bwareaopen(BW,threshSize);
% close gaps in the tail (image dilation + erosion)
BW = imclose(BW,se);
% plot the result
subplot(2,1,1);
imagesc(noback);
axis image
subplot(2,1,2);
imagesc(BW);
axis image
pause
end
close(f)
%% STEP 2 : Compute curvature
%--------------------------------------------------------------------------
Curv=nan(1,numFrames);
Ang=nan(1,numFrames);
Area=nan(1,numFrames);
perc_disp = 5; % display progression each X percent
mov = VideoReader(movieFile); % rewind at the beginning of the movie
current_frame = 0;
while mov.hasFrame()
img = mov.readFrame();
current_frame = current_frame+1;
% display progression
if (mod(current_frame,round(numFrames*perc_disp/100))==0)
disp([num2str(round(100*current_frame/numFrames)) ' %'])
end
imgGrayScaled = im2single(img(:,:,1));
% remove background abd apply an exponent
noback = (abs(imgGrayScaled-bckg)).^gamma;
% spatial gaussian filer to smooth the image
noback = imfilter(noback,h,'replicate');
% binarize and keep only big blobs
BW = (noback>threshBW);
BW = bwareaopen(BW,threshSize);
% close gaps in the tail (image dilation + erosion)
BW = imclose(BW,se);
Area(current_frame) = sum(BW(:));
[Curv(current_frame),Ang(current_frame)] = Curvature(BW,...
xOrigin,...
yOrigin,...
0,... % plot flag
imgGrayScaled,...
4);
end
TailOri=Curv*Len;
%% STEP 3 : Extract tail bouts
% -------------------------------------------------------------------------
Thresh = 0.05; %0.7
InterMvt = 20;
Durmin = 0; %5*5ms=0.0025s
DurationMvt = 50; % 50*5ms=0.250s
FusionMvt = 20; % 20*5ms=200ms
MinStrength = 0.04; %0.06
PlotFlag = 1;
[A,...
TailMvt,...
IndOnsetMvt,...
IndOffsetMvt,...
TimeCam,...
Tail,...
NumberOfMvt,...
ActivityTail,...
ActivityTailFinal] = ProcessTailMvtSPIM(TailOri,...
timestamps,...
Thresh,...
DurationMvt,...
FusionMvt,...
MinStrength,...
PlotFlag,...
Durmin);
%% STEP 4 : Classify movements
% -------------------------------------------------------------------------
load('LabeledMvt.mat')
kNN = 10;
exp = 1.5;
Membership = nan(5,size(TailMvt,1));
Bias = nan(1,size(TailMvt,1));
Outlier = nan(1,size(TailMvt,1));
for i=1:size(TailMvt,1)
[m,b,o] = ClassifyMvtkNN(TailMvt(i,:),LabeledMvt,kNN,exp);
Membership(:,i) = m;
Bias(i) = b;
Outlier(i) = o;
end
% Find Category of Tail Mvt (1: Scoot, 2: JTurn, 3: Routine Turn, 4: C Bend, 5: Burst):
[~,Cat] = max(Membership);
[NbCat,~] = hist(Cat,[1,2,3,4,5]);
% Display Mvt for each category:
for c=1:5
figure(c);
set(gcf,'WindowStyle','docked');
id=find(Cat==c);
plot(TailMvt(id,:)');
end
%% STEP 5 : Save
%--------------------------------------------------------------------------
clear Mvt
Mvt.timestamps = timestamps;
Mvt.Tail = Tail;
Mvt.IndOnset = IndOnsetMvt;
Mvt.IndOffset = IndOffsetMvt;
Mvt.TailMvt = TailMvt;
Mvt.Cat = Cat;
Mvt.Membership = Membership;
Mvt.Bias = Bias;
Mvt.Outlier = Outlier;
[ofile,opath] = uiputfile('.mat','Save results as');
outfile = fullfile(opath,ofile);
save(outfile,'Mvt')