-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBranchAndMincut.cpp
323 lines (247 loc) · 6.9 KB
/
BranchAndMincut.cpp
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
/*
This software contains the C++ implementation of the "branch-and-mincut" framework for image segmentation
with various high-level priors as described in the paper:
V. Lempitsky, A. Blake, C. Rother. Image Segmentation by Branch-and-Mincut.
In proceedings of European Conference on Computer Vision (ECCV), October 2008.
The software contains the core algorithm and an example of its application (globally-optimal
segmentations under Chan-Vese functional).
Implemented by Victor Lempitsky, 2008
*/
#include "BranchAndMincut.h"
#include <stdio.h>
#include <time.h>
#include <float.h>
//using stl for the queue in the min
#include <queue>
#include <deque>
#include <vector>
#include <functional>
//////////////////////////////////
int imWidth = 0;
int imHeight = 0;
static int *bestSegm;
static Branch *bestBranch = NULL;
static gtype upperBound; //best leaf energy found so far
//////////////////////////////////////////////
static int statFlowCalls; //counting calls to lower bound/energy evaluations
//////////////////////////////////////////////
struct
{
GraphT *graph;
gtype *bgUnaries;
gtype *fgUnaries;
bool maxflowWasCalled;
void Reset(gtype *pairwise, gtype *commonUnaries)
{
maxflowWasCalled = false;
graph->reset();
graph->add_node(imWidth*imHeight);
int x,y,i;
if(commonUnaries)
for(i = 0; i < imWidth*imHeight; i++)
{
if(commonUnaries[i] > 0)
graph->add_tweights(i, commonUnaries[i], 0);
else
graph->add_tweights(i, 0, -commonUnaries[i]);
}
for(y = 0, i = 0; y < imHeight; y++)
for(x = 0; x < imWidth; x++, i++)
{
if(y && x < imWidth-1) graph->add_edge(i, i-imWidth+1, pairwise[i*4], pairwise[i*4]);
if(x < imWidth-1) graph->add_edge(i, i+1, pairwise[i*4+1], pairwise[i*4+1]);
if(y < imHeight-1 && x < imWidth-1) graph->add_edge(i, i+imWidth+1, pairwise[i*4+2], pairwise[i*4+2]);
if(y < imHeight-1) graph->add_edge(i, i+imWidth, pairwise[i*4+3], pairwise[i*4+3]);
}
memset(fgUnaries, 0, sizeof(gtype)*imWidth*imHeight);
memset(bgUnaries, 0, sizeof(gtype)*imWidth*imHeight);
}
} reusable;
void PrepareGraph(int imwidth, int imheight)
{
imWidth = imwidth;
imHeight = imheight;
reusable.graph = new GraphT(imwidth*imheight, imwidth*imheight*4);
reusable.bgUnaries = new gtype[imwidth*imheight];
reusable.fgUnaries = new gtype[imwidth*imheight];
}
void ReleaseGraph()
{
delete reusable.graph;
delete reusable.bgUnaries;
delete reusable.fgUnaries;
}
//////////////////////////////////////////
//STL stuff
struct BranchWrapper
{
Branch *br;
BranchWrapper(Branch *b): br(b) {}
};
using namespace std;
bool operator<(const BranchWrapper& a, const BranchWrapper& b)
{
return a.br->bound < b.br->bound;
}
bool operator>(const BranchWrapper& a, const BranchWrapper& b)
{
return a.br->bound > b.br->bound;
}
typedef std::priority_queue<BranchWrapper, vector<BranchWrapper>, greater<vector<BranchWrapper>::value_type>> FRONT_QUEUE;
FRONT_QUEUE frontQueue;
///////////////////////////////////////////////
///////////////////////////////////////////////////////
gtype *currentBgUnaries = NULL;
gtype *currentFgUnaries = NULL;
////////////////////////////////////////////
bool BestFirstSearch();
void DepthFirstSearch(Branch *br);
gtype EvaluateBound(Branch *br);
Branch *BranchAndMincut(int imwidth, int imheight,
Branch *root, int *segmentation,
bool bestFirst, Branch *initialGuess,
gtype *pairwise,
gtype *commonUnaries,
int *nCalls)
{
assert(imwidth == imWidth && imheight == imHeight);
clock_t start = clock();
bestSegm = segmentation;
bestBranch = NULL;
statFlowCalls = 0;
if(currentBgUnaries)
delete currentBgUnaries;
if(currentFgUnaries)
delete currentFgUnaries;
currentBgUnaries = new gtype[imWidth*imHeight];
currentFgUnaries = new gtype[imWidth*imHeight];
reusable.Reset(pairwise, commonUnaries);
upperBound = INFTY;
if(!bestFirst && initialGuess)
upperBound = EvaluateBound(initialGuess);
Branch *root_;
root->Clone(&root_);
EvaluateBound(root_);
if(bestFirst)
{
frontQueue.push(BranchWrapper(root_));
while(BestFirstSearch());
while(!frontQueue.empty())
{
Branch *br = frontQueue.top().br;
delete br;
frontQueue.pop();
}
}
else
DepthFirstSearch(root_);
delete currentBgUnaries;
delete currentFgUnaries;
currentBgUnaries = NULL;
currentFgUnaries = NULL;
bestBranch->bound = upperBound;
if(nCalls)
*nCalls = statFlowCalls;
// printf("Time spent in Branch-And-Mincut is %lf sec\n", double(clock()-start)/CLOCKS_PER_SEC);
return bestBranch;
}
////////////////////////////////////////////
gtype EvaluateBound(Branch *br)
{
statFlowCalls++;
int i, x, y, imsize = imWidth*imHeight;
if(br->SkipEvaluation())
{
br->bound = -INFTY;
return -INFTY;
}
//working with the constant term
gtype boundVal = 0;
gtype constant = br->GetConstant();
gtype flow_limit = upperBound-constant;
if(flow_limit < 0)
{
br->bound = upperBound+EPSILON;
return upperBound+EPSILON;
}
//updating unary terms in the graph
br->GetUnaries(currentBgUnaries, currentFgUnaries);
for(y = 0, i = 0; y < imHeight; y++)
for(x = 0; x < imWidth; x++,i++)
{
gtype unaryUpdateBg = currentBgUnaries[i]-reusable.bgUnaries[i];
gtype unaryUpdateFg = currentFgUnaries[i]-reusable.fgUnaries[i];
reusable.bgUnaries[i] = currentBgUnaries[i];
reusable.fgUnaries[i] = currentFgUnaries[i];
if(unaryUpdateBg || unaryUpdateFg)
{
reusable.graph->add_tweights(i, unaryUpdateFg, unaryUpdateBg);
if(reusable.maxflowWasCalled)
reusable.graph->mark_node(i);
}
}
//evaluating lower bound by pushing flow
boundVal = reusable.graph->maxflow(reusable.maxflowWasCalled, NULL)+constant;
reusable.maxflowWasCalled = true;
br->bound = boundVal;
if(br->IsLeaf() && boundVal < upperBound)
{
//the new candidate for a global minimum
upperBound = boundVal;
if(bestBranch)
delete bestBranch;
br->Clone(&bestBranch);
for(i = 0; i < imsize; i++)
bestSegm[i] = (int)reusable.graph->what_segment(i);
// printf("Bound value = %lf\n", double(boundVal));
}
return boundVal;
}
//////////////////////////////////////////////
bool BestFirstSearch()
{
Branch *br = frontQueue.top().br;
// printf("%d\t%d\n", br->bound, frontQueue.size());
frontQueue.pop();
if(br->IsLeaf())
{
// printf("Minimum found!\n");
return false;
}
Branch *br1, *br2;
br->BranchFurther(&br1, &br2);
delete br;
EvaluateBound(br1);
frontQueue.push(BranchWrapper(br1));
EvaluateBound(br2);
frontQueue.push(BranchWrapper(br2));
return true;
}
void DepthFirstSearch(Branch *br)
{
if(br->IsLeaf())
return;
Branch *br1, *br2;
br->BranchFurther(&br1, &br2);
delete br;
EvaluateBound(br1);
EvaluateBound(br2);
if(br1->bound < br2->bound)
{
if(br1->bound < upperBound)
{
DepthFirstSearch(br1);
if(br2->bound < upperBound)
DepthFirstSearch(br2);
}
}
else
{
if(br2->bound < upperBound)
{
DepthFirstSearch(br2);
if(br1->bound < upperBound)
DepthFirstSearch(br1);
}
}
}