-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdebugVideo.m
More file actions
60 lines (54 loc) · 1.91 KB
/
debugVideo.m
File metadata and controls
60 lines (54 loc) · 1.91 KB
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
function debugVideo(binaryLabelArray, outputVideoName, framerate)
% This function creates a video from an array of regions created by the
% bwlabel command. The first two dimensions are the image itself, and the
% third is the series of images (if that makes sense).
% generate colors
maximum = max(binaryLabelArray(:));
colorMap = zeros(maximum, 3);
for i = 1:maximum
val = double(i)/double(maximum);
% r channel
colorMap(i,1) = ceil(255*val);
% g channel
if val < 0.5
colorMap(i,2) = ceil(255*val*2);
else
colorMap(i,2) = 255 - ceil(255*(val-0.5)*2);
end
% b channel
colorMap(i,3) = 255 - ceil(255*val);
end
% randomize colormap for more variety
randomIdx = randperm(maximum);
colorMap = colorMap(randomIdx, :);
%% write video to disk
waitDialog = waitbar(0, 'Creating video...');
blSize = size(binaryLabelArray);
movieFrame = zeros(blSize(1), blSize(2), 3, 'uint8');
writer = VideoWriter(outputVideoName);
writer.FrameRate = framerate;
open(writer);
for nofr = 1:blSize(3)
waitbar(nofr/blSize(3), waitDialog, ...
strcat({'Writing frame'},{' '}, num2str(nofr), {' '}, {'of'}, {' '}, num2str(blSize(3))));
for channel = 1:3
movieFrame(:,:,channel) = binaryLabelArray(:,:,nofr);
for color = 1:maximum
% color pixels according to region number
tmp = movieFrame(:,:,channel);
tmp(tmp == color) = colorMap(color,channel);
movieFrame(:,:,channel) = tmp;
end
end
% add text labels
movieProps = regionprops(binaryLabelArray(:,:,nofr),'Centroid');
for labelNumber = 1:((length([movieProps.Centroid]))/2)
if isfinite(movieProps(labelNumber).Centroid)
movieFrame = insertText(movieFrame, movieProps(labelNumber).Centroid, labelNumber, 'BoxColor', [255,255,255]);
end
end
writeVideo(writer, im2frame(movieFrame));
end
close(writer);
close(waitDialog);
return;