-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeepPIV_3DR_UnwarpAuto.m
163 lines (153 loc) · 5.49 KB
/
DeepPIV_3DR_UnwarpAuto.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
clc; clear; close all; warning('off');tic
dataset='150811_DeepPIV_BathoStyg_3DR_med';
display(dataset);
val=1;
vel=10; %mm/s %UNKNOWN
percent=0.05; %percent allowable change in imwarp transform
inc=1;
%retrieving data set-specific parameters
[dir,start,finish,fps,fstop,shutter,calib,red,aspectratio,contrast]=videoinfo(dataset,vel);
dir=dir(13:end);
viddir=[dir,'clips/'];
indir=[dir,'input/'];
outdir=[dir,'output/'];
nFrames=finish-start;
%% extract body features manually
display('Loading images for smoothing.')
% im_file=[indir,dataset,'_IMAGEfilter.mat'];
% mask_file=[indir,dataset,'_MASKfilter.mat'];
% outdata=[outdir,dataset,'_',num2str(val),'val_hiso.mat'];
% outstl=[outdir,dataset,'_',num2str(val),'val.stl'];
% outfile=[outdir,dataset,'_',num2str(val),'val.mp4'];
im_file=[indir,dataset,'_IMAGEbodyCorr.mat'];
% mask_file=[indir,dataset,'_MASKbodyCorrected.mat'];
im_outfile=[indir,dataset,'_IMAGEbodyCorrsm.mat'];
outdata=[outdir,dataset,'_',num2str(val),'val_bodyhiso.mat'];
outstl=[outdir,dataset,'_',num2str(val),'valbody.stl'];
outfile=[outdir,dataset,'_',num2str(val),'valbody.mp4'];
% im_file=[indir,dataset,'_IMAGEhouseCorr.mat'];
% im_outfile=[indir,dataset,'_IMAGEhouseCorrsm.mat'];
% % mask_file=[indir,dataset,'_MASKhouseCorrected.mat'];
% outdata=[outdir,dataset,'_',num2str(val),'val_househiso.mat'];
% outstl=[outdir,dataset,'_',num2str(val),'valhouse.stl'];
% outfile=[outdir,dataset,'_',num2str(val),'valhouse.mp4'];
if exist(im_file,'file')==2
display(' Loading IMAGE stacks...');
load(im_file);
else
display(' Cropped image stack does not exist. Run DeepPIV_3DR_Batch.m.')
break
end
%%%%%%%%
IMAGEfilter=IMAGEbody;
% IMAGEfilter=IMAGEhouse;
IMAGEunwarp=IMAGEfilter;%%COMMENT OUT WHEN USING UNWARP
%%%%%%%%
% %% unwarping all images in stack
% display('Unwarping all images in stack')
% im_file=[indir,dataset,'_IMAGEunwarp.mat'];
% data_file=[indir,dataset,'_transform.mat'];
% if exist(im_file,'file')==2
% load(im_file);
% elseif exist(data_file,'file')==2 && exist(im_file,'file')==0
% load(data_file);
% IMAGEunwarp=IMAGEfilter;
% for i=1:1:size(IMAGEfilter,3)-1
% tform=transform(:,:,i);
% im=IMAGEfilter(:,:,i+1);%makethemstoptalkingplease
% newim = imwarp(im,tform,'OutputView',imref2d(size(im)));
% IMAGEunwarp(:,:,i+1)=newim;
% end
% save(im_file,'IMAGEunwarp','-v7.3');
% else
% [optimizer, metric] = imregconfig('monomodal');
% im1=IMAGEfilter(:,:,1);%makethemstoptalkingplease
% im2=IMAGEfilter(:,:,2);
% tform=imregtform(im2,im1,'rigid',optimizer,metric);
% IMAGEunwarp=IMAGEfilter;
% counter=0;
% for i=1:1:size(IMAGEfilter,3)-1
% display([num2str((size(IMAGEfilter,3)-i)/size(IMAGEfilter,3)*100),'% complete'])
% if max(max(IMAGEfilter(:,:,i)))>0
% counter=counter+1;
% im2=IMAGEfilter(:,:,i+1);
% tform=imregtform(im2,im1,'rigid',optimizer,metric);
% rot=asin(tform.T(1,1));xdisp=tform.T(3,1);ydisp=tform.T(3,2);
% if counter>1
% rot2=asin(tform2.T(1,1));xdisp2=tform2.T(3,1);ydisp2=tform2.T(3,2);
% if abs(rot-rot2)/abs(rot2)>percent || abs(xdisp-xdisp2)/max(abs([xdisp,xdisp2]))>percent || abs(ydisp-ydisp2)/max(abs([ydisp,ydisp2]))>percent
% tform=tform2;
% end
% end
% newim2 = imwarp(im2,tform,'OutputView',imref2d(size(im1)));
% else
% newim2 = im1.*0;
% end
% IMAGEunwarp(:,:,i+1)=newim2;
% im1=newim2;
% transform(:,:,i)=tform;
% tform2=tform;
% % counter=0;
% end
% end
% clear im1 im2 newim2 tform tform2 optimizer metric im_filenew IMAGEfilter
%
% for i=1:1:size(IMAGEunwarp,3)
% imshow(IMAGEunwarp(:,:,i))
% pause(0.1)
% end
%
% n=input('Are you satisfied with the unwarped image stack? Yes [enter]; No [0] ');
% if n==0
% break
% else
% save(im_file,'IMAGEunwarp','-v7.3');
% save(data_file,'transform');
% end
% Smoothing adjacent images in stack
display('Smoothing images in body image stack')
% if exist(im_outfile,'file')==2
% display(' Loading post-processed image stack...');
% load(im_outfile);
% else
IMAGEsm=smooth3(IMAGEunwarp); %smooth images
save(im_outfile,'IMAGEsm','-v7.3'); %save post-processing parameters
% end
close all; clear IMAGEunwarp
%% plotting 3D reconstruction
display('Plotting 3D reconstruction');
h=figure(3);
set(h,'Position',[100,100,1000,1000]);
if exist(outdata,'file')==2
display([' Loading isosurface data with val = ',num2str(val),'...']);
load(outdata);
else
display([' Calculating isosurface data with val = ',num2str(val),'...']);
[X,Y,Z]=meshgrid(1:size(IMAGEsm,2),1:size(IMAGEsm,1),1:size(IMAGEsm,3));
newaspect=aspectratio/aspectratio(1,1);
xx=single(X)/newaspect(1);yy=single(Y)/newaspect(2);zz=single(Z)/newaspect(3);
clear X Y Z
fv=isosurface(xx,yy,zz,IMAGEsm,val,'verbose');
clear xx yy zz
display([' Saving isosurface data with val = ',num2str(val),'...']);
stlwrite(outstl,fv);
hiso=patch(fv,'FaceColor',[0.5,0.5,0.65],'EdgeColor','none');
save(outdata,'hiso');
end
display([' Plotting isosurface data with val = ',num2str(val),'...']);
isonormals(IMAGEsm,hiso)
alpha(0.2) %sets level of transparency
view(3)
camlight
lighting gouraud
grid on
daspect([1 1 1]);
% axis equal
%%
% display([' Saving 3DR movie with val = ',num2str(val)],'...');
% if exist(outfile,'file')==2
% return
% else
% makevideo(outfile);
% end
toc