-
Notifications
You must be signed in to change notification settings - Fork 15
/
GraphCutMex.cpp
444 lines (405 loc) · 16.8 KB
/
GraphCutMex.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "mex.h"
#include "GCoptimization.h"
#include "GraphCut.h"
#include <stdlib.h>
/* Declarations */
void SetLabels(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
void GetLabels(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
void ABswaps(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
void Expand(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
void Energy(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
void Truncate(GCoptimization *MyGraph, int nout, mxArray *pout[], int nin, const mxArray *pin[]);
GCoptimization* GetGCHandle(const mxArray *x); /* extract ahndle from mxArry */
/*
* Matlab wrapper for Weksler graph cut implementation
*
* GCoptimization cde by Olga Veksler.
* Wrapper code by Shai Bagon.
*
*
* Performing Graph Cut operations on a 2D grid.
*
* Usage:
* [gch ...] = GraphCutMex(gch, mode ...);
*
* Notes:
* 1. Data types are crucial!
* 2. The embedded implementation treat matrices as a row stack, while
* matlab treats them as column stack; thus there is a need to
* transpose the label matrices and the indices passed to expand
* algorithm.
*
* Inputs:
* - gch: a valid Graph Cut handle (use GraphCutConstr to create a handle).
* - mode: a char specifying mode of operation. See details below.
*
* Output:
* - gch: A handle to the constructed graph. Handle this handle with care
* and don't forget to close it in the end!
*
* Possible modes:
*
* - 's': Set labels
* [gch] = GraphCutMex(gch, 's', labels)
*
* Inputs:
* - labels: a width*height array of type int32, containing a
* label per pixel. note that labels values must be is range
* [0..num_labels-1].
*
* - 'g': Get current labeling
* [gch labels] = GraphCutMex(gch, 'g')
*
* Outputs:
* - labels: a width*height array of type int32, containing a
* label per pixel. note that labels values must be is range
* [0..num_labels-1].
*
* - 'n': Get current values of energy terms
* [gch se de] = GraphCutMex(gch, 'n')
*
* Outputs:
* - se: Smoothness energy term.
* - de: Data energy term.
*
* - 'e': Perform labels expansion
* [gch labels] = GraphCutMex(gch, 'e')
* [gch labels] = GraphCutMex(gch, 'e', iter)
* [gch labels] = GraphCutMex(gch, 'e', [], label)
* [gch labels] = GraphCutMex(gch, 'e', [], label, indices)
*
* When no inputs are provided, GraphCut performs expansion steps
* until it converges.
*
* Inputs:
* - iter: a double scalar, the maximum number of expand
* iterations to perform.
* - label: int32 scalar denoting the label for which to perfom
* expand step.
* - indices: int32 array of linear indices of pixels for which
* expand step is computed. indices _MUST_ be zero offset and not
* one offset like matlab usuall indexing system, i.e., to include
* the first pixel in the expand indices array must contain 0 and
* NOT 1!
*
* Outputs:
* - labels: a width*height array of type int32, containing a
* label per pixel. note that labels values must be is range
* [0..num_labels-1].
*
* - 'a': Perform alpha - beta swappings
* [gch labels] = GraphCutMex(gch, 'a')
* [gch labels] = GraphCutMex(gch, 'a', iter)
* [gch labels] = GraphCutMex(gch, 'a', label1, label2)
*
* When no inputs are provided, GraphCut performs alpha - beta swaps steps
* until it converges.
*
* Inputs:
* - iter: a double scalar, the maximum number of swap
* iterations to perform.
* - label1, label2: int32 scalars denoting two labels for swap
* step.
*
* Outputs:
* - labels: a width*height array of type int32, containing a
* label per pixel. note that labels values must be is range
* [0..num_labels-1].
*
* - 'c': Close the graph and release allocated resources.
* [gch] = GraphCutMex(gch,'c');
*
*
* See Also:
* GraphCutConstr
*
* This wrapper for Matlab was written by Shai Bagon ([email protected]).
* Department of Computer Science and Applied Mathmatics
* Wiezmann Institute of Science
* http://www.wisdom.weizmann.ac.il/
*
* The core cpp application was written by Veksler Olga:
*
* [1] Efficient Approximate Energy Minimization via Graph Cuts
* Yuri Boykov, Olga Veksler, Ramin Zabih,
* IEEE transactions on PAMI, vol. 20, no. 12, p. 1222-1239, November 2001.
*
* [2] What Energy Functions can be Minimized via Graph Cuts?
* Vladimir Kolmogorov and Ramin Zabih.
* To appear in IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI).
* Earlier version appeared in European Conference on Computer Vision (ECCV), May 2002.
*
* [3] An Experimental Comparison of Min-Cut/Max-Flow Algorithms
* for Energy Minimization in Vision.
* Yuri Boykov and Vladimir Kolmogorov.
* In IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI),
* September 2004
*
* [4] Matlab Wrapper for Graph Cut.
* Shai Bagon.
* in https://github.com/shaibagon/GCMex, December 2006.
*
* This software can be used only for research purposes, you should cite
* the aforementioned papers in any resulting publication.
* If you wish to use this software (or the algorithms described in the aforementioned paper)
* for commercial purposes, you should be aware that there is a US patent:
*
* R. Zabih, Y. Boykov, O. Veksler,
* "System and method for fast approximate energy minimization via graph cuts",
* United Stated Patent 6,744,923, June 1, 2004
*
*
* The Software is provided "as is", without warranty of any kind.
*
*
*/
void mexFunction(
int nlhs, /* number of expected outputs */
mxArray *plhs[], /* mxArray output pointer array */
int nrhs, /* number of inputs */
const mxArray *prhs[] /* mxArray input pointer array */
)
{
/* building graph is only call without gch */
GCoptimization *MyGraph = NULL;
char mode ='\0';
if ( nrhs < 2 ) {
mexErrMsgIdAndTxt("GraphCut","Too few input arguments");
}
/* get graph handle */
MyGraph = GetGCHandle(prhs[0]);
/* get mode */
GetScalar(prhs[1], mode);
switch (mode) {
case 'c': /* close */
delete MyGraph; /* ->~GCoptimization(); /* explicitly call the destructor */
MyGraph = NULL;
break;
case 's': /* set labels */
/* setting the labels: we have an extra parameter - int32 array of size w*l
* containing the new labels */
SetLabels(MyGraph, nlhs, plhs, nrhs, prhs);
break;
case 'g': /* get labels */
GetLabels(MyGraph, nlhs, plhs, nrhs, prhs);
break;
case 'a': /* a-b swaps */
ABswaps(MyGraph, nlhs, plhs, nrhs, prhs);
break;
case 'e': /* expand steps */
Expand(MyGraph, nlhs, plhs, nrhs, prhs);
break;
case 'n': /* get the current labeling energy */
Energy(MyGraph, nlhs, plhs, nrhs, prhs);
break;
case 't': /* truncation */
Truncate(MyGraph, nlhs, plhs, nrhs, prhs);
break;
default:
mexErrMsgIdAndTxt("GraphCut:mode","unrecognized mode");
}
/* update the gch output handle */
GraphHandle *pgh;
plhs[0] = mxCreateNumericMatrix(1, 1, MATLAB_POINTER_TYPE, mxREAL);
pgh = (GraphHandle*) mxGetData(plhs[0]);
*pgh = (GraphHandle)(MyGraph);
}
/**************************************************************************************/
/* set user defined labels to graph */
void SetLabels(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* we need exactly 3 input arguments: gch, mode, labels */
if (nrhs != 3 )
mexErrMsgIdAndTxt("GraphCut:SetLabels","Wrong number of input arguments");
if ( mxGetClassID(prhs[2]) != mxINT32_CLASS )
mexErrMsgIdAndTxt("GraphCut:SetLabels","labels array not int32");
if ( mxGetNumberOfElements(prhs[2]) != MyGraph->GetNumPixels() )
mexErrMsgIdAndTxt("GraphCut:SetLabels","wrong number of elements in labels array");
/* verify only one output parameter */
if (nlhs != 1)
mexErrMsgIdAndTxt("GraphCut:SetLabels","wrong number of output parameters");
MyGraph->SetAllLabels( (GCoptimization::LabelType*) mxGetData(prhs[2]) );
}
/**************************************************************************************/
/* set user defined labels to graph */
void GetLabels(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* exactly two input arguments */
if ( nrhs != 2 )
mexErrMsgIdAndTxt("GraphCut:GetLabels","Wrong number of input arguments");
/* we need exactly 2 output arguments: gch, labels */
if (nlhs != 2 )
mexErrMsgIdAndTxt("GraphCut:GetLabels","Wrong number of output arguments");
/* transposed result */
plhs[1] = mxCreateNumericMatrix(MyGraph->GetWidth(), MyGraph->GetNumPixels() / MyGraph->GetWidth(), mxINT32_CLASS, mxREAL);
MyGraph->ExportLabels( (GCoptimization::LabelType*) mxGetData(plhs[1]) );
}
/**************************************************************************************/
/* support several kinds of a/b swaps:
* 1. defualt - swap till convergence (no extra parameters)
* 2. #iterations - performs #iterations: GraphCut(gch, mode, #iteration)
* 3. swap two labels - GraphCut(gch, mode, l1, l2)
*/
void ABswaps(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int max_iterations(0), li(0);
GCoptimization::LabelType label[2] = { 0, 1};
GCoptimization::PixelType *indices = NULL;
/* check inputs */
switch (nrhs) {
case 2:
/* default - expand till convergence */
MyGraph->swap();
break;
case 3:
/* number of iterations */
GetScalar(prhs[2], max_iterations);
if ( max_iterations == 1 )
MyGraph->oneSwapIteration();
else
MyGraph->swap(max_iterations);
break;
case 4:
for ( li = 2; li<4;li++) {
GetScalar(prhs[li], label[li-2]);
if ( label[li-2] < 0 || label[li-2] >= MyGraph->GetNumLabels() )
mexErrMsgIdAndTxt("GraphCut:swap","invalid label value");
}
MyGraph->alpha_beta_swap(label[0], label[1]);
break;
default:
mexErrMsgIdAndTxt("GraphCut:swap","Too many input arguments");
}
/* output: at least one (gch) can output the labels as well */
if ( nlhs > 2 )
mexErrMsgIdAndTxt("GraphCut:swap","too many output arguments");
/* output labels */
if ( nlhs >= 2 ) {
/* set the lables */
/* transposed result */
plhs[1] = mxCreateNumericMatrix(MyGraph->GetWidth(), MyGraph->GetNumPixels() / MyGraph->GetWidth(), mxINT32_CLASS, mxREAL);
MyGraph->ExportLabels( (GCoptimization::LabelType*) mxGetData(plhs[1]) );
}
}
/**************************************************************************************/
/* support several kinds of expansion
* 1. default - expand untill convergence no extra parameters
* 2. #iterations - performs #iterations: GraphCut(gch, mode, #iteration)
* 3. expand label - expand specific label GraphCut(gch, mode, [], label)
* 4. expand label at specific indices GraphCut(gch, mode, [], label, indices) indices start with zero and not 1 like usuall matlab inices!!
*/
void Expand(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int num(0), max_iterations(0);
GCoptimization::LabelType label(0);
GCoptimization::PixelType *indices = NULL;
/* check inputs */
switch (nrhs) {
case 2:
/* default - expand till convergence */
MyGraph->expansion();
break;
case 3:
/* number of iterations */
GetScalar(prhs[2], max_iterations);
if ( max_iterations == 1 )
MyGraph->oneExpansionIteration();
else
MyGraph->expansion(max_iterations);
break;
case 5:
/* get indices */
if (mxGetClassID(prhs[4]) != mxINT32_CLASS)
mexErrMsgIdAndTxt("GraphCut:Expand","indices must be int32");
num = mxGetNumberOfElements(prhs[4]);
if (num < 0 || num > MyGraph->GetNumPixels())
mexErrMsgIdAndTxt("GraphCut:Expand","too many indices");
indices = (GCoptimization::PixelType*)mxGetData(prhs[4]);
case 4:
/* expand specific label */
if (mxGetNumberOfElements(prhs[2]) != 0)
mexErrMsgIdAndTxt("GraphCut:Expand","third argument must empty");
GetScalar(prhs[3], label);
if ( indices != NULL ) {
/* expand specific label at specific indices */
MyGraph->alpha_expansion(label, indices, num);
} else
MyGraph->alpha_expansion(label);
break;
default:
mexErrMsgIdAndTxt("GraphCut:Expand","Too many input arguments");
}
/* output: at least one (gch) can output the labels as well */
if ( nlhs > 2 )
mexErrMsgIdAndTxt("GraphCut:Expand","too many output arguments");
/* output labels */
if ( nlhs >= 2 ) {
/* set the lables */
/* transposed result */
plhs[1] = mxCreateNumericMatrix(MyGraph->GetWidth(), MyGraph->GetNumPixels() / MyGraph->GetWidth(), mxINT32_CLASS, mxREAL);
MyGraph->ExportLabels( (GCoptimization::LabelType*) mxGetData(plhs[1]) );
}
}
/**************************************************************************************/
/* extract energy of labeling
* [gch se de] = GraphCut(gch, 'n');
*/
void Energy(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if ( nrhs != 2 )
mexErrMsgIdAndTxt("GraphCut:Energy","Too many input arguments");
if ( nlhs != 3 )
mexErrMsgIdAndTxt("GraphCut:Energy","wrong number of output arguments");
GCoptimization::EnergyType *e;
plhs[1] = mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS, mxREAL);
e = (GCoptimization::EnergyType *)mxGetData(plhs[1]);
*e = MyGraph->giveSmoothEnergy();
plhs[2] = mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS, mxREAL);
e = (GCoptimization::EnergyType *)mxGetData(plhs[2]);
*e = MyGraph->giveDataEnergy();
}
/**************************************************************************************/
/* get/set truncation flag
* [gch tf] = GraphCut(gch, 'truncate', [ntf])
*/
void Truncate(GCoptimization *MyGraph, int nout, mxArray *pout[], int nin, const mxArray *pin[])
{
bool tf;
if ( nin == 2 ) {
// get the state
tf = MyGraph->GetTruncateState();
} else if ( nin == 3 ) {
// set the state
if ( mxIsComplex(pin[2]) || mxIsSparse(pin[2]) || mxGetNumberOfElements(pin[2])!=1 )
mexErrMsgIdAndTxt("GraphCut:Truncate","wrong truncate_flag");
tf = (mxGetScalar(pin[2]) != 0);
MyGraph->SetTruncateState(tf);
} else {
mexErrMsgIdAndTxt("GraphCut:Truncate","wrong number of input arguments");
}
if ( nout != 2 )
mexErrMsgIdAndTxt("GraphCut:Truncate","wrong number of output arguments");
pout[1] = mxCreateDoubleScalar( static_cast<double>(tf) );
}
/**************************************************************************************/
GCoptimization* GetGCHandle(const mxArray *x)
{
GraphHandle gch = 0;
GCoptimization* MyGraph = 0;
if ( mxGetNumberOfElements(x) != 1 ) {
mexErrMsgIdAndTxt("GraphCut:handle",
"Too many GC handles");
}
if ( mxGetClassID(x) != MATLAB_POINTER_TYPE ) {
mexErrMsgIdAndTxt("GraphCut:handle",
"GC handle argument is not of proper type");
}
gch = (GraphHandle*)mxGetData(x);
MyGraph = (GCoptimization*)(*(POINTER_CAST*)gch);
if ( MyGraph==NULL || ! MyGraph->IsClassValid() ) {
mexErrMsgIdAndTxt("GraphCut:handle",
"GC handle is not valid");
}
return MyGraph;
}
/**************************************************************************************/