-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_analysis.m
197 lines (171 loc) · 5.67 KB
/
main_analysis.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
% DICCOR
%
% An analysis routine for DIC analysis of microscopy data that generates a
% displacement field and a corresponding strain map.
%
% Dependencies
% ------------
% All code tested on MATLAB R2013a or greater
% Image Processing Toolbox
%
%
% This code was developed by the Brangwynne Group at Princeton University.
% If modifying or using this code, please cite the following article:
%
% W.Gilpin, S. Uppaluri, C. Brangwynne ?Worms under pressure:
% bulk mechanical properties of C. elegans are independent of the cuticle?
% Biophysical Journal, 2015.
%
% The "disp_on_blocks.m" MATLAB function has an unknown origin. If you are
% aware of the source of this file please let the authors know so that we
% can provide appropriate attribution.
%% calculate displacements in a set of images
close all;
pics=dir('sample_images/*.tif'); % just look in working directory
N = numel(pics);% N=500;
subpix=1; % greater than 0 is best
% blocksize=68/4;
blocksize=17;
%blocksize=23; % has to be odd, ideally grains move less than block radius from frame to frame
stdcut=.01; % b/w 0 and 1, larger is more stringent requirement for coarse structure
im2=im2double(mat2gray(imread(['sample_images/' pics(1).name])));
im2=im2(:,:,1);
imagesize=size(im2);
xsize=floor(imagesize(2)/blocksize);
ysize=floor(imagesize(1)/blocksize);
x=0.5*(blocksize)+blocksize*(0:(xsize));
y=0.5*(blocksize)+blocksize*(0:(ysize));
[x y]=meshgrid(x,y);
%% the slow step where I build displacement array
vel=zeros(N,ysize+1,xsize+1,2);
for ii=1:(N-1)
im1=im2double(mat2gray(imread(['sample_images/' pics(ii).name])));
im2=im2double(mat2gray(imread(['sample_images/' pics(ii+1).name])));
im1=im1(:,:,1);
im2=im2(:,:,1);
[bx,by,corsh]=disp_on_blocks(im1,im2,blocksize,subpix);
stdim1 = blkproc(im1,[blocksize blocksize],'std2(x)');
stdim2 = blkproc(im2,[blocksize blocksize],'std2(x)');
stdgeo=(stdim1.*stdim2).^(0.5);
k=find(stdgeo < stdcut);
bx(k)=0;
by(k)=0;
%test=size(bx);
vel(ii,:,:,1)=bx;
vel(ii,:,:,2)=by;
disp(ii);
end;
%% Check for outliers and use this to guess the upper and lower limits
% of acceptable velocities
alldat=vel(:,:,:,:);
figure(); hist(alldat(:),400)
%% manually remove outliers from vel and subtract mean
% topcut=-.35; %for 10x image
% botcut=-.65;
topcut=1; %60x images
botcut=-1;
blkarea=[60 60];
fun=@(x) nanmean(x(:));
for ii=1:N
for jj=1:2 % do both components
bb=squeeze(vel(ii,:,:,jj));
nzerobb=(bb~=0);
bb(~nzerobb)=NaN;
loc_otls=(bb>topcut)|(bb<botcut);
bb(logical(loc_otls.*nzerobb))=NaN;
meds=nlfilter(bb,blkarea,fun);
bb=bb-meds;
vel(ii,:,:,jj)=bb;
end
end
%% spatially smooth field
sz_smooth = 3;
for ii=1:N
for jj=1:2 % do both components
bb=squeeze(vel(ii,:,:,jj)); %
g_ker = fspecial('gaussian',[sz_smooth sz_smooth],2);
bb=conv2(bb,g_ker,'same');
vel(ii,:,:,jj)=bb;
end
end
%% temporally average and save images of average velocity fields
window=2; % the number of frames to average into velocity field
iter=1; % number of frames to skip when generating image sequence (reduces frame rate)
dosave=1; % toggle to save images
dsamp=0;
close all;
figvec=figure();
figstr=figure();
if dosave
mkdir(['sample_output/vec_field']);
mkdir(['sample_output/strain_map']);
end
for ii=(floor(window/2)+1):iter:(N-floor(window/2))
disp(ii);
swapfig(figvec);
pic=im2double(mat2gray(imread(['sample_images/' pics(ii).name])));
imshow(imresize(pic,1));
hold on;
range=(ii-floor(window/2)):(ii+floor(window/2));
bx=squeeze(nanmedian(vel(range,:,:,1)));
by=squeeze(nanmedian(vel(range,:,:,2)));
% don't display all vectors
if dsamp==1
sf=2;
x2=downsample(downsample(x,sf)',sf);
y2=downsample(downsample(y,sf)',sf);
bx=downsample(downsample(bx,sf)',sf);
by=downsample(downsample(by,sf)',sf);
else
x2=x;
y2=y;
end
%bx(end-1:end,:)=NaN; % adjust for each image
%by(end-1:end,:)=NaN; % adjust for each image
quiver(x2,y2,bx,by,3,'LineWidth',3,'AutoScale','Off')
axis off
if dosave
export_fig(fullfile('sample_output/vec_field',['overlay' num2str(ii)]),gcf)
end
% find the Cauchy strain %div = divergence(x,y,bx,by);
e_xx=diff(bx,1,1); e_xx(end+1,:)=0;
e_xy=diff(bx,1,2); e_xy(:,end+1)=0;
e_yx=diff(by,1,1); e_yx(end+1,:)=0;
e_yy=diff(by,1,2); e_yy(:,end+1)=0;
e_xy=.5*(e_xy+e_yx);
e_yx=e_xy;
ee_size=size(e_xx);
allc=cat(2,e_xx(:), e_xy(:),e_yx(:), e_yy(:));
div=zeros(length(allc),1);
for jj=1:length(allc)
eig1=.5*((allc(jj,1) + allc(jj,4))-sqrt((allc(jj,1)-allc(jj,4))^2+4*allc(jj,2)*allc(jj,3)));
eig2=.5*((allc(jj,1) + allc(jj,4))+sqrt((allc(jj,1)-allc(jj,4))^2+4*allc(jj,2)*allc(jj,3)));
div(jj)=max(abs([eig1 eig2]));
if isnan(eig1)|isnan(eig2)
div(jj)=NaN;
end
end
div=reshape(div,ee_size(1),ee_size(2));
div=real(div); % remove local rotation
hold off;
swapfig(figstr);
where_nan=isnan(div); % set so that nans are middle of map
div(where_nan)=0;
%div(:,end-1:end)=0; % adjust for each image
%div(end-6:end,:)=0; % adjust for each image
imagesc(div)
colormap(parula(1024))
if ii==(floor(window/2)+1)
ord_div=sort(div(:));
bound=ord_div(end-10); % change this range to reduce saturation
end
caxis([-bound bound])
axis off
pbaspect([xsize ysize 1]);
if dosave
export_fig(fullfile('sample_output/strain_map',['map' num2str(ii)]),gcf)
end
if ~dosave
pause(.4)
end
end