-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemse_MODAL_old.m
More file actions
executable file
·216 lines (173 loc) · 6.8 KB
/
demse_MODAL_old.m
File metadata and controls
executable file
·216 lines (173 loc) · 6.8 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
% DEMSE_MODAL Demonstrate state estimation of 3D drosophila
% kinematic model
%
%
% See also in this script:
% auto_init - initializes the tracker by calculating calibration,
% background, and model shape
%
% gssm_flyOcc - defines the motion and measurement model of the fish
%
%
% srcdkf - performs the estimation
%
% More details are available in each file
% -------------------------------------------------------------------
%
% When 'getIC' is set to 'true', this program will calculate and save a
% structure named 'ManualFit' that contains all the tracking parameters
% associated with a particular video sequence. It saves it in the same
% directory that the image data is located.
%
% When 'getIC' is set to 'false', you have already calculated the initial
% parameters, so the program just loads them from disk.
%=============================================================================================
close all;clear all;clc;
% PAR is a static structure that defines various parameters of the tracking
% sequence
global PAR
% Add the subdirectories that contain important / necessary files
cd '/home/xisco/Code/MATLAB/FlyTracker/'
addpath(pwd);
addpath('mex/');
addpath('core/');
fprintf('\nDEMSE_MODAL : This demonstration performs state estimation\n');
fprintf(' for 3D Drosophila body and wing trajectories\n');
fprintf(' The pixel observations are corrupted by additive\n');
fprintf(' white Gaussian noise.\n\n');
startpath = '/home/xisco/Code/MATLAB/FlyTracker/solutions/';
getImageData = true;
if getImageData
PAR = LoadVideo;
ManualFit.ImageData = PAR;
% Make two directories to save the estimated state and the features into
% if they don't already exist
if exist([PAR.solutionpath ['fly_' PAR.stub]],'dir') ~= 7
mkdir(PAR.solutionpath,['fly_' PAR.stub]);
mkdir(PAR.solutionpath,['Features_' PAR.stub]);
end
save([PAR.solutionpath 'fly_' PAR.stub '/' 'ManualFit_' PAR.stub],'ManualFit');
else
%Load the previously stored data
[FileName,PathName] = uigetfile({'*.mat'},'Select "ManualFit" data file for the video sequence',startpath);
load([PathName FileName]);
% Check that the paths stored in 'ManualFit' match the location that you
% just selected the file from
% If 75% of the paths match , then we'll assume everything's okay.
endd = round(.75*length(ManualFit.ImageData.solutionpath));
if strcmp(PathName(1:endd),ManualFit.ImageData.solutionpath(1:endd)) == 0
%if different, run Loadvideo routine
warning('The directories stored in ManualFit structure do not match its current location.\n You are prompted to relocate the directories',[]);
PAR = LoadVideo;
ManualFit.ImageData = PAR;
save([PAR.solutionpath 'fly_' PAR.stub '/' 'ManualFit_' PAR.stub],'ManualFit');
else
PAR = ManualFit.ImageData;
end
end
%exp95: 440:640
startframe = 430;
endframe = 640;%PAR.numframes;
numframes = 211;
PAR.frames2skip = [];
PAR.framesample = 1;
% - Camera Info
PAR.dt = 1/6000; %Framerate of the camera
PAR.numcam = 3;
PAR.imgres = [512 512];
PAR.CalibFile = 'kine/Calibrations for Kine/cal_for_20031217_done_20040205';
%exp095:
PAR.ICframe = 430;
PAR.FileFromKine = 'KineManualFit_exp095f430';
% Set to true if you need to calculate state of fly at start frame 'PAR.ICframe'.
getIC = 1;
getBodyShape = 1;
%Change this depending on how many flies you are tracking
%More than 1 Fly Is Not Working Yet!!!!!
PAR.flynum = [1 ]; %[1 2];
PAR.numfly = length(PAR.flynum);
PAR.OccludeShape = {[],[],[]};
%spline order
PAR.c = 4;
%This is the number of sample points used within the Fly model.
%It changes how fine the mesh is.
PAR.L1 = 15; %# of steps for body along length
PAR.L2 = 6; %# of steps for head along length
PAR.L3 = 30; %# of steps for wing around the boundary
PAR.T1 = 13; %# of theta steps for head and body
PAR.T2 = 2; %# of steps towards center of wing
%-- These dimensions have to be defined by the user. The
% dimensions are for a single fly.
%The subsample scale of the image is 1/2^(PAR.pwr)
PAR.etamax = 0;
PAR.paramdim = 1;
PAR.statedim = 15*ones(1,PAR.numfly);
PAR.pNoisedim = PAR.statedim;
% This is the number of frames to scan back when performing the pattern
% matching in the motion prediction.
PAR.NumPrevSol = 5;
PAR.streams = 2;
% END ADVANCED PARAMETERS
%=======================================================
% Get Initial Conditions
if getIC == true
%Calculate the initial condition
ManualFit = auto_init(ManualFit,PAR.ICframe,'BG');
ManualFit = auto_init(ManualFit,PAR.ICframe,'IC');
save([PAR.solutionpath 'fly_' PAR.stub '/ManualFit_' PAR.stub],'ManualFit');
else
%Load initial condition
load([PAR.solutionpath 'fly_' PAR.stub '/' 'ManualFit_' PAR.stub]);
end
% Assign model parameters & image BG
PAR.params = ManualFit.params;
PAR.DLT = ManualFit.DLT;
PAR.cam = ManualFit.cam;
% Assign image BG
PAR.BG = ManualFit.IMBG;
% ================================================
% Modify Body Shape
if getBodyShape == 1
ManualFit = auto_init(ManualFit,PAR.ICframe,'autoradius');
save([PAR.solutionpath 'fly_' PAR.stub '/ManualFit_' PAR.stub],'ManualFit');
end
PAR.params = ManualFit.params;
%Reassign values again because they are changed in 'auotradius' mode of
%'auto_init.m'
PAR.statedim = 15*ones(1,PAR.numfly);
PAR.pNoisedim = PAR.statedim;
%--- Initialise GSSM model from external system description script.
model = gssm_flyOcc('init');
% Define start and end frames of calculation
frames = [startframe endframe];
Arg.type = 'state';
Arg.tag = ['State estimation for ' PAR.stub ' data.'];
Arg.model = model;
% Create inference data structure and
InfDS = geninfds(Arg);
% generate process and observation noise sources
[pNoise, oNoise, InfDS] = gensysnoiseds(InfDS, 'srcdkf');
%Initialize occlusion index
InfDS.model.Occ = cell(1,length(PAR.numfly));
%--- initial estimate of state E[X(0)]
p0 = reshape(ManualFit(1).solnQ',[],1);
Xh(:,1) = p0;
% 2*standard deviation in degrees;
angvar = 0.002;
%Variance for the wing joint locations
JointVar = [0.001 0.0004 0.0003];
%Variance for the body linear acceleration
LinVar = [0.183 0.639 1.33];
%Variance for the body angular acceleration
AngVar = [.168 .181 2.20];
% initial state covariance
Px_ = [0.001.*ones(1,3) 0.00001.*ones(1,4) angvar.*ones(1,8)];
%Create a diagonal covariance matrix by replicating Px_ # of fish
%times and then placing it on the diagonal of a matrix.
Px = diag(repmat(Px_,1,PAR.numfly));
%--- Call inference algorithm / estimator
% Square Root Central Difference Kalman Filter
%---------------
InfDS.spkfParams = sqrt(3); % scale factor (CDKF parameter h)
Sx = chol(Px)';
srcdkf_const(Xh(:,1),Sx,pNoise,oNoise,InfDS,frames);