-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizeTriangulation.m
196 lines (150 loc) · 6.04 KB
/
visualizeTriangulation.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
function visualizeTriangulation(expInfo,genotype_code,raw_images,omma_triangles,...
save_individual_images,save_movies)
%--------------------------------------------------------------------------
% make directory
%--------------------------------------------------------------------------
close all
% make folder
base_path = expInfo.filepath_output;
masterpath_out = strcat(base_path,'/latticeTriangulation/');
[ status, msg ] = mkdir(masterpath_out);
if status == 0
msg
end
%--------------------------------------------------------------------------
% Set up the movie structure.
%--------------------------------------------------------------------------
% Preallocate movie, which will be an array of structures.
% First get a cell array with all the frames.
numberOfFrames = size(raw_images,4);
allTheFrames = cell(numberOfFrames,1);
vidHeight = size(raw_images,1);
vidWidth = size(raw_images,2);
allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
% Next get a cell array with all the colormaps.
allTheColorMaps = cell(numberOfFrames,1);
allTheColorMaps(:) = {zeros(256, 3)};
% Now combine these to make the array of structures.
myMovie = struct('cdata', allTheFrames, 'colormap', allTheColorMaps);
% Need to change from the default renderer to zbuffer to get it to work right.
% openGL doesn't work and Painters is way too slow.
set(gcf, 'renderer', 'zbuffer');
%--------------------------------------------------------------------------
% loop through number of images and record movie
%--------------------------------------------------------------------------
disp('\n')
disp("Rendering frame: ")
for t = 1:numberOfFrames
%----------------
% display counter
%----------------
if t > 1
for q=0:log10(t-1)
fprintf('\b'); % delete previous counter display
end
fprintf('%d',t)
end
%-----------------------------------------
% visualize triangles on top of raw images
%-----------------------------------------
imshow(raw_images(:,:,:,t))
hold on
triplot(omma_triangles{t},'LineWidth',2,'Color','cyan')
hold off
thisFrame = getframe(gca);
tempFrame = uint8(zeros(vidHeight,vidWidth,3));
tempFrame(:,:,1) = imresize(thisFrame.cdata(:,:,1),[vidHeight,vidWidth]);
tempFrame(:,:,2) = imresize(thisFrame.cdata(:,:,2),[vidHeight,vidWidth]);
tempFrame(:,:,3) = imresize(thisFrame.cdata(:,:,3),[vidHeight,vidWidth]);
thisFrame.cdata = tempFrame;
myMovie(t) = thisFrame;
end
close all
%--------------------------------------------------------------------------
% save individual frames to file
%--------------------------------------------------------------------------
if save_individual_images
disp('\n')
disp("Saving frame: ")
% make folder
filepath_out = strcat(masterpath_out,'/individualFrames/');
[ status, msg ] = mkdir(filepath_out);
if status == 0
msg
end
% loop through and save each frame
for j = 1:numberOfFrames
%-------------------------------
% assemble frame w/ text overlay
%-------------------------------
% assemble side-by-side image of raw + raw w/ ilastik probabilities
new_frame = myMovie(j).cdata;
% write genotype ontop of image
position = [15 15];
new_frame = insertText(new_frame,position,char(expInfo.genotypes_full{j}),'FontSize',30,...
'BoxColor','white','TextColor','black');
%-------------
% save to file
%-------------
file_name = strcat(expInfo.filenames(j)," ",'latticeTriangulation.jpeg');
imwrite(new_frame,fullfile(filepath_out,file_name));
%----------------------------------
% display counter in command window
%----------------------------------
if j > 1
for q=0:log10(j-1)
fprintf('\b'); % delete previous counter display
end
fprintf('%d',j)
end
end
end
%--------------------------------------------------------------------------
% save movies for individual genotypes
%--------------------------------------------------------------------------
% loop through list of genotypes to save
if save_movies
for m = 1:length(genotype_code)
if m == 1
disp('\n')
end
disp(strcat("Saving movie for ", genotype_code(m), " genotype"))
%-------------
% render movie
%-------------
% pull out indices matching current target genotype
ind = find(expInfo.genotypes_code == genotype_code(m));
% make movie of this individual genotype
temp_movie = uint8(zeros(vidHeight,vidWidth,3,length(ind)));
% loop through indices that contain target genotype
for z = 1:length(ind)
j = ind(z);
%-------------------------------
% assemble frame w/ text overlay
%-------------------------------
% assemble side-by-side image of raw + raw w/ ilastik probabilities
new_frame = myMovie(j).cdata;
% write genotype ontop of image
position = [15 15];
new_frame = insertText(new_frame,position,char(expInfo.genotypes_full{j}),'FontSize',30,...
'BoxColor','white','TextColor','black');
%--------------------
% store current frame
%--------------------
temp_movie(:,:,:,z) = new_frame;
end
%-----------
% save movie
%-----------
% use genotype for movie name
baseFileName = genotype_code(m);
% assemble full file name
fullFileName = fullfile(masterpath_out, baseFileName);
% record video
writerObj = VideoWriter(fullFileName,'MPEG-4');
writerObj.FrameRate = 1;
open(writerObj);
writeVideo(writerObj,temp_movie)
close(writerObj);
end
end