Repository navigation
Expand file tree
/
Copy pathFlowToColor.m
More file actions
205 lines (159 loc) · 5.8 KB
/
Copy pathFlowToColor.m
File metadata and controls
205 lines (159 loc) · 5.8 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
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
198
199
200
201
202
203
204
205
function [img, maxrad] = FlowToColor(flow, maxrad, show_circle)
% flowToColor(flow, maxFlow) flowToColor color codes flow field, normalize
% based on specified value,
%
% flowToColor(flow) flowToColor color codes flow field, normalize
% based on maximum flow present otherwise
% According to the c++ source code of Daniel Scharstein
% Contact: schar@middlebury.edu
% Author: Deqing Sun, Department of Computer Science, Brown University
% Contact: dqsun@cs.brown.edu
% $Date: 2007-10-31 18:33:30 (Wed, 31 Oct 2006) $
% Copyright 2007, Deqing Sun.
%
% All Rights Reserved
%
% Permission to use, copy, modify, and distribute this software and its
% documentation for any purpose other than its incorporation into a
% commercial product is hereby granted without fee, provided that the
% above copyright notice appear in all copies and that both that
% copyright notice and this permission notice appear in supporting
% documentation, and that the name of the author and Brown University not be used in
% advertising or publicity pertaining to distribution of the software
% without specific, written prior permission.
%
% THE AUTHOR AND BROWN UNIVERSITY DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
% INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
% PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR OR BROWN UNIVERSITY BE LIABLE FOR
% ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
UNKNOWN_FLOW_THRESH = 1e9;
if nargin < 3, show_circle = false; end;
[height width nBands] = size(flow);
if (height == 2 && width * nBands == 1) || (width == 2 && height * nBands == 1)
img = computeColor(flow(1) / maxrad, flow(2) / maxrad);
return
elseif nBands ~= 2
error('flowToColor: image must have two bands');
end;
u = flow(:,:,1);
v = flow(:,:,2);
% fix unknown flow
idxUnknown = (abs(u)> UNKNOWN_FLOW_THRESH) | (abs(v)> UNKNOWN_FLOW_THRESH) ;
u(idxUnknown) = 0;
v(idxUnknown) = 0;
if ~exist('maxrad', 'var')
rad = sqrt(u.^2 + v.^2);
maxrad = max(rad(:));
end
u = u / (maxrad + eps);
v = v / (maxrad + eps);
% compute color
img = computeColor(u, v);
% unknown flow
IDX = repmat(idxUnknown, [1 1 3]);
img(IDX) = 0;
if show_circle
r = maxrad;
k = 2 * r + 1;
circle_flow = cat(3, repmat(-r:r, [k,1]), repmat((-r:r)', [1,k]));
circle_flow(sqrt(sum(circle_flow.^2, 3)) > r) = nan;
circle_rgb = FlowToColor(circle_flow, r);
for i = 1:k
for j = 1:k
if ~isnan(circle_flow(i, j, 1))
img(i, j, :) = circle_rgb(i, j, :);
end
end
end
end
end
function img = computeColor(u,v)
% computeColor color codes flow field U, V
% According to the c++ source code of Daniel Scharstein
% Contact: schar@middlebury.edu
% Author: Deqing Sun, Department of Computer Science, Brown University
% Contact: dqsun@cs.brown.edu
% $Date: 2007-10-31 21:20:30 (Wed, 31 Oct 2006) $
% Copyright 2007, Deqing Sun.
%
% All Rights Reserved
%
% Permission to use, copy, modify, and distribute this software and its
% documentation for any purpose other than its incorporation into a
% commercial product is hereby granted without fee, provided that the
% above copyright notice appear in all copies and that both that
% copyright notice and this permission notice appear in supporting
% documentation, and that the name of the author and Brown University not be used in
% advertising or publicity pertaining to distribution of the software
% without specific, written prior permission.
%
% THE AUTHOR AND BROWN UNIVERSITY DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
% INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
% PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR OR BROWN UNIVERSITY BE LIABLE FOR
% ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
nanIdx = isnan(u) | isnan(v);
u(nanIdx) = 0;
v(nanIdx) = 0;
colorwheel = makeColorwheel();
ncols = size(colorwheel, 1);
rad = sqrt(u.^2 + v.^2);
a = atan2(-v, -u)/pi;
fk = (a+1) /2 * (ncols-1) + 1; % -1~1 maped to 1~ncols
k0 = floor(fk); % 1, 2, ..., ncols
k1 = k0+1;
k1(k1==ncols+1) = 1;
f = fk - k0;
for i = 1:size(colorwheel, 2)
tmp = colorwheel(:,i);
col0 = tmp(k0)/255;
col1 = tmp(k1)/255;
col = (1-f).*col0 + f.*col1;
idx = (rad <= 1);
col(idx) = 1 - rad(idx) .* (1 - col(idx)); % increase saturation with radius
col(~idx) = col(~idx) * 0.75; % out of range
img(:,:,i) = uint8(floor(255 * col .* (1 - nanIdx)));
end;
end
function colorwheel = makeColorwheel()
% color encoding scheme
% adapted from the color circle idea described at
% http://members.shaw.ca/quadibloc/other/colint.htm
RY = 15;
YG = 6;
GC = 4;
CB = 11;
BM = 13;
MR = 6;
ncols = RY + YG + GC + CB + BM + MR;
colorwheel = zeros(ncols, 3); % r g b
col = 0;
%RY
colorwheel(1:RY, 1) = 255;
colorwheel(1:RY, 2) = floor(255*(0:RY-1)/RY)';
col = col+RY;
%YG
colorwheel(col+(1:YG), 1) = 255 - floor(255*(0:YG-1)/YG)';
colorwheel(col+(1:YG), 2) = 255;
col = col+YG;
%GC
colorwheel(col+(1:GC), 2) = 255;
colorwheel(col+(1:GC), 3) = floor(255*(0:GC-1)/GC)';
col = col+GC;
%CB
colorwheel(col+(1:CB), 2) = 255 - floor(255*(0:CB-1)/CB)';
colorwheel(col+(1:CB), 3) = 255;
col = col+CB;
%BM
colorwheel(col+(1:BM), 3) = 255;
colorwheel(col+(1:BM), 1) = floor(255*(0:BM-1)/BM)';
col = col+BM;
%MR
colorwheel(col+(1:MR), 3) = 255 - floor(255*(0:MR-1)/MR)';
colorwheel(col+(1:MR), 1) = 255;
end