-
Notifications
You must be signed in to change notification settings - Fork 31
/
example.cpp
437 lines (347 loc) · 12.9 KB
/
example.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
//////////////////////////////////////////////////////////////////////////////
// Example illustrating the use of GCoptimization.cpp
//
/////////////////////////////////////////////////////////////////////////////
//
// Optimization problem:
// is a set of sites (pixels) of width 10 and hight 5. Thus number of pixels is 50
// grid neighborhood: each pixel has its left, right, up, and bottom pixels as neighbors
// 7 labels
// Data costs: D(pixel,label) = 0 if pixel < 25 and label = 0
// : D(pixel,label) = 10 if pixel < 25 and label is not 0
// : D(pixel,label) = 0 if pixel >= 25 and label = 5
// : D(pixel,label) = 10 if pixel >= 25 and label is not 5
// Smoothness costs: V(p1,p2,l1,l2) = min( (l1-l2)*(l1-l2) , 4 )
// Below in the main program, we illustrate different ways of setting data and smoothness costs
// that our interface allow and solve this optimizaiton problem
// For most of the examples, we use no spatially varying pixel dependent terms.
// For some examples, to demonstrate spatially varying terms we use
// V(p1,p2,l1,l2) = w_{p1,p2}*[min((l1-l2)*(l1-l2),4)], with
// w_{p1,p2} = p1+p2 if |p1-p2| == 1 and w_{p1,p2} = p1*p2 if |p1-p2| is not 1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include "GCoptimization.h"
struct ForDataFn{
int numLab;
int *data;
};
int smoothFn(int p1, int p2, int l1, int l2)
{
if ( (l1-l2)*(l1-l2) <= 4 ) return((l1-l2)*(l1-l2));
else return(4);
}
int dataFn(int p, int l, void *data)
{
ForDataFn *myData = (ForDataFn *) data;
int numLab = myData->numLab;
return( myData->data[p*numLab+l] );
}
////////////////////////////////////////////////////////////////////////////////
// smoothness and data costs are set up one by one, individually
// grid neighborhood structure is assumed
//
void GridGraph_Individually(int width,int height,int num_pixels,int num_labels)
{
int *result = new int[num_pixels]; // stores result of optimization
try{
GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
// first set up data costs individually
for ( int i = 0; i < num_pixels; i++ )
for (int l = 0; l < num_labels; l++ )
if (i < 25 ){
if( l == 0 ) gc->setDataCost(i,l,0);
else gc->setDataCost(i,l,10);
}
else {
if( l == 5 ) gc->setDataCost(i,l,0);
else gc->setDataCost(i,l,10);
}
// next set up smoothness costs individually
for ( int l1 = 0; l1 < num_labels; l1++ )
for (int l2 = 0; l2 < num_labels; l2++ ){
int cost = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
gc->setSmoothCost(l1,l2,cost);
}
printf("\nBefore optimization energy is %lld",gc->compute_energy());
gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
printf("\nAfter optimization energy is %lld",gc->compute_energy());
for ( int i = 0; i < num_pixels; i++ )
result[i] = gc->whatLabel(i);
delete gc;
}
catch (GCException e){
e.Report();
}
delete [] result;
}
////////////////////////////////////////////////////////////////////////////////
// in this version, set data and smoothness terms using arrays
// grid neighborhood structure is assumed
//
void GridGraph_DArraySArray(int width,int height,int num_pixels,int num_labels)
{
int *result = new int[num_pixels]; // stores result of optimization
// first set up the array for data costs
int *data = new int[num_pixels*num_labels];
for ( int i = 0; i < num_pixels; i++ )
for (int l = 0; l < num_labels; l++ )
if (i < 25 ){
if( l == 0 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
else {
if( l == 5 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
// next set up the array for smooth costs
int *smooth = new int[num_labels*num_labels];
for ( int l1 = 0; l1 < num_labels; l1++ )
for (int l2 = 0; l2 < num_labels; l2++ )
smooth[l1+l2*num_labels] = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
try{
GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
gc->setDataCost(data);
gc->setSmoothCost(smooth);
printf("\nBefore optimization energy is %lld",gc->compute_energy());
gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
printf("\nAfter optimization energy is %lld",gc->compute_energy());
for ( int i = 0; i < num_pixels; i++ )
result[i] = gc->whatLabel(i);
delete gc;
}
catch (GCException e){
e.Report();
}
delete [] result;
delete [] smooth;
delete [] data;
}
////////////////////////////////////////////////////////////////////////////////
// in this version, set data and smoothness terms using arrays
// grid neighborhood structure is assumed
//
void GridGraph_DfnSfn(int width,int height,int num_pixels,int num_labels)
{
int *result = new int[num_pixels]; // stores result of optimization
// first set up the array for data costs
int *data = new int[num_pixels*num_labels];
for ( int i = 0; i < num_pixels; i++ )
for (int l = 0; l < num_labels; l++ )
if (i < 25 ){
if( l == 0 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
else {
if( l == 5 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
try{
GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
// set up the needed data to pass to function for the data costs
ForDataFn toFn;
toFn.data = data;
toFn.numLab = num_labels;
gc->setDataCost(&dataFn,&toFn);
// smoothness comes from function pointer
gc->setSmoothCost(&smoothFn);
printf("\nBefore optimization energy is %lld",gc->compute_energy());
gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
printf("\nAfter optimization energy is %lld",gc->compute_energy());
for ( int i = 0; i < num_pixels; i++ )
result[i] = gc->whatLabel(i);
delete gc;
}
catch (GCException e){
e.Report();
}
delete [] result;
delete [] data;
}
////////////////////////////////////////////////////////////////////////////////
// Uses spatially varying smoothness terms. That is
// V(p1,p2,l1,l2) = w_{p1,p2}*[min((l1-l2)*(l1-l2),4)], with
// w_{p1,p2} = p1+p2 if |p1-p2| == 1 and w_{p1,p2} = p1*p2 if |p1-p2| is not 1
void GridGraph_DArraySArraySpatVarying(int width,int height,int num_pixels,int num_labels)
{
int *result = new int[num_pixels]; // stores result of optimization
// first set up the array for data costs
int *data = new int[num_pixels*num_labels];
for ( int i = 0; i < num_pixels; i++ )
for (int l = 0; l < num_labels; l++ )
if (i < 25 ){
if( l == 0 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
else {
if( l == 5 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
// next set up the array for smooth costs
int *smooth = new int[num_labels*num_labels];
for ( int l1 = 0; l1 < num_labels; l1++ )
for (int l2 = 0; l2 < num_labels; l2++ )
smooth[l1+l2*num_labels] = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
// next set up spatially varying arrays V and H
int *V = new int[num_pixels];
int *H = new int[num_pixels];
for ( int i = 0; i < num_pixels; i++ ){
H[i] = i+(i+1)%3;
V[i] = i*(i+width)%7;
}
try{
GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
gc->setDataCost(data);
gc->setSmoothCostVH(smooth,V,H);
printf("\nBefore optimization energy is %lld",gc->compute_energy());
gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
printf("\nAfter optimization energy is %lld",gc->compute_energy());
for ( int i = 0; i < num_pixels; i++ )
result[i] = gc->whatLabel(i);
delete gc;
}
catch (GCException e){
e.Report();
}
delete [] result;
delete [] smooth;
delete [] data;
}
////////////////////////////////////////////////////////////////////////////////
// in this version, set data and smoothness terms using arrays
// grid neighborhood is set up "manually"
//
void GeneralGraph_DArraySArray(int width,int height,int num_pixels,int num_labels)
{
int *result = new int[num_pixels]; // stores result of optimization
// first set up the array for data costs
int *data = new int[num_pixels*num_labels];
for ( int i = 0; i < num_pixels; i++ )
for (int l = 0; l < num_labels; l++ )
if (i < 25 ){
if( l == 0 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
else {
if( l == 5 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
// next set up the array for smooth costs
int *smooth = new int[num_labels*num_labels];
for ( int l1 = 0; l1 < num_labels; l1++ )
for (int l2 = 0; l2 < num_labels; l2++ )
smooth[l1+l2*num_labels] = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
try{
GCoptimizationGeneralGraph *gc = new GCoptimizationGeneralGraph(num_pixels,num_labels);
gc->setDataCost(data);
gc->setSmoothCost(smooth);
// now set up a grid neighborhood system
// first set up horizontal neighbors
for (int y = 0; y < height; y++ )
for (int x = 1; x < width; x++ )
gc->setNeighbors(x+y*width,x-1+y*width);
// next set up vertical neighbors
for (int y = 1; y < height; y++ )
for (int x = 0; x < width; x++ )
gc->setNeighbors(x+y*width,x+(y-1)*width);
printf("\nBefore optimization energy is %lld",gc->compute_energy());
gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
printf("\nAfter optimization energy is %lld",gc->compute_energy());
for ( int i = 0; i < num_pixels; i++ )
result[i] = gc->whatLabel(i);
delete gc;
}
catch (GCException e){
e.Report();
}
delete [] result;
delete [] smooth;
delete [] data;
}
////////////////////////////////////////////////////////////////////////////////
// in this version, set data and smoothness terms using arrays
// grid neighborhood is set up "manually". Uses spatially varying terms. Namely
// V(p1,p2,l1,l2) = w_{p1,p2}*[min((l1-l2)*(l1-l2),4)], with
// w_{p1,p2} = p1+p2 if |p1-p2| == 1 and w_{p1,p2} = p1*p2 if |p1-p2| is not 1
void GeneralGraph_DArraySArraySpatVarying(int width,int height,int num_pixels,int num_labels)
{
int *result = new int[num_pixels]; // stores result of optimization
// first set up the array for data costs
int *data = new int[num_pixels*num_labels];
for ( int i = 0; i < num_pixels; i++ )
for (int l = 0; l < num_labels; l++ )
if (i < 25 ){
if( l == 0 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
else {
if( l == 5 ) data[i*num_labels+l] = 0;
else data[i*num_labels+l] = 10;
}
// next set up the array for smooth costs
int *smooth = new int[num_labels*num_labels];
for ( int l1 = 0; l1 < num_labels; l1++ )
for (int l2 = 0; l2 < num_labels; l2++ )
smooth[l1+l2*num_labels] = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
try{
GCoptimizationGeneralGraph *gc = new GCoptimizationGeneralGraph(num_pixels,num_labels);
gc->setDataCost(data);
gc->setSmoothCost(smooth);
// now set up a grid neighborhood system
// first set up horizontal neighbors
for (int y = 0; y < height; y++ )
for (int x = 1; x < width; x++ ){
int p1 = x-1+y*width;
int p2 =x+y*width;
gc->setNeighbors(p1,p2,p1+p2);
}
// next set up vertical neighbors
for (int y = 1; y < height; y++ )
for (int x = 0; x < width; x++ ){
int p1 = x+(y-1)*width;
int p2 =x+y*width;
gc->setNeighbors(p1,p2,p1*p2);
}
printf("\nBefore optimization energy is %lld",gc->compute_energy());
gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
printf("\nAfter optimization energy is %lld",gc->compute_energy());
for ( int i = 0; i < num_pixels; i++ )
result[i] = gc->whatLabel(i);
delete gc;
}
catch (GCException e){
e.Report();
}
delete [] result;
delete [] smooth;
delete [] data;
}
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
int width = 10;
int height = 5;
int num_pixels = width*height;
int num_labels = 7;
// smoothness and data costs are set up one by one, individually
GridGraph_Individually(width,height,num_pixels,num_labels);
// smoothness and data costs are set up using arrays
GridGraph_DArraySArray(width,height,num_pixels,num_labels);
// smoothness and data costs are set up using functions
GridGraph_DfnSfn(width,height,num_pixels,num_labels);
// smoothness and data costs are set up using arrays.
// spatially varying terms are present
GridGraph_DArraySArraySpatVarying(width,height,num_pixels,num_labels);
//Will pretend our graph is
//general, and set up a neighborhood system
// which actually is a grid
GeneralGraph_DArraySArray(width,height,num_pixels,num_labels);
//Will pretend our graph is general, and set up a neighborhood system
// which actually is a grid. Also uses spatially varying terms
GeneralGraph_DArraySArraySpatVarying(width,height,num_pixels,num_labels);
printf("\n Finished %lu (%lu) clock per sec %d",clock()/CLOCKS_PER_SEC,clock(),CLOCKS_PER_SEC);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////