-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_centerlines.m
82 lines (75 loc) · 3.33 KB
/
find_centerlines.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
function success = find_centerlines(folder_name)
% gets the centerlines given tracks and their associated individual worm
% images
addpath(genpath(pwd))
parameters = load_parameters(folder_name); %load experiment parameters
ImageSize = 2*round(parameters.TrackingWormBoxHalfSize * parameters.CameraPixeltommConversion);
if parameters.TrackOnly
success = true;
return
end
relevant_track_fields = {'Eccentricity','Direction','Speed'};
%% Load tracks
Tracks = load_single_folder(folder_name, relevant_track_fields);
if isempty(Tracks)
success = false;
return
end
%% Preallocate memory
track_count = length(Tracks);
Tracks(track_count).Centerlines = [];
Tracks(track_count).UncertainTips = [];
Tracks(track_count).OmegaTurnAnnotation = [];
Tracks(track_count).PossibleHeadSwitch = [];
Tracks(track_count).Length = [];
Tracks(track_count).TotalScore = [];
Tracks(track_count).ImageScore = [];
Tracks(track_count).DisplacementScore = [];
Tracks(track_count).PixelsOutOfBody = [];
Tracks(track_count).PotentialProblems = [];
Tracks(track_count).DilationSize = [];
Tracks(track_count).AspectRatio = [];
Tracks(track_count).MeanAspectRatio = [];
Tracks(track_count).ThinningIteration = [];
Tracks(track_count).MeanAngle = [];
Tracks(track_count).Angles = [];
Tracks(track_count).ProjectedEigenValues = [];
Tracks(track_count).Velocity = [];
Tracks(track_count).PhaseVelocity = [];
% try
% parpool(feature('numcores'))
% catch
% %sometimes matlab attempts to write to the same temp file. wait and
% %restart
% pause(randi(60));
% parpool(feature('numcores'))
% end
%% Extract Centerlines and eigenworms
parfor track_index = 1:track_count
% for track_index = 1:track_count
% for track_index = 10:10
% loop through all the tracks to get centerlines
% track_index
% tic
try
% if anything goes wrong in centerline finding, make a note to
% remove the track
loaded_file = load([folder_name, filesep, 'individual_worm_imgs', filesep, 'worm_', num2str(track_index), '.mat']);
worm_images = loaded_file.worm_images;
Tracks(track_index) = initial_sweep(worm_images, Tracks(track_index), parameters, track_index); %get the centerlines
[angles, Tracks(track_index).MeanAngle] = centerlines_to_angles(Tracks(track_index).Centerlines); %get the angles
Tracks(track_index).Angles = angles - (diag(parameters.MeanAngles)*ones(size(angles))); %mean center
Tracks(track_index).ProjectedEigenValues = parameters.EigenVectors\Tracks(track_index).Angles; %project into PCA space
Tracks(track_index).Velocity = find_velocity(Tracks(track_index).Speed, Tracks(track_index).Direction, Tracks(track_index).Centerlines, ImageSize);
Tracks(track_index).PhaseVelocity = worm_phase_velocity(Tracks(track_index).ProjectedEigenValues, parameters);
catch
% % error in centerline finding. get rid of it in error resolution
Tracks(track_index).PotentialProblems = [];
end
% toc
end
delete(gcp('nocreate'))
%% save the results
savetracks(Tracks, folder_name);
success = true;
end