This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathplot_sequence_preview.m
149 lines (108 loc) · 4.46 KB
/
plot_sequence_preview.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
function file = plot_sequence_preview(sequence, trajectories, file, varargin)
% plot_sequence_preview Generates a sequence preview image
%
% Generates a preview animation for the sequence as an animated GIF.
%
% Input:
% - sequence (structure): A valid sequence structure.
% - trajectories (cell): An array of additional trajectories to draw.
% - file (string): Path to the destination file.
% - varargin[Samples] (integer): Number of preview images.
% - varargin[Scale] (double): Scaling factor.
% - varargin[Width] (double): Reference width.
% - varargin[Height] (double): Reference height.
% - varargin[GroundtruthColor] (vector): Color for groundtruth region.
% - varargin[TrajectoryColor] (cell): Color for groundtruth region.
% - varargin[Palette] (matrix): A color palette matrix for GIF images.
% - varargin[Delay] (integer): Delay between frames for GIF animation.
% - varargin[Loops] (integer): Number of loops in case for GIF animation.
% - varargin[Static] (integer): Also export a static version of the first
% frame.
%
% Output:
% - file (string): Path to the destination file.
%
loops = inf;
delay = 0;
static = false;
samples = 12;
palette = 256;
scale = 1;
groundtruth_color = [0, 1, 0];
trajectories_colors = repmat([1, 0, 0], length(trajectories), 1);
for i = 1:2:length(varargin)
switch lower(varargin{i})
case 'samples'
samples = varargin{i+1};
case 'scale'
scale = varargin{i+1};
case 'width'
scale = varargin{i+1} / sequence.width;
case 'height'
scale = varargin{i+1} / sequence.height;
case 'groundtruthcolor'
groundtruth_color = varargin{i+1};
case 'trajectorycolor'
trajectories_colors = varargin{i+1};
case 'palette'
palette = max(2, min(256, varargin{i+1}));
case 'delay'
delay = max(0, min(655, varargin{i+1}));
case 'loops'
loops = max(0, varargin{i+1});
case 'static'
static = max(0, varargin{i+1});
otherwise
error(['Unknown switch ', varargin{i},'!']) ;
end
end
if size(trajectories_colors, 1) < length(trajectories)
trajectories_colors = repmat(trajectories_colors(1, :), length(trajectories), 1);
end;
indices = round(linspace(1, sequence.length, samples));
animation = zeros(ceil(sequence.height * scale), ceil(sequence.width * scale), numel(indices), 3);
for i = 1:length(indices)
image = double(imread(sequence_get_image(sequence, indices(i)))) / 255;
if size(image, 3) == 1
image = repmat(image, 1, 1, 3);
end
image = imresize(image, scale);
image_red = image(:, :, 1);
image_green = image(:, :, 2);
image_blue = image(:, :, 3);
for t = 1:length(trajectories)
region = trajectories{t}(indices(i), :);
if any(isnan(region))
continue;
end;
region = region_convert(region, 'polygon') .* scale;
rasterized = edge(poly2mask(region(1:2:end), region(2:2:end), size(image, 1), size(image, 2)), 'canny');
image_red(rasterized) = trajectories_colors(t, 1);
image_green(rasterized) = trajectories_colors(t, 2);
image_blue(rasterized) = trajectories_colors(t, 3);
end;
if ~isempty(groundtruth_color)
region = sequence_get_region(sequence, indices(i));
region = region_convert(region, 'polygon') .* scale;
rasterized = edge(poly2mask(region(1:2:end), region(2:2:end), size(image, 1), size(image, 2)), 'canny');
image_red(rasterized) = groundtruth_color(1);
image_green(rasterized) = groundtruth_color(2);
image_blue(rasterized) = groundtruth_color(3);
end;
animation(:, :, i, :) = cat(3, image_red, image_green, image_blue);
end
strip = reshape(animation, size(animation, 1), size(animation, 2) * size(animation, 3), 3);
% Obtain a palette over entire sequence strip, not just first
% image.
[strip, map] = rgb2ind(strip, palette, 'nodither');
animation = reshape(strip, size(animation, 1), size(animation, 2), 1, size(animation, 3));
[directory, name, ext] = fileparts(file);
if static
file = fullfile(directory, [name, '_animated.gif']);
imwrite(uint8(animation), map, file, 'DelayTime', delay, 'LoopCount', loops);
file_static = fullfile(directory, [name, '_static.gif']);
imwrite(uint8(animation(:, :, 1)), map, file_static);
else
file = fullfile(directory, [name, '.gif']);
imwrite(uint8(animation), map, file, 'DelayTime', delay, 'LoopCount', loops);
end