-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinitializeCLWorkspace.m
executable file
·191 lines (161 loc) · 6.25 KB
/
initializeCLWorkspace.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
function initializeCLWorkspace(dataFolder)
% This script is used for initializing parameters and data for centerline
% detection. This script can be run cell by cell, with a few or the cells
% running for or parfor loops over every frame of the video, and some cells
% require user inpurts to initialize centerlines or crop out image regions.
% Prior to running this script, you may run wormCL_tip_clicker.m for a GUI
% that allows the user to click centerline tips.
%% Select datafolder for analysis
if nargin==0
mostRecent=getappdata(0,'mostRecent');
dataFolder=uipickfiles('FilterSpec',mostRecent,...
'Prompt', 'Select the BrainScanner Folder or the LowMag Folder');
dataFolder=dataFolder{1};
end
setappdata(0,'mostRecent',fileparts(dataFolder));
display(['Data Folder: ', dataFolder]);
aviFiles=dir([dataFolder filesep '*.avi']);
aviFiles={aviFiles.name}';
HUDFiles=aviFiles(cellfun(@(x) ~isempty(strfind(x,'HUDS')),aviFiles));
oldFlag=length(HUDFiles);
if ~oldFlag
%% get lowmag folder
if strfind(dataFolder,'LowMag')
low_mag_folder=dataFolder;
else
low_mag_folder=dir([dataFolder filesep 'LowMag*']);
if isempty(low_mag_folder)
error(...
'LowMag folder is missing! ensure the Low mag folder is in the BrainScanner Folder')
end
low_mag_folder=[dataFolder filesep low_mag_folder(1).name];
end
%setup paths to movies
fluormovie=[low_mag_folder filesep 'cam0.avi'];
behaviormovie=[low_mag_folder filesep 'cam1.avi'];
%get movie length
bf2fluor_lookup=[];
else
low_mag_folder=dataFolder;
aviFiles=aviFiles(cellfun(@(x) isempty(strfind(x,'HUDS')),aviFiles));
aviFluorIdx=cellfun(@(x) ~isempty(strfind(x,'fluor')),aviFiles);
behaviormovie=[dataFolder filesep aviFiles{~aviFluorIdx}];
fluormovie=[dataFolder filesep aviFiles{aviFluorIdx}];
%% set up timing alignments and lookups
%get timing sync for old data movies, folders were hard saved at
%1200x600
[bfAll,fluorAll,~]=tripleFlashAlign(dataFolder);
fluorIdxList=1:length(fluorAll.frameTime);
bf2fluor_lookup=interp1(fluorAll.frameTime,fluorIdxList,bfAll.frameTime,'linear');
end
%% set up low magvideos, we've changed the way we save data, the older version
%initialize video objects
behavior_vidobj = VideoReader(behaviormovie);
fluor_vidobj= VideoReader(fluormovie);
%get move length
nframes=round(behavior_vidobj.Duration*behavior_vidobj.FrameRate);
nframes_fluor=round(fluor_vidobj.Duration*fluor_vidobj.FrameRate);
bf_imsize=[behavior_vidobj.Height,behavior_vidobj.Width];
if ~oldFlag && nframes~=nframes_fluor
error('Videos should be of the same length, check if one of the videos is corrupt')
end
%% initialize centerline points
%cut up the entire video into nCells chunks and then initialize nCells+1
%centerlines for fitting each chunk forwards and backwards.
nCells=16;
nSteps=ceil(nframes/nCells); %number of frames in each chunk (except last)
bfCell_i=cell(nCells,1);
clStartI=cell(nCells+1,1);
display(['Click the points of the centerline starting at the head. When '...
'you are done, double click the last point.']);
%
sample_images=zeros(bf_imsize(1),bf_imsize(1),nCells+1);
for ichunk=1:nCells+1
%get bounds for each chunk
lowframe=min((ichunk-1)*nSteps+1,nframes);
hiframe=min(nSteps*ichunk,nframes);
%read the lower frame
BFFrameRaw = double(read(behavior_vidobj,lowframe));
BFFrameRaw=BFFrameRaw(:,:,1); %for old videos images come out as RGB, but we only need one
%select centerline points
display(['Select Points for frame ' num2str(ichunk) ' of ' num2str(nCells+1) ', showing frame ' num2str(lowframe)]);
imagesc(BFFrameRaw);
[xpts,ypts]=getpts();
clStartI{ichunk}=[xpts,ypts];
pause(.3)
if ichunk<=nCells
bfCell_i{ichunk}=lowframe:hiframe;
end
sample_images(:,:,ichunk)=BFFrameRaw;
end
close all
mean_sample=sum(sample_images,3);
%% preview centerlines
close all
button = questdlg('Would you like to preview the centerlines?');
if strcmp(button,'Yes')
for i=1:nCells+1
lowframe=min((i-1)*nSteps+1,nframes);
BFFrameRaw = double(read(behavior_vidobj,lowframe));
% BFFrame(BFFrame<0)=0;
imagesc(BFFrameRaw)
hold on
plot(clStartI{i}(:,1),clStartI{i}(:,2),'r');
pause(.3)
hold off
end
end
%% find which pixels of the subset vary the most, excluding ones that may
% bubble related
button = questdlg('Is there an obvious bubble in the video?');
bubble_mask=false(bf_imsize);
while strcmp(button,'Yes')
display('Crop out where the bubble explores')
imagesc(mean_sample.*~bubble_mask)
bubble_mask=and(bubble_mask,roipoly());
button = questdlg('Is there another bubble?');
end
%% Remove worm for background calculation if it doesnt move much
button = questdlg('Is the worm moving??');
if strcmp(button,'No')
display('Crop out the worm!')
imagesc(mean_sample)
worm_mask=roipoly();
else
worm_mask=false(bf_imsize);
end
%% calculate background for fluor
progressbar(0);
fluor_stack=0;
skip=max(500,round(nframes_fluor/1000)); %don't need to do every frame, only do 1 every skip.
counter=0;
for itime=1:skip:nframes_fluor;
progressbar(itime/nframes_fluor);
fluor_frame = read(fluor_vidobj,itime);
fluor_stack=fluor_stack+double(fluor_frame(:,:,1));
counter=counter+1;
end
progressbar(1);
fluor_stack_proj=(fluor_stack/counter);
%% pick a region around the head to cut out.
%cut out the head and reinperpolate in to get a background without the
%brain
display('Select an ROI around the bright brain region')
imagesc(fluor_stack_proj);
hold on
fluor_mask=roipoly();
hold off
%% compile masks
masks.fluor_mask=fluor_mask;
masks.worm_mask=worm_mask;
masks.bubble_mask=bubble_mask;
%% save all outputs
display('Done! if you need tips, run wormCL_tip_clicker, otherwise, move the file:')
display([low_mag_folder filesep 'CLworkspace'])
display('into the same folder in /tigress/LEIFER/PanNeuronal')
save([low_mag_folder filesep 'CLworkspace'],...
'clStartI',...
'masks',...
'nCells',...
'fluormovie',...
'behaviormovie');