00001
00002
00003
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <math.h>
00026 #include <string.h>
00027 #include <time.h>
00028 #include "GCoptimization.h"
00029
00030
00031 struct ForDataFn{
00032 int numLab;
00033 int *data;
00034 };
00035
00036
00037 int smoothFn(int p1, int p2, int l1, int l2)
00038 {
00039 if ( (l1-l2)*(l1-l2) <= 4 ) return((l1-l2)*(l1-l2));
00040 else return(4);
00041 }
00042
00043 int dataFn(int p, int l, void *data)
00044 {
00045 ForDataFn *myData = (ForDataFn *) data;
00046 int numLab = myData->numLab;
00047
00048 return( myData->data[p*numLab+l] );
00049 }
00050
00051
00052
00054
00055
00056
00057 void GridGraph_Individually(int width,int height,int num_pixels,int num_labels)
00058 {
00059
00060 int *result = new int[num_pixels];
00061
00062
00063
00064 try{
00065 GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
00066
00067
00068 for ( int i = 0; i < num_pixels; i++ )
00069 for (int l = 0; l < num_labels; l++ )
00070 if (i < 25 ){
00071 if( l == 0 ) gc->setDataCost(i,l,0);
00072 else gc->setDataCost(i,l,10);
00073 }
00074 else {
00075 if( l == 5 ) gc->setDataCost(i,l,0);
00076 else gc->setDataCost(i,l,10);
00077 }
00078
00079
00080 for ( int l1 = 0; l1 < num_labels; l1++ )
00081 for (int l2 = 0; l2 < num_labels; l2++ ){
00082 int cost = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
00083 gc->setSmoothCost(l1,l2,cost);
00084 }
00085
00086 printf("\nBefore optimization energy is %d",gc->compute_energy());
00087 gc->expansion(2);
00088 printf("\nAfter optimization energy is %d",gc->compute_energy());
00089
00090 for ( int i = 0; i < num_pixels; i++ )
00091 result[i] = gc->whatLabel(i);
00092
00093 delete gc;
00094 }
00095 catch (GCException e){
00096 e.Report();
00097 }
00098
00099 delete [] result;
00100 }
00101
00103
00104
00105
00106 void GridGraph_DArraySArray(int width,int height,int num_pixels,int num_labels)
00107 {
00108
00109 int *result = new int[num_pixels];
00110
00111
00112 int *data = new int[num_pixels*num_labels];
00113 for ( int i = 0; i < num_pixels; i++ )
00114 for (int l = 0; l < num_labels; l++ )
00115 if (i < 25 ){
00116 if( l == 0 ) data[i*num_labels+l] = 0;
00117 else data[i*num_labels+l] = 10;
00118 }
00119 else {
00120 if( l == 5 ) data[i*num_labels+l] = 0;
00121 else data[i*num_labels+l] = 10;
00122 }
00123
00124 int *smooth = new int[num_labels*num_labels];
00125 for ( int l1 = 0; l1 < num_labels; l1++ )
00126 for (int l2 = 0; l2 < num_labels; l2++ )
00127 smooth[l1+l2*num_labels] = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
00128
00129
00130 try{
00131 GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
00132 gc->setDataCost(data);
00133 gc->setSmoothCost(smooth);
00134 printf("\nBefore optimization energy is %d",gc->compute_energy());
00135 gc->expansion(2);
00136 printf("\nAfter optimization energy is %d",gc->compute_energy());
00137
00138 for ( int i = 0; i < num_pixels; i++ )
00139 result[i] = gc->whatLabel(i);
00140
00141 delete gc;
00142 }
00143 catch (GCException e){
00144 e.Report();
00145 }
00146
00147 delete [] result;
00148 delete [] smooth;
00149 delete [] data;
00150
00151 }
00153
00154
00155
00156 void GridGraph_DfnSfn(int width,int height,int num_pixels,int num_labels)
00157 {
00158
00159 int *result = new int[num_pixels];
00160
00161
00162 int *data = new int[num_pixels*num_labels];
00163 for ( int i = 0; i < num_pixels; i++ )
00164 for (int l = 0; l < num_labels; l++ )
00165 if (i < 25 ){
00166 if( l == 0 ) data[i*num_labels+l] = 0;
00167 else data[i*num_labels+l] = 10;
00168 }
00169 else {
00170 if( l == 5 ) data[i*num_labels+l] = 0;
00171 else data[i*num_labels+l] = 10;
00172 }
00173
00174
00175 try{
00176 GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
00177
00178
00179 ForDataFn toFn;
00180 toFn.data = data;
00181 toFn.numLab = num_labels;
00182
00183 gc->setDataCost(&dataFn,&toFn);
00184
00185
00186 gc->setSmoothCost(&smoothFn);
00187
00188 printf("\nBefore optimization energy is %d",gc->compute_energy());
00189 gc->expansion(2);
00190 printf("\nAfter optimization energy is %d",gc->compute_energy());
00191
00192 for ( int i = 0; i < num_pixels; i++ )
00193 result[i] = gc->whatLabel(i);
00194
00195 delete gc;
00196 }
00197 catch (GCException e){
00198 e.Report();
00199 }
00200
00201 delete [] result;
00202 delete [] data;
00203
00204 }
00206
00207
00208
00209 void GridGraph_DArraySArraySpatVarying(int width,int height,int num_pixels,int num_labels)
00210 {
00211 int *result = new int[num_pixels];
00212
00213
00214 int *data = new int[num_pixels*num_labels];
00215 for ( int i = 0; i < num_pixels; i++ )
00216 for (int l = 0; l < num_labels; l++ )
00217 if (i < 25 ){
00218 if( l == 0 ) data[i*num_labels+l] = 0;
00219 else data[i*num_labels+l] = 10;
00220 }
00221 else {
00222 if( l == 5 ) data[i*num_labels+l] = 0;
00223 else data[i*num_labels+l] = 10;
00224 }
00225
00226 int *smooth = new int[num_labels*num_labels];
00227 for ( int l1 = 0; l1 < num_labels; l1++ )
00228 for (int l2 = 0; l2 < num_labels; l2++ )
00229 smooth[l1+l2*num_labels] = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
00230
00231
00232
00233 int *V = new int[num_pixels];
00234 int *H = new int[num_pixels];
00235
00236
00237 for ( int i = 0; i < num_pixels; i++ ){
00238 H[i] = i+(i+1)%3;
00239 V[i] = i*(i+width)%7;
00240 }
00241
00242
00243 try{
00244 GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
00245 gc->setDataCost(data);
00246 gc->setSmoothCostVH(smooth,V,H);
00247 printf("\nBefore optimization energy is %d",gc->compute_energy());
00248 gc->expansion(2);
00249 printf("\nAfter optimization energy is %d",gc->compute_energy());
00250
00251 for ( int i = 0; i < num_pixels; i++ )
00252 result[i] = gc->whatLabel(i);
00253
00254 delete gc;
00255 }
00256 catch (GCException e){
00257 e.Report();
00258 }
00259
00260 delete [] result;
00261 delete [] smooth;
00262 delete [] data;
00263
00264
00265 }
00266
00268
00269
00270
00271 void GeneralGraph_DArraySArray(int width,int height,int num_pixels,int num_labels)
00272 {
00273
00274 int *result = new int[num_pixels];
00275
00276
00277 int *data = new int[num_pixels*num_labels];
00278 for ( int i = 0; i < num_pixels; i++ )
00279 for (int l = 0; l < num_labels; l++ )
00280 if (i < 25 ){
00281 if( l == 0 ) data[i*num_labels+l] = 0;
00282 else data[i*num_labels+l] = 10;
00283 }
00284 else {
00285 if( l == 5 ) data[i*num_labels+l] = 0;
00286 else data[i*num_labels+l] = 10;
00287 }
00288
00289 int *smooth = new int[num_labels*num_labels];
00290 for ( int l1 = 0; l1 < num_labels; l1++ )
00291 for (int l2 = 0; l2 < num_labels; l2++ )
00292 smooth[l1+l2*num_labels] = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
00293
00294
00295 try{
00296 GCoptimizationGeneralGraph *gc = new GCoptimizationGeneralGraph(num_pixels,num_labels);
00297 gc->setDataCost(data);
00298 gc->setSmoothCost(smooth);
00299
00300
00301
00302 for (int y = 0; y < height; y++ )
00303 for (int x = 1; x < width; x++ )
00304 gc->setNeighbors(x+y*width,x-1+y*width);
00305
00306
00307 for (int y = 1; y < height; y++ )
00308 for (int x = 0; x < width; x++ )
00309 gc->setNeighbors(x+y*width,x+(y-1)*width);
00310
00311 printf("\nBefore optimization energy is %d",gc->compute_energy());
00312 gc->expansion(2);
00313 printf("\nAfter optimization energy is %d",gc->compute_energy());
00314
00315 for ( int i = 0; i < num_pixels; i++ )
00316 result[i] = gc->whatLabel(i);
00317
00318 delete gc;
00319 }
00320 catch (GCException e){
00321 e.Report();
00322 }
00323
00324 delete [] result;
00325 delete [] smooth;
00326 delete [] data;
00327
00328 }
00330
00331
00332
00333
00334
00335 void GeneralGraph_DArraySArraySpatVarying(int width,int height,int num_pixels,int num_labels)
00336 {
00337 int *result = new int[num_pixels];
00338
00339
00340 int *data = new int[num_pixels*num_labels];
00341 for ( int i = 0; i < num_pixels; i++ )
00342 for (int l = 0; l < num_labels; l++ )
00343 if (i < 25 ){
00344 if( l == 0 ) data[i*num_labels+l] = 0;
00345 else data[i*num_labels+l] = 10;
00346 }
00347 else {
00348 if( l == 5 ) data[i*num_labels+l] = 0;
00349 else data[i*num_labels+l] = 10;
00350 }
00351
00352 int *smooth = new int[num_labels*num_labels];
00353 for ( int l1 = 0; l1 < num_labels; l1++ )
00354 for (int l2 = 0; l2 < num_labels; l2++ )
00355 smooth[l1+l2*num_labels] = (l1-l2)*(l1-l2) <= 4 ? (l1-l2)*(l1-l2):4;
00356
00357
00358 try{
00359 GCoptimizationGeneralGraph *gc = new GCoptimizationGeneralGraph(num_pixels,num_labels);
00360 gc->setDataCost(data);
00361 gc->setSmoothCost(smooth);
00362
00363
00364
00365 for (int y = 0; y < height; y++ )
00366 for (int x = 1; x < width; x++ ){
00367 int p1 = x-1+y*width;
00368 int p2 =x+y*width;
00369 gc->setNeighbors(p1,p2,p1+p2);
00370 }
00371
00372
00373 for (int y = 1; y < height; y++ )
00374 for (int x = 0; x < width; x++ ){
00375 int p1 = x+(y-1)*width;
00376 int p2 =x+y*width;
00377 gc->setNeighbors(p1,p2,p1*p2);
00378 }
00379
00380 printf("\nBefore optimization energy is %d",gc->compute_energy());
00381 gc->expansion(2);
00382 printf("\nAfter optimization energy is %d",gc->compute_energy());
00383
00384 for ( int i = 0; i < num_pixels; i++ )
00385 result[i] = gc->whatLabel(i);
00386
00387 delete gc;
00388 }
00389 catch (GCException e){
00390 e.Report();
00391 }
00392
00393 delete [] result;
00394 delete [] smooth;
00395 delete [] data;
00396
00397
00398 }
00400
00401 int main(int argc, char **argv)
00402 {
00403 int width = 10;
00404 int height = 5;
00405 int num_pixels = width*height;
00406 int num_labels = 7;
00407
00408
00409
00410 GridGraph_Individually(width,height,num_pixels,num_labels);
00411
00412
00413 GridGraph_DArraySArray(width,height,num_pixels,num_labels);
00414
00415
00416 GridGraph_DfnSfn(width,height,num_pixels,num_labels);
00417
00418
00419
00420 GridGraph_DArraySArraySpatVarying(width,height,num_pixels,num_labels);
00421
00422
00423
00424
00425 GeneralGraph_DArraySArray(width,height,num_pixels,num_labels);
00426
00427
00428
00429 GeneralGraph_DArraySArraySpatVarying(width,height,num_pixels,num_labels);
00430
00431 printf("\n Finished %d (%d) clock per sec %d",clock()/CLOCKS_PER_SEC,clock(),CLOCKS_PER_SEC);
00432
00433 return 0;
00434 }
00435
00437