-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdispmap_super.m
330 lines (273 loc) · 9.34 KB
/
dispmap_super.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
% Johannes Ulén and Carl Olsson 2013
%
classdef dispmap_super < handle
properties
smoothness_kernel
assignment;
% For TRW-S or Exhaustive binary fusion
maxiter = 1000;
max_relgap = 1e-4;
% Use RD-Improve
improve = false;
end
properties (SetAccess = protected)
sz;
images;
neighborhood;
stored_energy; % Keep track on energy
smooth_weights;
end
methods
%% Constructor
function self = dispmap_super(images,kernel)
self.images = images;
self.sz = size(images{1});
self.sz = self.sz(1:2);
self.smoothness_kernel = kernel;
% Initilization
construct_neighborhood(self);
% Default all smooth weights are 1.
self.smooth_weights = ones(1,numel(self.neighborhood.ind1));
end
%% Set functions
function set.max_relgap(self, max_relgap)
if (max_relgap < 0)
error('Maximum relative gap must be non-negative');
end
self.max_relgap = max_relgap;
end
function set.improve(self, improve)
self.improve = logical(improve);
end
function set.assignment(self, assignment)
self.assignment = assignment;
update_energy(self);
end
function set.smoothness_kernel(self, kernel)
self.smoothness_kernel = kernel;
update_energy(self);
end
function E = energy(self)
E = self.stored_energy;
end
function [e,lb, num_unlabelled] = binary_fusion(self, proposal)
% Perform one binary fusion
% Input check
if (~all(size(proposal) == size(self.assignment)))
error('Binary fusion: Proposals is of wrong size');
end
% Unary cost for all combinations
[E00,E01, E10,E11] = all_pairwise_costs(self, self.assignment, proposal);
% Unary cost for the two choses
U0 = unary_cost(self, self.assignment);
U1 = unary_cost(self, proposal);
connectivity = uint32([self.neighborhood.ind1'; self.neighborhood.ind2']);
% Call Roof duality solver
rd_options.improve = self.improve;
[labelling, e, lb, num_unlabelled] = ...
rd(U0, U1, E00, E01, E10, E11, connectivity, rd_options);
% Set
self.assignment(:,labelling == 1) = proposal(:,labelling == 1);
end
function number_of_iterations = binary_fuse_until_convergence(self, proposal_cell, show_steps)
% Fuse binary proposals until either max iterations is reached or no proposals can improve the result
if nargin < 3
show_steps = false;
end
if ~(isa(proposal_cell,'cell'));
error('Input proposals should be given in cell array.')
end
% Iteration order
number_of_random_ids = self.maxiter*5;
ids = [1:length(proposal_cell)' randi([1 length(proposal_cell)],number_of_random_ids,1)'];
ids([diff(ids) == 0]) = 0; % Removing repating proposal number
ids(ids < 1) = [];
ids(ids > length(proposal_cell)) = [];
propsals_fun = @(n) proposal_cell{ids(n)};
iter = 0;
E = energy(self);
visited_propsals = zeros(numel(proposal_cell),1);
fused_propsals_in_order = {};
for iter = 1:self.maxiter
% If random sets runs out
if iter > number_of_random_ids
ids = [ids ids];
end
iter = iter+1;
% No proposals have fused since last visist
% hence no need to run this again.
if ( visited_propsals(ids(iter)))
continue;
end
P = propsals_fun(iter);
% Fuse
binary_fusion(self, P);
E(end+1) = energy(self);
if (show_steps)
display_current_dispmap(self);
drawnow();
end
% If we tried ALL propsals without improvement we quit
if (iter > 1)
if (E(end-1) ~= E(end))
visited_propsals(:) = 0;
else
visited_propsals(ids(iter)) = 1;
end
else
visited_propsals(ids(1)) = 1;
end
if (all(visited_propsals))
break;
end
end
number_of_iterations = length(E);
end
function [e,lb, iterations] = simultaneous_fusion(self,proposal_cell)
if ~(isa(proposal_cell,'cell'));
error('Input proposals should be given in cell array.')
end
% Keep the pixel choose.
proposal_cell{end+1} = self.assignment;
%Data cost
points = get_points(self);
unary = zeros(length(proposal_cell),size(points,2));
parfor i = 1:length(proposal_cell);
unary(i,:) = unary_cost(self, proposal_cell{i});
end
connectivity = uint32([self.neighborhood.ind1'; self.neighborhood.ind2']);
% Calculate all p's and q's
ind1 = self.neighborhood.ind1;
ind2 = self.neighborhood.ind2;
kernel = int32(self.smoothness_kernel);
points = get_points(self);
q = zeros(length(proposal_cell),length(ind1));
qprim = zeros(length(proposal_cell),length(ind2));
parfor p_id = 1:length(proposal_cell)
q(p_id,:) = disparitymap_from_assignment(self, proposal_cell{p_id}(:,ind2), points(:,ind2));
qprim(p_id,:) = disparitymap_from_assignment(self, proposal_cell{p_id}(:,ind1), points(:,ind2));
end
options_struct.maxiter = self.maxiter;
options_struct.max_relgap = self.max_relgap;
alphas = self.smooth_weights(:);
[L,e,lb, iterations] = trws(kernel, unary, connectivity, q, qprim, alphas, self.tol, options_struct);
%Extract solution
assignments = zeros(size(proposal_cell{1}));
for i = 1:length(proposal_cell)
assignments(:,L == i) = proposal_cell{i}(:,L==i);
end
self.assignment = assignments;
end
function im = current_dispmap(self)
im = reshape(disparitymap_from_assignment(self, self.assignment), [self.sz]);
end
function display_current_dispmap(self)
imagesc(self.current_dispmap());
colormap gray(256);
title(sprintf('Solution energy: %g \n', self.energy()));
axis equal;
end
function display(self)
fprintf('Energy of current solution: %g. \n', self.energy());
fprintf('Image pair size: (%d,%d) \n', self.sz(1), self.sz(2));
fprintf('Settings: \n');
fprintf('Smoothness kernel : %d \n', self.smoothness_kernel)
fprintf('Maximum iterations : %d \n', self.maxiter)
fprintf('Max relative gap : %g \n', self.max_relgap);
fprintf('RD-Improve : %d \n', int32(self.improve));
display_current_dispmap(self);
end
end
methods (Access = protected)
function U = unary_cost(self, assignment)
error('Overload unary_cost \n');
end
function c = pairwise_cost(self, p,q)
switch (self.smoothness_kernel)
case 1
c = self.smooth_weights.* min( abs( (p-q)), self.tol);
case 2
c= self.smooth_weights.* min( ((p-q)).^2, self.tol);
otherwise
error('Unkown kernel type');
end
end
function [E00,E01, E10,E11] = all_pairwise_costs(self, assignment, proposals)
% Smoothcost
points = get_points(self);
ind1 = self.neighborhood.ind1;
ind2 = self.neighborhood.ind2;
q = disparitymap_from_assignment(self, assignment(:,ind2), points(:,ind2));
qprim = disparitymap_from_assignment(self, assignment(:,ind1), points(:,ind2));
E00 = pairwise_cost(self,q,qprim);
if nargout > 1
%new points
new_q = disparitymap_from_assignment(self, proposals(:,ind2), points(:,ind2));
new_qprim = disparitymap_from_assignment(self, proposals(:,ind1), points(:,ind2));
%Smoothness cost p = alpha, q = alpha => V(p,q)
E11 = pairwise_cost(self,new_q, new_qprim);
%Smoothness cost p = alpha (=0), q = beta (=1)
E10 = pairwise_cost(self,q, new_qprim);
%Smoothness cost p = beta (=1), q = alpha (=0)
E01 = pairwise_cost(self,new_q, qprim);
end
end
function update_energy(self)
% Return inf if no assigment is set yet
if (isempty(self.assignment))
self.stored_energy = inf;
else
% Call everytime assigments is update
U = unary_cost(self, self.assignment);
P = all_pairwise_costs(self, self.assignment);
self.stored_energy = sum(U(:)) + sum(P(:));
end
end
function points = get_points(self)
[xx,yy] = meshgrid(1:self.sz(2),1:self.sz(1));
points = [xx(:)'; yy(:)'];
end
function construct_neighborhood(self)
%Generate neighborhood
nodenr = zeros(self.sz(1), self.sz(2) );
nodenr(:) = 1:length(nodenr(:));
%Vertical edges
start = nodenr(1:end-1,:);
finish = nodenr(2:end,:);
self.neighborhood.ind1 = [start(:); finish(:)];
self.neighborhood.ind2 = [finish(:); start(:)];
%Horisotal edges
start = nodenr(:,1:end-1);
finish = nodenr(:,2:end);
self.neighborhood.ind1 = [self.neighborhood.ind1; start(:); finish(:)];
self.neighborhood.ind2 = [self.neighborhood.ind2; finish(:); start(:)];
%remove edgesl from or to background node
keep_edges = (self.neighborhood.ind2 ~= 0) & (self.neighborhood.ind1 ~= 0);
self.neighborhood.ind1 = self.neighborhood.ind1(keep_edges);
self.neighborhood.ind2 = self.neighborhood.ind2(keep_edges);
self.neighborhood.nodenr = nodenr;
end
function set_disparity(self, assignment)
self.assignment(1:2,:) = 0;
self.assignment(3,:) = 1;
self.assignment(4,:) = -assignment(:);
end
function init_assigments(self)
self.assignment = zeros(4, prod(self.sz(1:2)) );
self.assignment(3,:) = 1;
end
% Remark:
% It is possible to make this implementation with assigments(3,i) = 1 \forall i and hence removing it.
% However keeping all four variables for each voxel leads more straightforward code.
% Convert given assigment to a disparty map.
function disps = disparitymap_from_assignment(self, assignment, points)
if nargin < 3
points = self.get_points();
end
if (any(any(assignment(3,:) == 0)))
error('Infinite disparity');
end
disps = -(sum(assignment(1:2,:).*points)+assignment(4,:))./assignment(3,:);
end
end
end