example.cpp
Go to the documentation of this file.
00001 
00002 // Example illustrating the use of GCoptimization.cpp
00003 //
00005 //
00006 //  Optimization problem:
00007 //  is a set of sites (pixels) of width 10 and hight 5. Thus number of pixels is 50
00008 //  grid neighborhood: each pixel has its left, right, up, and bottom pixels as neighbors
00009 //  7 labels
00010 //  Data costs: D(pixel,label) = 0 if pixel < 25 and label = 0
00011 //            : D(pixel,label) = 10 if pixel < 25 and label is not  0
00012 //            : D(pixel,label) = 0 if pixel >= 25 and label = 5
00013 //            : D(pixel,label) = 10 if pixel >= 25 and label is not  5
00014 // Smoothness costs: V(p1,p2,l1,l2) = min( (l1-l2)*(l1-l2) , 4 )
00015 // Below in the main program, we illustrate different ways of setting data and smoothness costs
00016 // that our interface allow and solve this optimizaiton problem
00017 
00018 // For most of the examples, we use no spatially varying pixel dependent terms. 
00019 // For some examples, to demonstrate spatially varying terms we use
00020 // V(p1,p2,l1,l2) = w_{p1,p2}*[min((l1-l2)*(l1-l2),4)], with 
00021 // w_{p1,p2} = p1+p2 if |p1-p2| == 1 and w_{p1,p2} = p1*p2 if |p1-p2| is not 1
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 // smoothness and data costs are set up one by one, individually
00055 // grid neighborhood structure is assumed
00056 //
00057 void GridGraph_Individually(int width,int height,int num_pixels,int num_labels)
00058 {
00059 
00060         int *result = new int[num_pixels];   // stores result of optimization
00061 
00062 
00063 
00064         try{
00065                 GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
00066 
00067                 // first set up data costs individually
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                 // next set up smoothness costs individually
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);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
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 // in this version, set data and smoothness terms using arrays
00104 // grid neighborhood structure is assumed
00105 //
00106 void GridGraph_DArraySArray(int width,int height,int num_pixels,int num_labels)
00107 {
00108 
00109         int *result = new int[num_pixels];   // stores result of optimization
00110 
00111         // first set up the array for data costs
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         // next set up the array for smooth costs
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);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
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 // in this version, set data and smoothness terms using arrays
00154 // grid neighborhood structure is assumed
00155 //
00156 void GridGraph_DfnSfn(int width,int height,int num_pixels,int num_labels)
00157 {
00158 
00159         int *result = new int[num_pixels];   // stores result of optimization
00160 
00161         // first set up the array for data costs
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                 // set up the needed data to pass to function for the data costs
00179                 ForDataFn toFn;
00180                 toFn.data = data;
00181                 toFn.numLab = num_labels;
00182 
00183                 gc->setDataCost(&dataFn,&toFn);
00184 
00185                 // smoothness comes from function pointer
00186                 gc->setSmoothCost(&smoothFn);
00187 
00188                 printf("\nBefore optimization energy is %d",gc->compute_energy());
00189                 gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
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 // Uses spatially varying smoothness terms. That is 
00207 // V(p1,p2,l1,l2) = w_{p1,p2}*[min((l1-l2)*(l1-l2),4)], with 
00208 // w_{p1,p2} = p1+p2 if |p1-p2| == 1 and w_{p1,p2} = p1*p2 if |p1-p2| is not 1
00209 void GridGraph_DArraySArraySpatVarying(int width,int height,int num_pixels,int num_labels)
00210 {
00211         int *result = new int[num_pixels];   // stores result of optimization
00212 
00213         // first set up the array for data costs
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         // next set up the array for smooth costs
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         // next set up spatially varying arrays V and H
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);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
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 // in this version, set data and smoothness terms using arrays
00269 // grid neighborhood is set up "manually"
00270 //
00271 void GeneralGraph_DArraySArray(int width,int height,int num_pixels,int num_labels)
00272 {
00273 
00274         int *result = new int[num_pixels];   // stores result of optimization
00275 
00276         // first set up the array for data costs
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         // next set up the array for smooth costs
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                 // now set up a grid neighborhood system
00301                 // first set up horizontal neighbors
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                 // next set up vertical neighbors
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);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
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 // in this version, set data and smoothness terms using arrays
00331 // grid neighborhood is set up "manually". Uses spatially varying terms. Namely
00332 // V(p1,p2,l1,l2) = w_{p1,p2}*[min((l1-l2)*(l1-l2),4)], with 
00333 // w_{p1,p2} = p1+p2 if |p1-p2| == 1 and w_{p1,p2} = p1*p2 if |p1-p2| is not 1
00334 
00335 void GeneralGraph_DArraySArraySpatVarying(int width,int height,int num_pixels,int num_labels)
00336 {
00337         int *result = new int[num_pixels];   // stores result of optimization
00338 
00339         // first set up the array for data costs
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         // next set up the array for smooth costs
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                 // now set up a grid neighborhood system
00364                 // first set up horizontal neighbors
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                 // next set up vertical neighbors
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);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
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         // smoothness and data costs are set up one by one, individually
00410         GridGraph_Individually(width,height,num_pixels,num_labels);
00411 
00412         // smoothness and data costs are set up using arrays
00413         GridGraph_DArraySArray(width,height,num_pixels,num_labels);
00414 
00415         // smoothness and data costs are set up using functions
00416         GridGraph_DfnSfn(width,height,num_pixels,num_labels);
00417         
00418         // smoothness and data costs are set up using arrays. 
00419         // spatially varying terms are present
00420         GridGraph_DArraySArraySpatVarying(width,height,num_pixels,num_labels);
00421 
00422         //Will pretend our graph is 
00423         //general, and set up a neighborhood system
00424         // which actually is a grid
00425         GeneralGraph_DArraySArray(width,height,num_pixels,num_labels);
00426 
00427         //Will pretend our graph is general, and set up a neighborhood system
00428         // which actually is a grid. Also uses spatially varying terms
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 


tabletop_pushing
Author(s): Tucker Hermans
autogenerated on Wed Nov 27 2013 11:59:44