compare.c
Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include <math.h>
00004 #include <string.h>
00005 #include <stdlib.h>
00006 
00007 
00008 #define         RAS_MAGIC       0x59a66a95      /* rasterfile magic number */
00009 
00010 #define         MAX_REGIONS     246     /* max number of surface regions */
00011 #define         MAX_LABELS      256     /* restricted by data structure types */
00012 #define         MAX_ANGLES      100     /* max number of angles in GT */
00013 #define         FW_LABELS       19      /* field widths, in characters, used */
00014 #define         FW_MEASURES     12      /* in output file, for labels columns,*/
00015 #define         FW_CLASSIFS     14      /* measures columns and classif.s col */
00016 #define         BLANKS          "                         "
00017 
00018 
00019 /******************** IMPORTANT -- EDIT PATHS ********************/
00020 /* paths to search for GT and MS files */
00021 char            *PATHS[]={"./", /* leave me first! */
00022                         NULL /* must be at end! */};
00023 
00024 /* NOISE_DEFINED => which of the 10 labels are used */
00025 int             NOISE_DEFINED[10] =   {1,1,1,1,0,0,0,0,0,0  };
00026 /* globals for ROWS and COLS */
00027 int             ROWS,COLS;
00028 /* global for compare threshold T */
00029 double          T_SEG;
00030 
00031 int             LoadFiles();
00032 void            ComputeTables();
00033 void            ClassifyRegions();
00034 double          AngleBetweenVectors();
00035 void            OutputTable();
00036 void            OutputImage();
00037 
00038 /*
00039  *  This program will take as input two segmented images, one segmented 
00040  *  by a machine algorithm (MS), the other by a hand segmentation tool (GT).
00041  *  Treating the hand segmentation as the correct result, this tool  
00042  *  compares them to evaluate the performance of the machine routine.   
00043  */
00044 
00045 int main(argc,argv)
00046 
00047 int     argc;
00048 char    *argv[];
00049 
00050 {
00051 unsigned char   *GTImage,*MSImage;
00052 char            FilePrefix[160],Filename[150],text[500];
00053 int             i,j,k;
00054 char            GTRegionClassifications[MAX_LABELS];
00055 char            MSRegionClassifications[MAX_LABELS];
00056 int             GTRegionMappings[MAX_LABELS],MSRegionMappings[MAX_LABELS];
00057 double          GTMappingMeasures[MAX_LABELS][2];
00058 double          MSMappingMeasures[MAX_LABELS][2];
00059 int             OverlapTable[MAX_LABELS][MAX_LABELS];
00060 int             GTRegionSizes[MAX_LABELS],MSRegionSizes[MAX_LABELS];
00061 int             TotalRegionsInMS,TotalRegionsInGT;
00062 int             TotalCorrectClassifications,TotalOversegClassifications;
00063 int             TotalUndersegClassifications,TotalAnglesInGT;
00064 int             TotalMissedClassifications,TotalNoiseClassifications;
00065 int             TotalCorrectPixelCount, TotalGTPixelCount, TotalNotCorrectMSPixels;
00066 int             GTAngleRegions[MAX_ANGLES][2];
00067 double          GTAngleValues[MAX_ANGLES];
00068 double          MSRegionNormals[MAX_LABELS][3];
00069 int             TotalAnglesCompared;
00070 double          C_Avg_m1,C_Avg_m2,O_Avg_m1,O_Avg_m2,U_Avg_m1,U_Avg_m2;
00071 double          AvgAngDiff,stddev;
00072 int             PerImageGTRegions,PerImageCorrect;
00073 int             PerImageOverseg,PerImageUnderseg,PerImageMissed;
00074 int             PerImageNoise,TotalImagesCompared;
00075 int             PerImageCorrectPixels, PerImageGTPixels, PerImageNotCorrectMSPixels;
00076 double          PerImageAngDiff,PerImageStdDev,TotalImagesWithAnglesCompared;
00077 int             PrintFile,OutputImages,StartImage,EndImage,PrintHelp;
00078 int             NoNormals;
00079 int             Sequence[50],SequenceTotal;
00080 
00081 
00082 
00083 OutputImages=StartImage=EndImage=SequenceTotal=PrintHelp=0;
00084 NoNormals=0;
00085 T_SEG=0.51;
00086 if (argc > 1)
00087   {
00088   for (i=1; i<argc; i++)
00089     {
00090     if (strcmp(argv[i],"-images") == 0)
00091       OutputImages=1;
00092     else if (strcmp(argv[i],"-start") == 0)
00093       {
00094       if (i == argc-1  ||  SequenceTotal != 0)
00095         break;
00096       StartImage=atoi(argv[i+1]);
00097       i++;
00098       }
00099     else if (strcmp(argv[i],"-end") == 0)
00100       {
00101       if (i == argc-1  ||  SequenceTotal != 0)
00102         break;
00103       EndImage=atoi(argv[i+1]);
00104       i++;
00105       }
00106     else if (strcmp(argv[i],"-sequence") == 0)
00107       {
00108       if (i == argc-1  ||  StartImage != 0  ||  EndImage != 0)
00109         break;
00110       i++;
00111       j=0;
00112       do
00113         {
00114         k=0;
00115         do
00116           {
00117           text[k]=argv[i][j+k];
00118           k++;
00119           }
00120         while ((int)strlen(argv[i]) > j+k  &&  argv[i][j+k] != ',');
00121         text[k]='\0';
00122         j+=(k+1);
00123         Sequence[SequenceTotal]=atoi(text);
00124         SequenceTotal++;
00125         }
00126       while (strlen(argv[i]) > j);
00127       EndImage=SequenceTotal-1;
00128       }
00129     else if (strcmp(argv[i],"-T") == 0)
00130       {
00131       if (i == argc-1)
00132         break;
00133       T_SEG=atof(argv[i+1]);
00134       if (T_SEG <= 0.5  ||  T_SEG > 1.0)
00135         PrintHelp=1;
00136       i++;
00137       }
00138     else if (strcmp(argv[i],"-noNormals") == 0)
00139       {
00140         NoNormals = 1;
00141       }
00142     else if (i > 1)
00143       break;
00144     }
00145   if (i != argc)
00146     PrintHelp=1;
00147   if (StartImage > EndImage)
00148     StartImage=EndImage;
00149   }
00150 
00151 if (argc == 1  ||  PrintHelp == 1)
00152   {
00153   printf("\nUsage:  compare [prefix] [-start #] [-end #]\n");
00154   printf("                [-sequence #[,#...]] [-images] [-T #]\n\n");
00155   printf("This program will look for the following images:\n");
00156   printf("\t[prefix].gt-seg, [prefix].gt-ang  (ground truth files)\n");
00157   printf("\t[prefix].ms-seg, [prefix].ms-nor  (machine segmentation files)\n");
00158   printf("...in the following directories:\n");
00159   i=0;
00160   while (PATHS[i])
00161     printf("\t%s\n",PATHS[i++]);
00162   printf("and will write the file ./[prefix].compared (table file)\n");
00163   printf("If -images is specified, the program will also write:\n");
00164   printf("\t./[prefix].gt-lab (labeled GT compare image)\n");
00165   printf("\t./[prefix].ms-lab (labeled MS compare image)\n");
00166   printf("If -start # and/or -end # is specified, the program will run on\n");
00167   printf("\tthe sequence of images [prefix].[start#] ... [prefix].[end#],\n");
00168   printf("\tcomputing averages across the image sequence.\n");
00169   printf("If -sequence #[,#...] is specified, the program will run on the\n");
00170   printf("\tarbitrary sequence of image numbers given and report averages.\n");
00171   printf("The compare threshold can be set using -T.  Valid values\n");
00172   printf("\tare 0.5 < T <= 1.0; the default is 0.51.\n");
00173   printf("If no normal Information should be used, start the program with\n");
00174   printf("\t -noNormals\n\n");
00175   if (PrintHelp == 1)
00176     exit(1);
00177   printf("Enter range image filename prefix: ");
00178   if(!scanf("%s",Filename))return(1);
00179   }
00180 else
00181   strcpy(Filename,argv[1]);
00182 
00183 PerImageGTRegions=PerImageCorrect=PerImageAngDiff=PerImageStdDev=0;
00184 PerImageOverseg=PerImageUnderseg=PerImageMissed=PerImageNoise=0;
00185 PerImageCorrectPixels=PerImageGTPixels=PerImageNotCorrectMSPixels=0;
00186 TotalImagesCompared=TotalImagesWithAnglesCompared=0;
00187 TotalCorrectPixelCount=TotalGTPixelCount=TotalNotCorrectMSPixels=0;
00188 for (j=StartImage; j<=EndImage; j++)
00189   {
00190   if (StartImage == EndImage)
00191     strcpy(FilePrefix,Filename);
00192   else if (SequenceTotal != 0)
00193     sprintf(FilePrefix,"%s.%d",Filename,Sequence[j]);
00194   else
00195     sprintf(FilePrefix,"%s.%d",Filename,j);
00196 
00197   printf("Loading ground truth files...\n");
00198   fflush(stdout);
00199   if (LoadFiles(FilePrefix,&ROWS,&COLS,&GTImage,&TotalRegionsInGT,
00200         &TotalAnglesInGT,GTAngleRegions,GTAngleValues,
00201         &MSImage,&TotalRegionsInMS,MSRegionNormals,&NoNormals) != 1)
00202     continue;
00203 
00204 
00205   printf("Computing tables...\n");
00206   fflush(stdout);
00207         /* Compute the 3 tables needed for region classification */
00208   ComputeTables(GTImage,MSImage,OverlapTable,GTRegionSizes,MSRegionSizes,
00209                 TotalRegionsInGT,TotalRegionsInMS);
00210 
00211 
00212   for (i=0; i<MAX_LABELS; i++)
00213     {
00214     GTRegionClassifications[i]=MSRegionClassifications[i]=' ';
00215     GTRegionMappings[i]=MSRegionMappings[i]=0;
00216     GTMappingMeasures[i][0]=GTMappingMeasures[i][1]=0.0;
00217     MSMappingMeasures[i][0]=MSMappingMeasures[i][1]=0.0;
00218     }
00219   printf("Performing classifications...\n");
00220   fflush(stdout);
00221   ClassifyRegions(OverlapTable,GTRegionSizes,MSRegionSizes,
00222         TotalRegionsInGT,TotalRegionsInMS,
00223         GTRegionClassifications,GTRegionMappings,GTMappingMeasures,
00224         MSRegionClassifications,MSRegionMappings,MSMappingMeasures,
00225         &TotalCorrectPixelCount, &TotalGTPixelCount, &TotalNotCorrectMSPixels);
00226 
00227 
00228 
00229   if (StartImage != EndImage)
00230     PrintFile=0;        /* to compute averages without writing file */
00231   else
00232     PrintFile=1;
00233   OutputTable(FilePrefix,TotalRegionsInGT,TotalRegionsInMS,
00234         TotalAnglesInGT,GTAngleRegions,GTAngleValues,MSRegionNormals,
00235         GTRegionClassifications,GTRegionMappings,GTMappingMeasures,
00236         MSRegionClassifications,MSRegionMappings,MSMappingMeasures,
00237         PrintFile,
00238         &TotalCorrectClassifications,&C_Avg_m1,&C_Avg_m2,
00239         &TotalAnglesCompared,&AvgAngDiff,&stddev,
00240         &TotalOversegClassifications,&O_Avg_m1,&O_Avg_m2,
00241         &TotalUndersegClassifications,&U_Avg_m1,&U_Avg_m2,
00242         &TotalMissedClassifications,&TotalNoiseClassifications, &NoNormals);
00243   printf("**** %s:\n",FilePrefix);
00244   printf("\t  GT reg: %d  Correct: %d",
00245         TotalRegionsInGT,TotalCorrectClassifications);
00246   if(NoNormals)printf("\n");
00247   else printf("   Angle:  %f   StdDev:  %f\n",
00248         AvgAngDiff,stddev);
00249   printf("\t  Overseg: %d  Underseg: %d  Missed: %d  Noise: %d\n",
00250         TotalOversegClassifications,TotalUndersegClassifications,
00251         TotalMissedClassifications,TotalNoiseClassifications);
00252   printf("\t  #pixel in correct regions: %d  Total: %d  InPercent: %lf\n",
00253                   TotalCorrectPixelCount, TotalGTPixelCount,
00254                   ((double)TotalCorrectPixelCount/(double)TotalGTPixelCount)*100);
00255   printf("\t  #pixel missed in gt: %d  #pixel not correct in ms: %d\n",
00256                   TotalGTPixelCount - TotalCorrectPixelCount, TotalNotCorrectMSPixels);
00257   PerImageGTRegions+=TotalRegionsInGT;
00258   PerImageCorrect+=TotalCorrectClassifications;
00259   PerImageAngDiff+=AvgAngDiff;
00260   PerImageStdDev+=stddev;
00261   PerImageOverseg+=TotalOversegClassifications;
00262   PerImageUnderseg+=TotalUndersegClassifications;
00263   PerImageMissed+=TotalMissedClassifications;
00264   PerImageNoise+=TotalNoiseClassifications;
00265   PerImageCorrectPixels+=TotalCorrectPixelCount;
00266   PerImageGTPixels+=TotalGTPixelCount;
00267   PerImageNotCorrectMSPixels+=TotalNotCorrectMSPixels;
00268   TotalImagesCompared++;
00269   if (TotalAnglesCompared > 0)
00270     TotalImagesWithAnglesCompared++;
00271 
00272         /* write out labeled image files, if requested */
00273   if (OutputImages == 1)
00274     {
00275     printf("Writing MS labeled compare image...\n");
00276     fflush(stdout);
00277     sprintf(text,"%s.ms-lab",FilePrefix);
00278     OutputImage(text,MSImage,TotalRegionsInMS,MSRegionClassifications);
00279     printf("Writing GT labeled compare image...\n");
00280     fflush(stdout);
00281     sprintf(text,"%s.gt-lab",FilePrefix);
00282     OutputImage(text,GTImage,TotalRegionsInGT,GTRegionClassifications);
00283     }
00284   }
00285 
00286         /* print averages across image sequence (if it was a sequence) */
00287 if (StartImage != EndImage)
00288   {
00289   if (SequenceTotal == 0)
00290     printf("\n**** AVERAGES %s.%d ... %s.%d\n",Filename,StartImage,
00291         Filename,EndImage);
00292   else
00293     {
00294     printf("\n**** AVERAGES %s.%d",Filename,Sequence[0]);
00295     for (j=1; j<SequenceTotal; j++)
00296       printf(",%d",Sequence[j]);
00297     printf("\n");
00298     }
00299   printf(" GT reg: %f   Correct:  %f",
00300         (float)PerImageGTRegions/(float)TotalImagesCompared,
00301         (float)PerImageCorrect/(float)TotalImagesCompared);
00302   if(NoNormals)printf("\n");
00303   else printf("   Angle:  %f   StdDev:  %f\n",
00304         (float)PerImageAngDiff/(float)TotalImagesWithAnglesCompared,
00305         (float)PerImageStdDev/(float)TotalImagesWithAnglesCompared);
00306   printf(" Overseg: %f  Underseg: %f  Missed: %f  Noise: %f\n",
00307         (float)PerImageOverseg/(float)TotalImagesCompared,
00308         (float)PerImageUnderseg/(float)TotalImagesCompared,
00309         (float)PerImageMissed/(float)TotalImagesCompared,
00310         (float)PerImageNoise/(float)TotalImagesCompared);
00311   printf(" Correct pixels: %f  GT pixels: %f  Percent: %f\n",
00312         (double)PerImageCorrectPixels/(double)TotalImagesCompared,
00313         (double)PerImageGTPixels/(double)TotalImagesCompared,
00314         ((double)PerImageCorrectPixels/(double)PerImageGTPixels)*100);
00315   printf(" Missed Pixels in GT: %f  Not Correct Pixels in MS: %f\n",
00316         ((double)(PerImageGTPixels - PerImageCorrectPixels))/(double)TotalImagesCompared,
00317         (double)PerImageNotCorrectMSPixels/(double)TotalImagesCompared);
00318   }
00319 
00320 }
00321 
00322 
00323 
00324 
00325 
00326 
00327 void ComputeTables(GTImage, MSImage, OverlapTable, GTRegionSizes,
00328         MSRegionSizes, TotalRegionsInGT, TotalRegionsInMS)
00329 
00330 unsigned char   *GTImage, *MSImage;
00331 int             OverlapTable[MAX_LABELS][MAX_LABELS];
00332 int             GTRegionSizes[MAX_LABELS],MSRegionSizes[MAX_LABELS];
00333 int             TotalRegionsInGT,TotalRegionsInMS;
00334 
00335 {
00336 int     r,c,i;
00337 
00338         /* Initialize all table entries to 0 (pixels) */
00339 for (r=0; r<MAX_LABELS; r++)
00340   {
00341   for (c=0; c<MAX_LABELS; c++)
00342     OverlapTable[r][c]=0;
00343   GTRegionSizes[r]=MSRegionSizes[r]=0;
00344   }
00345         /* Scan through images and simply count */
00346 for (i=0; i<ROWS*COLS; i++)
00347   {
00348   GTRegionSizes[GTImage[i]]++;
00349   MSRegionSizes[MSImage[i]]++;
00350   OverlapTable[MSImage[i]][GTImage[i]]++;
00351   }
00352         /* Done */
00353 }
00354 
00355 
00356 
00357 
00358 
00359 
00360 void ClassifyRegions(OverlapTable,GTRegionSizes,MSRegionSizes,
00361         TotalRegionsInGT,TotalRegionsInMS,
00362         GTRegionClassifications,GTRegionMappings,GTMappingMeasures,
00363         MSRegionClassifications,MSRegionMappings,MSMappingMeasures,
00364         TotalCorrectPixelCount, TotalGTPixelCount, TotalNotCorrectMSPixels)
00365 
00366 int     OverlapTable[MAX_LABELS][MAX_LABELS];   /* overlap of regions' pixels;
00367                                                 ** first index is MS label,
00368                                                 ** second index is GT label. */
00369 int     GTRegionSizes[MAX_LABELS];              /* size of GT regions (pixels)*/
00370 int     MSRegionSizes[MAX_LABELS];              /* size of MS regions (pixels)*/
00371 int     TotalRegionsInGT,TotalRegionsInMS;
00372 char    GTRegionClassifications[MAX_LABELS];    /* type of mapping for region */
00373 int     GTRegionMappings[MAX_LABELS];           /* if MS side of mapping is
00374                                                 ** only 1 region, then which
00375                                                 ** MS region it maps to */
00376 double  GTMappingMeasures[MAX_LABELS][2];       /* _TOTAL_ metric measure for
00377                                                 ** mappings, indexed by GT
00378                                                 ** region labels; first index
00379                                                 ** is GT side of mapping,
00380                                                 ** second is MS side. */
00381 char    MSRegionClassifications[MAX_LABELS];    /* type of mapping for region */
00382 int     MSRegionMappings[MAX_LABELS];           /* if GT side of mapping is
00383                                                 ** only 1 region, then which
00384                                                 ** GT region it maps to */
00385 double  MSMappingMeasures[MAX_LABELS][2];       /* _TOTAL_ metric measure for
00386                                                 ** mappings, indexed by MS
00387                                                 ** region labels; first index
00388                                                 ** is GT side of mapping,
00389                                                 ** second is MS side. */
00390 int             *TotalCorrectPixelCount;
00391 int             *TotalGTPixelCount;
00392 int             *TotalNotCorrectMSPixels;
00393 
00394 {
00395 int     m,n,HowManyInMapping,TotalMSArea,TotalGTArea;
00396 int     TotalOverlap,NewMappingIsBetter;
00397 double  Measure1,Measure2,SoloMeasure;
00398 
00399 *TotalCorrectPixelCount=*TotalGTPixelCount=*TotalNotCorrectMSPixels=0;
00400 
00401         /* Scan through table and find all CORRECT_DETECTION mappings */
00402 
00403 for (n=10; (n-10)<TotalRegionsInGT; n++)
00404   {
00405   (*TotalGTPixelCount)+=GTRegionSizes[n];
00406   for (m=10; (m-10)<TotalRegionsInMS; m++)
00407     {
00408     Measure1=(double)OverlapTable[m][n]/(double)GTRegionSizes[n];
00409     Measure2=(double)OverlapTable[m][n]/(double)MSRegionSizes[m];
00410     if (Measure1 >= T_SEG  &&  Measure2 >= T_SEG)
00411       {
00412       GTRegionClassifications[n]=MSRegionClassifications[m]='c';
00413       GTRegionMappings[n]=m;
00414       MSRegionMappings[m]=n;
00415       GTMappingMeasures[n][0]=MSMappingMeasures[m][0]=Measure1;
00416       GTMappingMeasures[n][1]=MSMappingMeasures[m][1]=Measure2;
00417       (*TotalCorrectPixelCount)+=OverlapTable[m][n];
00418       (*TotalNotCorrectMSPixels)+=MSRegionSizes[m]-OverlapTable[m][n];
00419       }
00420     }
00421   }
00422 
00423         /* Now scan through table looking for OVER-SEGMENTATION mappings.
00424         ** If one contradicts an already-found mapping, then take the
00425         ** mapping with the highest measure. */
00426 
00427 for (n=10; (n-10)<TotalRegionsInGT; n++)
00428   {
00429   HowManyInMapping=0;
00430   TotalOverlap=TotalMSArea=0;
00431   for (m=10; (m-10)<TotalRegionsInMS; m++)
00432     {
00433     SoloMeasure=(double)OverlapTable[m][n]/(double)MSRegionSizes[m];
00434     if (SoloMeasure >= T_SEG)
00435       {
00436       HowManyInMapping++;
00437       TotalMSArea+=MSRegionSizes[m];
00438       TotalOverlap+=OverlapTable[m][n];
00439       MSRegionMappings[m]=n;
00440       }
00441     }
00442   if (HowManyInMapping > 1)
00443     {
00444     Measure1=(double)TotalOverlap/(double)GTRegionSizes[n];
00445     Measure2=(double)TotalOverlap/(double)TotalMSArea;
00446     if (Measure1 >= T_SEG  &&  Measure2 >= T_SEG)
00447       {
00448       NewMappingIsBetter=1;
00449       if (GTRegionClassifications[n] != ' ')
00450                 /* Has a classification already.  See if this one is higher. */
00451         if ((Measure1+Measure2)/2.0 <= (GTMappingMeasures[n][0]+
00452                 GTMappingMeasures[n][1])/2.0)
00453           NewMappingIsBetter=0;
00454       if (NewMappingIsBetter)
00455         {
00456         GTRegionClassifications[n]='o';
00457         GTMappingMeasures[n][0]=Measure1;
00458         GTMappingMeasures[n][1]=Measure2;
00459         for (m=10; (m-10)<TotalRegionsInMS; m++)
00460           if (MSRegionMappings[m] == n)
00461             {
00462             MSRegionClassifications[m]='o';
00463             MSMappingMeasures[m][0]=Measure1;
00464             MSMappingMeasures[m][1]=Measure2;
00465             }
00466         }
00467       }
00468     }
00469   }
00470 
00471         /* Now scan through table looking for UNDER-SEGMENTATION mappings.
00472         ** Again, if one contradicts an already-found mapping, then take the
00473         ** mapping with the highest measure. */
00474 
00475 for (m=10; (m-10)<TotalRegionsInMS; m++)
00476   {
00477   TotalNotCorrectMSPixels+=MSRegionSizes[m];
00478   HowManyInMapping=0;
00479   TotalOverlap=TotalGTArea=0;
00480   for (n=10; (n-10)<TotalRegionsInGT; n++)
00481     {
00482     SoloMeasure=(double)OverlapTable[m][n]/(double)GTRegionSizes[n];
00483     if (SoloMeasure >= T_SEG)
00484       {
00485       HowManyInMapping++;
00486       TotalGTArea+=GTRegionSizes[n];
00487       TotalOverlap+=OverlapTable[m][n];
00488       GTRegionMappings[n]=m;
00489       }
00490     }
00491   if (HowManyInMapping > 1)
00492     {
00493     Measure1=(double)TotalOverlap/(double)TotalGTArea;
00494     Measure2=(double)TotalOverlap/(double)MSRegionSizes[m];
00495     if (Measure1 >= T_SEG  &&  Measure2 >= T_SEG)
00496       {
00497       NewMappingIsBetter=1;
00498       if (MSRegionClassifications[m] != ' ')
00499                 /* Has a classification already.  See if this one is higher. */
00500         if ((Measure1+Measure2)/2.0 <= (MSMappingMeasures[m][0]+
00501                 MSMappingMeasures[m][1])/2.0)
00502           NewMappingIsBetter=0;
00503       if (NewMappingIsBetter)
00504         {
00505         MSRegionClassifications[m]='u';
00506         MSMappingMeasures[m][0]=Measure1;
00507         MSMappingMeasures[m][1]=Measure2;
00508         for (n=10; (n-10)<TotalRegionsInGT; n++)
00509           if (GTRegionMappings[n] == m)
00510             {
00511             GTRegionClassifications[n]='u';
00512             GTMappingMeasures[n][0]=Measure1;
00513             GTMappingMeasures[n][1]=Measure2;
00514             }
00515         }
00516       }
00517     }
00518   }
00519 
00520         /* Any GT regions not participating in mappings are classified
00521         ** as MISSED. */
00522 
00523 for (n=10; (n-10)<TotalRegionsInGT; n++)
00524   if (GTRegionClassifications[n] == ' ')
00525     GTRegionClassifications[n]='m';
00526 
00527         /* Any MS regions not participating in mappings are classified
00528         ** as NOISE. */
00529 
00530 for (m=10; (m-10)<TotalRegionsInMS; m++)
00531   if (MSRegionClassifications[m] == ' ')
00532     MSRegionClassifications[m]='n';
00533 
00534         /* Done */
00535 }
00536 
00537 
00538 
00539 
00540 
00541 
00542 double  AngleBetweenVectors(Vector1,Vector2)
00543 
00544 double  Vector1[3],Vector2[3];
00545 
00546 {
00547 double  length1,length2,dotproduct,arc,Angle;
00548 
00549 dotproduct=Vector1[0]*Vector2[0]+Vector1[1]*Vector2[1]+Vector1[2]*Vector2[2];
00550 length1=sqrt(pow(Vector1[0],2.0)+pow(Vector1[1],2.0)+pow(Vector1[2],2.0));
00551 length2=sqrt(pow(Vector2[0],2.0)+pow(Vector2[1],2.0)+pow(Vector2[2],2.0));
00552 arc=dotproduct/(length1*length2);
00553 if (fabs(arc) < 1.0)
00554   Angle=acos(fabs(arc));
00555 else
00556 if (arc >= 1.0)
00557   Angle=0.0;
00558 else
00559   Angle=M_PI;
00560 Angle *= (180.0/M_PI);
00561 return(Angle);
00562 }
00563 
00564 
00565 
00566 
00567 
00568 void OutputTable(FilePrefix,TotalRegionsInGT,TotalRegionsInMS,
00569         TotalAnglesInGT,GTAngleRegions,GTAngleValues,MSRegionNormals,
00570         GTRegionClassifications,GTRegionMappings,GTMappingMeasures,
00571         MSRegionClassifications,MSRegionMappings,MSMappingMeasures,
00572         PrintFile,
00573         TotalCorrectClassifications,C_Avg_m1,C_Avg_m2,
00574         TotalAnglesCompared,AvgAngDiff,stddev,
00575         TotalOversegClassifications,O_Avg_m1,O_Avg_m2,
00576         TotalUndersegClassifications,U_Avg_m1,U_Avg_m2,
00577         TotalMissedClassifications,TotalNoiseClassifications, NoNormals)
00578 
00579 char            FilePrefix[];
00580 int             TotalRegionsInGT,TotalRegionsInMS,TotalAnglesInGT;
00581 int             GTAngleRegions[MAX_ANGLES][2];
00582 double          GTAngleValues[MAX_ANGLES];
00583 double          MSRegionNormals[MAX_LABELS][3];
00584 char            GTRegionClassifications[MAX_LABELS];
00585 char            MSRegionClassifications[MAX_LABELS];
00586 int             GTRegionMappings[MAX_LABELS],MSRegionMappings[MAX_LABELS];
00587 double          GTMappingMeasures[MAX_LABELS][2];
00588 double          MSMappingMeasures[MAX_LABELS][2];
00589 int             PrintFile;
00590 int             *TotalCorrectClassifications,*TotalOversegClassifications;
00591 int             *TotalUndersegClassifications;
00592 int             *TotalMissedClassifications,*TotalNoiseClassifications;
00593 int             *TotalAnglesCompared;
00594 double          *C_Avg_m1,*C_Avg_m2,*O_Avg_m1,*O_Avg_m2,*U_Avg_m1,*U_Avg_m2;
00595 double          *AvgAngDiff,*stddev;
00596 int             *NoNormals;
00597 
00598 {
00599 char    text[500],buffer[30],line[81],temp[30];
00600 FILE    *fpt;
00601 int     i,j,k,a,LabelCount;
00602 double  SumOfAngles,AngleCompared[MAX_ANGLES],sigma;
00603 
00604 *TotalCorrectClassifications=*TotalOversegClassifications=0;
00605 *TotalUndersegClassifications=*TotalMissedClassifications=0;
00606 *TotalNoiseClassifications=*TotalAnglesCompared=0;
00607 *C_Avg_m1=*C_Avg_m2=*O_Avg_m1=*O_Avg_m2=*U_Avg_m1=*U_Avg_m2=0.0;
00608 *AvgAngDiff=*stddev=0.0;
00609 fpt=0;
00610 
00611 if (PrintFile)
00612   {
00613   strcpy(text,FilePrefix);
00614   strcat(text,".compared");
00615   if ((fpt=fopen(text,"w")) == NULL)
00616     {
00617     fprintf(stderr,"Couldn't open %s for writing.\n",text);
00618     exit(1);
00619     }
00620   printf("Writing results file...\n");
00621   fflush(stdout);
00622         /* Header */
00623   sprintf(buffer,"GT Region(s)");
00624   strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00625   strcpy(line,buffer);
00626   sprintf(buffer,"Category");
00627   strncat(buffer,BLANKS,FW_CLASSIFS-strlen(buffer));
00628   strcat(line,buffer);
00629   sprintf(buffer,"MS Region(s)");
00630   strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00631   strcat(line,buffer);
00632   sprintf(buffer,"Measure1");
00633   strncat(buffer,BLANKS,FW_MEASURES-strlen(buffer));
00634   strcat(line,buffer);
00635   sprintf(buffer,"Measure2");
00636   strncat(buffer,BLANKS,FW_MEASURES-strlen(buffer));
00637   strcat(line,buffer);
00638   fprintf(fpt,"%s\n",line);
00639   strcpy(line,"=");
00640   for (i=1; i<(FW_LABELS*2+FW_MEASURES*2+FW_CLASSIFS); i++)
00641     strcat(line,"=");
00642   fprintf(fpt,"%s\n",line);
00643   }
00644 
00645         /* Print out all correct detection mappings together */
00646 *TotalAnglesCompared = *TotalCorrectClassifications = 0;
00647 *C_Avg_m1 = *C_Avg_m2 = 0.0;
00648 SumOfAngles=0.0;
00649 for (i=10; (i-10)<TotalRegionsInGT; i++)
00650   {
00651   if(GTRegionClassifications[i] == 'c')
00652     {
00653     sprintf(buffer,"%d",i);
00654     strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00655     strcpy(line,buffer);
00656     sprintf(buffer,"correct");
00657     strncat(buffer,BLANKS,FW_CLASSIFS-strlen(buffer));
00658     strcat(line,buffer);
00659     sprintf(buffer,"%d",GTRegionMappings[i]);
00660     strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00661     strcat(line,buffer);
00662     sprintf(buffer,"%1.3lf",GTMappingMeasures[i][0]);
00663     strncat(buffer,BLANKS,FW_MEASURES-strlen(buffer));
00664     strcat(line,buffer);
00665     sprintf(buffer,"%1.3lf",GTMappingMeasures[i][1]);
00666     strncat(buffer,BLANKS,FW_MEASURES-strlen(buffer));
00667     strcat(line,buffer);
00668     if (PrintFile)
00669       fprintf(fpt,"%s\n",line);
00670     (*C_Avg_m1) += GTMappingMeasures[i][0];
00671     (*C_Avg_m2) += GTMappingMeasures[i][1];
00672     (*TotalCorrectClassifications)++;
00673     if(!(*NoNormals))
00674     {
00675     for (a=0; a<TotalAnglesInGT; a++)
00676       {
00677       if (GTAngleRegions[a][0] == i)
00678         {
00679         if (GTRegionClassifications[GTAngleRegions[a][1]] == 'c')
00680           {
00681           if(GTAngleValues[a] > 90)
00682                 GTAngleValues[a]=fabs(GTAngleValues[a] - 180);
00683           AngleCompared[*TotalAnglesCompared]=
00684                 fabs(GTAngleValues[a]-AngleBetweenVectors(
00685                 MSRegionNormals[GTRegionMappings[GTAngleRegions[a][0]]],
00686                 MSRegionNormals[GTRegionMappings[GTAngleRegions[a][1]]]));
00687           SumOfAngles+=AngleCompared[*TotalAnglesCompared];
00688           if(AngleCompared[*TotalAnglesCompared] > 15.0){
00689             printf("compared angle between gtregions %d and %d is greater than 15 degrees: %lf\n",
00690                 GTAngleRegions[a][0], GTAngleRegions[a][1], AngleCompared[*TotalAnglesCompared]);
00691             printf("angle should be %lf but has value %lf\n", GTAngleValues[a], AngleBetweenVectors(
00692                 MSRegionNormals[GTRegionMappings[GTAngleRegions[a][0]]],
00693                 MSRegionNormals[GTRegionMappings[GTAngleRegions[a][1]]]));
00694             printf("normals are: (%lf, %lf, %lf)\n", MSRegionNormals[GTRegionMappings[GTAngleRegions[a][0]]][0],
00695                         MSRegionNormals[GTRegionMappings[GTAngleRegions[a][0]]][1],
00696                         MSRegionNormals[GTRegionMappings[GTAngleRegions[a][0]]][2]);
00697             printf("and: (%lf, %lf, %lf)\n", MSRegionNormals[GTRegionMappings[GTAngleRegions[a][1]]][0],
00698                         MSRegionNormals[GTRegionMappings[GTAngleRegions[a][1]]][1],
00699                         MSRegionNormals[GTRegionMappings[GTAngleRegions[a][1]]][2]);
00700           }
00701           (*TotalAnglesCompared)++;
00702           }
00703         }
00704       }
00705     }
00706     }
00707   }
00708 if(*TotalCorrectClassifications > 0)
00709   {
00710   (*C_Avg_m1) /= (double)(*TotalCorrectClassifications);
00711   (*C_Avg_m2) /= (double)(*TotalCorrectClassifications);
00712   }
00713 
00714         /* Get average and standard deviation of angular differences */
00715 if(!(*NoNormals) && *TotalAnglesCompared > 0)
00716   {
00717   *AvgAngDiff=SumOfAngles/(double)(*TotalAnglesCompared);
00718   sigma=0.0;
00719   for(i=0; i<*TotalAnglesCompared; i++)
00720     sigma += pow((AngleCompared[i]-(*AvgAngDiff)),2.0);
00721   *stddev = sqrt(sigma/(*TotalAnglesCompared));
00722   }
00723 
00724         /* Print out all over-segmentation mappings together */
00725 *TotalOversegClassifications = 0;
00726 *O_Avg_m1 = *O_Avg_m2 = 0.0;
00727 for (i=10; (i-10)<TotalRegionsInGT; i++)
00728   {
00729   if(GTRegionClassifications[i] == 'o')
00730     {
00731     sprintf(buffer,"%d",i);
00732     strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00733     strcpy(line,buffer);
00734     sprintf(buffer,"over-seg");
00735     strncat(buffer,BLANKS,FW_CLASSIFS-strlen(buffer));
00736     strcat(line,buffer);
00737     LabelCount=0;
00738     for (j=10; j<TotalRegionsInMS+10; j++)
00739       {
00740       if(MSRegionMappings[j] == i)
00741         {
00742         if (LabelCount == 0)
00743           sprintf(buffer,"%d",j);
00744         else
00745           {
00746           sprintf(temp,",%d",j);
00747           if (strlen(buffer)+strlen(temp) > FW_LABELS-1)
00748             {
00749             strcat(buffer,",");
00750             break;
00751             }
00752           strcat(buffer,temp);
00753           }
00754         LabelCount++;
00755         }
00756       }
00757     strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00758     strcat(line,buffer);
00759     sprintf(buffer,"%1.3lf",GTMappingMeasures[i][0]);
00760     strncat(buffer,BLANKS,FW_MEASURES-strlen(buffer));
00761     strcat(line,buffer);
00762     sprintf(buffer,"%1.3lf",GTMappingMeasures[i][1]);
00763     strncat(buffer,BLANKS,FW_MEASURES-strlen(buffer));
00764     strcat(line,buffer);
00765     if (PrintFile)
00766       fprintf(fpt,"%s\n",line);
00767     (*O_Avg_m1) += GTMappingMeasures[i][0];
00768     (*O_Avg_m2) += GTMappingMeasures[i][1];
00769     (*TotalOversegClassifications)++;
00770     while (j != TotalRegionsInMS+10)
00771       {
00772       LabelCount=0;
00773       for (k=j; k<TotalRegionsInMS+10; k++)
00774         {
00775         if(MSRegionMappings[k] == i)
00776           {
00777           if (LabelCount == 0)
00778             sprintf(buffer,"%d",k);
00779           else
00780             {
00781             sprintf(temp,",%d",k);
00782             if (strlen(buffer)+strlen(temp) > FW_LABELS-1)
00783               break;
00784             strcat(buffer,temp);
00785             }
00786           LabelCount++;
00787           }
00788         }
00789       strncpy(line,BLANKS,FW_LABELS);
00790       line[FW_LABELS]='\0';
00791       strncat(line,BLANKS,FW_CLASSIFS);
00792       strcat(line,buffer);
00793       if (PrintFile)
00794         fprintf(fpt,"%s\n",line);
00795       j=k;
00796       }
00797     }  
00798   }
00799 if(*TotalOversegClassifications > 0)
00800   {
00801   (*O_Avg_m1) /= (double)(*TotalOversegClassifications);
00802   (*O_Avg_m2) /= (double)(*TotalOversegClassifications);
00803   }
00804 
00805 
00806         /* Print out all under-segmentation mappings together */
00807 *TotalUndersegClassifications = 0;
00808 *U_Avg_m1 = *U_Avg_m2 = 0.0;
00809 for (i=10; (i-10)<TotalRegionsInMS; i++)
00810   {
00811   if(MSRegionClassifications[i] == 'u')
00812     {
00813     LabelCount=0;
00814     for (j=10; j<TotalRegionsInGT+10; j++)
00815       {
00816       if(GTRegionMappings[j] == i)
00817         {
00818         if (LabelCount == 0)
00819           sprintf(buffer,"%d",j);
00820         else
00821           {
00822           sprintf(temp,",%d",j);
00823           if (strlen(buffer)+strlen(temp) > FW_LABELS-1)
00824             {
00825             strcat(buffer,",");
00826             break;
00827             }
00828           strcat(buffer,temp);
00829           }
00830         LabelCount++;
00831         }
00832       }
00833     strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00834     strcpy(line,buffer);
00835     sprintf(buffer,"under-seg");
00836     strncat(buffer,BLANKS,FW_CLASSIFS-strlen(buffer));
00837     strcat(line,buffer);
00838     sprintf(buffer,"%d",i);
00839     strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00840     strcat(line,buffer);
00841     sprintf(buffer,"%1.3lf",MSMappingMeasures[i][0]);
00842     strncat(buffer,BLANKS,FW_MEASURES-strlen(buffer));
00843     strcat(line,buffer);
00844     sprintf(buffer,"%1.3lf",MSMappingMeasures[i][1]);
00845     strncat(buffer,BLANKS,FW_MEASURES-strlen(buffer));
00846     strcat(line,buffer);
00847     if (PrintFile)
00848       fprintf(fpt,"%s\n",line);
00849     (*U_Avg_m1) += MSMappingMeasures[i][0];
00850     (*U_Avg_m2) += MSMappingMeasures[i][1];
00851     (*TotalUndersegClassifications)++;
00852     while (j != TotalRegionsInGT+10)
00853       {
00854       LabelCount=0;
00855       for (k=j; k<TotalRegionsInGT+10; k++)
00856         {
00857         if(GTRegionMappings[k] == i)
00858           {
00859           if (LabelCount == 0)
00860             sprintf(buffer,"%d",k);
00861           else
00862             {
00863             sprintf(temp,",%d",k);
00864             if (strlen(buffer)+strlen(temp) > FW_LABELS-1)
00865               break;
00866             strcat(buffer,temp);
00867             }
00868           LabelCount++;
00869           }
00870         }
00871       if (PrintFile)
00872         fprintf(fpt,"%s\n",buffer);
00873       j=k;
00874       }
00875     }  
00876   }
00877 if(*TotalUndersegClassifications > 0)
00878   {
00879   (*U_Avg_m1) /= (double)(*TotalUndersegClassifications);
00880   (*U_Avg_m2) /= (double)(*TotalUndersegClassifications);
00881   }
00882 
00883 
00884         /*  Print out all missed regions together  */
00885 *TotalMissedClassifications=0;
00886 for (i=10; (i-10)<TotalRegionsInGT; ++i)
00887   {
00888   if(GTRegionClassifications[i] == 'm')
00889     {
00890     sprintf(buffer,"%d",i);
00891     strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00892     strcpy(line,buffer);
00893     sprintf(buffer,"missed");
00894     strncat(buffer,BLANKS,FW_CLASSIFS-strlen(buffer));
00895     strcat(line,buffer);
00896     if (PrintFile)
00897       fprintf(fpt,"%s\n",line);
00898     (*TotalMissedClassifications)++;
00899     }
00900   }
00901 
00902         /*  Print out all noise regions together  */
00903 *TotalNoiseClassifications=0;
00904 for (i=10; (i-10)<TotalRegionsInMS; ++i)
00905   {
00906   if(MSRegionClassifications[i] == 'n')
00907     {
00908     strncpy(line,BLANKS,FW_LABELS);
00909     line[FW_LABELS]='\0';
00910     sprintf(buffer,"noise");
00911     strncat(buffer,BLANKS,FW_CLASSIFS-strlen(buffer));
00912     strcat(line,buffer);
00913     sprintf(buffer,"%d",i);
00914     strncat(buffer,BLANKS,FW_LABELS-strlen(buffer));
00915     strcat(line,buffer);
00916     if (PrintFile)
00917       fprintf(fpt,"%s\n",line);
00918     (*TotalNoiseClassifications)++;
00919     }
00920   }
00921 
00922 if (!PrintFile)
00923   return;
00924 
00925         /* Print out all totals, averages and such */
00926 fprintf(fpt,"\n\nTotal regions in Ground Truth:  %d\n",TotalRegionsInGT);
00927 fprintf(fpt,"Total regions in Machine Segmentation:  %d\n",TotalRegionsInMS);
00928 
00929 fprintf(fpt,"\n\nTotal CORRECT DETECTION classifications:  %d\n",
00930                 *TotalCorrectClassifications);
00931 if(TotalCorrectClassifications > 0)
00932   {
00933   fprintf(fpt,"\tAverage measure1:  %lf\n",*C_Avg_m1);
00934   fprintf(fpt,"\tAverage measure2:  %lf\n",*C_Avg_m2);
00935   }
00936 else
00937   {
00938   fprintf(fpt,"\tAverage measure1:  Not applicable\n");
00939   fprintf(fpt,"\tAverage measure2:  Not applicable\n");
00940   }
00941 fprintf(fpt,"\n");
00942 fprintf(fpt,"\tTotal angles recorded in ground truth:  %d\n",TotalAnglesInGT);
00943 fprintf(fpt,"\tTotal angles compared:  %d\n",*TotalAnglesCompared);
00944 if(*TotalAnglesCompared > 0)
00945   {
00946   fprintf(fpt,"\tAverage error in compared angles (degrees) :  %lf\n",
00947                 *AvgAngDiff);
00948   fprintf(fpt,"\tStandard deviation:  %lf\n\n",*stddev);
00949   }
00950 else
00951   {
00952   fprintf(fpt,"\tAverage error in compared angles (degrees) :  Not applicable\n");
00953   fprintf(fpt,"\tStandard deviation:  Not applicable\n");
00954   }
00955 
00956 fprintf(fpt,"\n\nTotal OVER-SEGMENTATION classifications:  %d\n",
00957                 *TotalOversegClassifications);
00958 if(*TotalOversegClassifications > 0)
00959   {
00960   fprintf(fpt,"\tAverage measure1:  %lf\n",*O_Avg_m1);
00961   fprintf(fpt,"\tAverage measure2:  %lf\n",*O_Avg_m2);
00962   }
00963 else
00964   {
00965   fprintf(fpt,"\tAverage measure1:  Not applicable\n");
00966   fprintf(fpt,"\tAverage measure2:  Not applicable\n");
00967   }
00968 
00969 fprintf(fpt,"\n\nTotal UNDER-SEGMENTATION classifications:  %d\n",
00970                 *TotalUndersegClassifications);
00971 if(*TotalUndersegClassifications > 0)
00972   {
00973   fprintf(fpt,"\tAverage measure1:  %lf\n",*U_Avg_m1);
00974   fprintf(fpt,"\tAverage measure2:  %lf\n",*U_Avg_m2);
00975   }
00976 else
00977   {
00978   fprintf(fpt,"\tAverage measure1:  Not applicable\n");
00979   fprintf(fpt,"\tAverage measure2:  Not applicable\n");
00980   }
00981 
00982 fprintf(fpt,"\n\nTotal MISSED classifications:  %d\n",
00983                 *TotalMissedClassifications);
00984 fprintf(fpt,"\n\nTotal NOISE classifications:  %d\n",
00985                 *TotalNoiseClassifications);
00986 fclose(fpt);
00987 
00988         /* DONE! */
00989 }
00990 
00991 
00992 
00993 
00994 int LoadFiles(FilePrefix,ROWS,COLS,GTImage,TotalRegionsInGT,
00995         TotalAnglesInGT,GTAngleRegions,GTAngleValues,
00996         MSImage,TotalRegionsInMS,MSRegionNormals, NoNormals)
00997 
00998 char            FilePrefix[];
00999 int             *ROWS,*COLS;
01000 unsigned char   **GTImage,**MSImage;
01001 int             *TotalRegionsInGT,*TotalRegionsInMS;
01002 int             *TotalAnglesInGT;
01003 
01004 int             GTAngleRegions[MAX_ANGLES][2];
01005 double          GTAngleValues[MAX_ANGLES];
01006 double          MSRegionNormals[MAX_LABELS][3];
01007 int             *NoNormals;
01008 
01009 {
01010 FILE            *fpt;
01011 char            text[180];
01012 int             RasterHeader[8];
01013 int             i,a;
01014 unsigned int j;
01015 int             GTIndex[MAX_LABELS],MSIndex[MAX_LABELS];
01016 
01017         /* Read in ground truth segmentation */
01018 i=0;
01019 while(PATHS[i])
01020   {
01021   strcpy(text, PATHS[i]);
01022   strcat(text, FilePrefix);
01023   strcat(text,".gt-seg");
01024   if ((fpt=fopen(text,"r")) != NULL)
01025     break;
01026   i++;
01027   }
01028 if (PATHS[i] == NULL)
01029   {
01030   fprintf(stderr,"Couldn't open %s.gt-seg\n",FilePrefix);
01031   return(0);
01032   }
01033 fread(RasterHeader,sizeof(int),8,fpt);
01034 if (RasterHeader[0] != RAS_MAGIC)
01035 {
01036         if (ntohl(RasterHeader[0]) != RAS_MAGIC){
01037                 printf("Wrong file format for ground truth image (must be rasterfile).\n");
01038                 exit(1);
01039         }
01040         for(j=0; j<8; ++j){
01041                 RasterHeader[j] = ntohl(RasterHeader[j]);
01042         }
01043 }
01044 if (RasterHeader[7] != 0)
01045 {
01046         fprintf(stderr,"Color format of sunraster file not supported.\n");
01047         exit(1);
01048 }
01049 *ROWS=RasterHeader[2];
01050 *COLS=RasterHeader[1];
01051 if ((*GTImage=(unsigned char *)calloc((*ROWS)*(*COLS), sizeof(unsigned char))) == NULL)
01052 {
01053         fprintf(stderr,"Could not allocate space for array GTImage.\n");
01054         fprintf(stderr,"Program terminating!\n");
01055         exit(1);
01056 }
01057 fread(*GTImage,sizeof(unsigned char),(*ROWS)*(*COLS),fpt);
01058 fclose(fpt);
01059 
01060 if(!(*NoNormals)){
01061         /* Read in ground truth angles file */
01062 i=0;
01063 while(PATHS[i])
01064   {
01065   strcpy(text, PATHS[i]);
01066   strcat(text, FilePrefix);
01067   strcat(text,".gt-ang");
01068   if ((fpt=fopen(text,"r")) != NULL)
01069     break;
01070   i++;
01071   }
01072 if (PATHS[i] == NULL)
01073   {
01074   fprintf(stderr,"Couldn't open %s.gt-ang\n",FilePrefix);
01075   return(0);
01076   }
01077 fscanf(fpt,"%d",TotalAnglesInGT);
01078 for (i=0; i<*TotalAnglesInGT; i++)
01079   {
01080   if (fscanf(fpt,"%d %d %lf",&GTAngleRegions[i][0],&GTAngleRegions[i][1],
01081                              &GTAngleValues[i]) != 3)
01082     {
01083     fprintf(stderr,"Wrong number of angles in %s.\n",text);
01084     exit(1);
01085     }
01086   }
01087 fclose(fpt);
01088 }
01089         /* Read in machine segmentation */
01090 i=0;
01091 while(PATHS[i])
01092   {
01093   strcpy(text, PATHS[i]);
01094   strcat(text, FilePrefix);
01095   strcat(text,".ms-seg");
01096   if ((fpt=fopen(text,"r")) != NULL)
01097     break;
01098   i++;
01099   }
01100 if (PATHS[i] == NULL)
01101   {
01102   fprintf(stderr,"Couldn't open %s.ms-seg\n",FilePrefix);
01103   return(0);
01104   }
01105 fread(RasterHeader,sizeof(int),8,fpt);
01106 if (RasterHeader[0] != RAS_MAGIC)
01107 {
01108         if (ntohl(RasterHeader[0]) != RAS_MAGIC){
01109                 printf("Wrong file format for ground truth image (must be rasterfile).\n");
01110                 exit(1);
01111         }
01112         for(j=0; j<8; ++j){
01113                 RasterHeader[j] = ntohl(RasterHeader[j]);
01114         }
01115 }
01116 if (RasterHeader[0] == RAS_MAGIC)
01117   {
01118   if (RasterHeader[7] != 0)
01119     {
01120     fprintf(stderr,"Color format of sunraster file not supported.\n");
01121     exit(1);
01122     }
01123   if (RasterHeader[1] != *COLS  ||  RasterHeader[2] != *ROWS)
01124     {
01125     fprintf(stderr,"Image size does not match that of the ground truth.\n");
01126     exit(1);
01127     }
01128   if ((*MSImage=(unsigned char *)calloc((*ROWS)*(*COLS), sizeof(unsigned char)))
01129         == NULL)
01130     {
01131     fprintf(stderr,"Could not allocate space for array MSImage.\n");
01132     fprintf(stderr,"Program terminating!\n");
01133     exit(1);
01134     }
01135   fread(*MSImage,sizeof(unsigned char),(*ROWS)*(*COLS),fpt);
01136   fclose(fpt);
01137   }
01138 else
01139   {
01140   printf("Wrong file format for machine segmentation (must be rasterfile).\n");
01141   exit(1);
01142   }
01143 
01144 if(!(*NoNormals)){
01145         /* Read in normals for machine segmentation image regions */
01146 i=0;
01147 
01148 while(PATHS[i])
01149   {
01150   strcpy(text, PATHS[i]);
01151   strcat(text, FilePrefix);
01152   strcat(text,".ms-nor");
01153   if ((fpt=fopen(text,"r")) != NULL)
01154     break;
01155   i++;
01156   }
01157 if (PATHS[i] == NULL)
01158   {
01159   fprintf(stderr,"Couldn't open %s.ms-nor\n",FilePrefix);
01160   return(0);
01161   }
01162 fscanf(fpt,"%d",TotalRegionsInMS);
01163 for (i=0; i<*TotalRegionsInMS; i++)
01164   {
01165   if (fscanf(fpt,"%d",&a) != 1)
01166     {
01167     fprintf(stderr,"Bad file format in %s.\n",text);
01168     exit(1);
01169     }
01170   if (fscanf(fpt,"%lf %lf %lf",&MSRegionNormals[a][0],
01171         &MSRegionNormals[a][1],&MSRegionNormals[a][2]) != 3)
01172     {
01173     fprintf(stderr,"Bad file format in %s.\n",text);
01174     exit(1);
01175     }
01176   if (MSRegionNormals[a][2] < 0.0)
01177     {
01178     MSRegionNormals[a][0]=-MSRegionNormals[a][0];
01179     MSRegionNormals[a][1]=-MSRegionNormals[a][1];
01180     MSRegionNormals[a][2]=-MSRegionNormals[a][2];
01181     }
01182   }
01183 fclose(fpt);
01184 }
01185 
01186         /* Although we already (supposedly) know TotalRegionsInMS,
01187         ** we'll compute both it and TotalRegionsInGT explicitly here.
01188         ** This is done by seeing how many consecutive labels there
01189         ** are in each image from 10 upwards.  At the same time, we'll
01190         ** do a little checking to make sure the images are proper.
01191         ** Boy, are we nice. */
01192 for (i=0; i<MAX_LABELS; i++)
01193   GTIndex[i]=MSIndex[i]=0;
01194 for (i=0; i<(*ROWS)*(*COLS); i++)
01195   GTIndex[(*GTImage)[i]]=MSIndex[(*MSImage)[i]]=1;
01196 for (i=0; i<10; i++)
01197   {
01198   if (GTIndex[i]  &&  !(NOISE_DEFINED[i]))
01199     {
01200     fprintf(stderr,"Ground truth image has badly defined pixel value %d\n",i);
01201     exit(1);
01202     }
01203   if (MSIndex[i]  &&  !(NOISE_DEFINED[i]))
01204     {
01205     fprintf(stderr,"Machine segmentation has badly defined pixel value %d\n",i);
01206     exit(1);
01207     }
01208   }
01209 *TotalRegionsInMS=*TotalRegionsInGT=0;
01210 for (i=10; i<MAX_LABELS; i++)
01211   {
01212   if (GTIndex[i])
01213     {
01214     (*TotalRegionsInGT)++;
01215     if ((i > 10  &&  !(GTIndex[i-1]))  ||  !(GTIndex[10]))
01216       {
01217       fprintf(stderr,"Ground truth region labels not consecutive\n");
01218       fprintf(stderr," from 10 upward.  %d is bad.\n",i);
01219       exit(1);
01220       }
01221     }
01222   if (MSIndex[i])
01223     {
01224     (*TotalRegionsInMS)++;
01225     if ((i > 10  &&  !(MSIndex[i-1]))  ||  !(MSIndex[10]))
01226       {
01227       fprintf(stderr,"Machine segmentation region labels not consecutive\n");
01228       fprintf(stderr," from 10 upward.  %d is bad.\n",i);
01229       exit(1);
01230       }
01231     }
01232   }
01233 if (*TotalRegionsInGT == 0)
01234   {
01235    fprintf(stderr,"Nothing but noise regions in ground truth.\n");
01236    exit(1);
01237   }
01238 if (*TotalRegionsInMS == 0)
01239   {
01240    fprintf(stderr,"Nothing but noise regions in machine segmentation.\n");
01241    exit(1);
01242   }
01243         /* DONE! */
01244 return(1);
01245 }
01246 
01247 
01248 
01249 
01250 
01251 
01252 char digits[10][5][4]={
01253 {
01254 "1111",
01255 "1  1",
01256 "1  1",
01257 "1  1",
01258 "1111",
01259 },{
01260 " 11 ",
01261 " 11 ",
01262 " 11 ",
01263 " 11 ",
01264 " 11 ",
01265 },{
01266 "1111",
01267 "   1",
01268 "1111",
01269 "1   ",
01270 "1111",
01271 },{
01272 "1111",
01273 "   1",
01274 "1111",
01275 "   1",
01276 "1111",
01277 },{
01278 "1  1",
01279 "1  1",
01280 "1111",
01281 "   1",
01282 "   1",
01283 },{
01284 "1111",
01285 "1   ",
01286 "1111",
01287 "   1",
01288 "1111",
01289 },{
01290 "1   ",
01291 "1   ",
01292 "1111",
01293 "1  1",
01294 "1111",
01295 },{
01296 "1111",
01297 "   1",
01298 "   1",
01299 "   1",
01300 "   1",
01301 },{
01302 "1111",
01303 "1  1",
01304 "1111",
01305 "1  1",
01306 "1111",
01307 },{
01308 "1111",
01309 "1  1",
01310 "1111",
01311 "   1",
01312 "   1",
01313 }};
01314 
01315 unsigned char color_numbers[24]={0,0,0,103,102,135,46,169,103,60,89,234,
01316                                 215,134,29,218,2,121,86,19,123,255,255,255};
01317 
01318 void OutputImage(Filename,InImage,TotalRegions,RegionClassifications)
01319 
01320 char            Filename[];
01321 unsigned char   *InImage;
01322 int             TotalRegions;
01323 char            RegionClassifications[MAX_LABELS];
01324 
01325 {
01326 unsigned char   *OutImage;
01327 FILE            *fpt;
01328 unsigned char   cmap[24];
01329 int             i,j,r,c,d,x,y,width,height,real_width,raster[8];
01330 int             CenterR[256],CenterC[256],TotalPixels[256];
01331 int             Label[5][14],mappit[8];
01332 
01333 if ((OutImage=(unsigned char *)calloc(ROWS*COLS,sizeof(unsigned char))) == NULL)
01334   {
01335   printf("Unable to allocate space for OutImage.\n");
01336   exit(1);
01337   }
01338 
01339 for (i=0; i<8; i++)
01340   mappit[i]=0;
01341 for (i=0; i<ROWS*COLS; i++)
01342   {
01343   if (InImage[i] < 10)
01344     mappit[0]=1;
01345   else if (RegionClassifications[InImage[i]] == 'c')
01346     mappit[1]=1;
01347   else if (RegionClassifications[InImage[i]] == 'o')
01348     mappit[2]=1;
01349   else if (RegionClassifications[InImage[i]] == 'u')
01350     mappit[3]=1;
01351   else if (RegionClassifications[InImage[i]] == 'n')
01352     mappit[4]=1;
01353   else if (RegionClassifications[InImage[i]] == 'm')
01354     mappit[5]=1;
01355   }
01356 mappit[6]=mappit[7]=1;
01357 for (i=7; i>=0; i--)
01358   if (mappit[i])
01359     for (j=i-1; j>=0; j--)
01360       mappit[i]+=mappit[j];
01361 
01362 for (i=10; i<10+TotalRegions; i++)
01363   CenterR[i]=CenterC[i]=TotalPixels[i]=0;
01364 for (r=0; r<ROWS; r++)
01365   for (c=0; c<COLS; c++)
01366     if (InImage[r*COLS+c] >= 10)
01367       {
01368       CenterR[InImage[r*COLS+c]]+=r;
01369       CenterC[InImage[r*COLS+c]]+=c;
01370       TotalPixels[InImage[r*COLS+c]]++;
01371       switch(RegionClassifications[InImage[r*COLS+c]])
01372         {
01373         case 'c':
01374           OutImage[r*COLS+c]=mappit[1]-1;
01375           break;
01376         case 'o':
01377           OutImage[r*COLS+c]=mappit[2]-1;
01378           break;
01379         case 'u':
01380           OutImage[r*COLS+c]=mappit[3]-1;
01381           break;
01382         case 'n':
01383           OutImage[r*COLS+c]=mappit[4]-1;
01384           break;
01385         case 'm':
01386           OutImage[r*COLS+c]=mappit[5]-1;
01387           break;
01388         default:        /* should never happen */
01389           OutImage[r*COLS+c]=mappit[6]-1;
01390           break;
01391         }
01392       if ((r > 0  &&  InImage[(r-1)*COLS+c] != InImage[r*COLS+c])  ||
01393           (r < ROWS-1  &&  InImage[(r+1)*COLS+c] != InImage[r*COLS+c])  ||
01394           (c > 0  &&  InImage[r*COLS+c-1] != InImage[r*COLS+c])  ||
01395           (c < COLS-1  &&  InImage[r*COLS+c+1] != InImage[r*COLS+c]))
01396         OutImage[r*COLS+c]=mappit[6]-1; /* border of region */
01397       }
01398     else
01399       OutImage[r*COLS+c]=mappit[0]-1;   /* unlabeled/noise pixel */
01400 
01401 for (r=0; r<5; r++)
01402   for (c=0; c<14; c++)
01403     Label[r][c]=0;
01404 for (i=10; i<TotalRegions+10; i++)
01405   {
01406   CenterR[i]/=TotalPixels[i];
01407   CenterC[i]/=TotalPixels[i];
01408   width=0;
01409   for (j=0; j<3; j++)
01410     {
01411     if (j == 0  &&  i < 100)
01412       continue;
01413     if (j == 0)
01414       d=i/100;
01415     else if (j == 1)
01416       d=(i-((i/100)*100))/10;
01417     else
01418       d=i-((i/10)*10);
01419     for (r=0; r<5; r++)
01420       for (c=0; c<4; c++)
01421         if (digits[d][r][c] != ' ')
01422           Label[r][c+width]=1;
01423         else
01424           Label[r][c+width]=0;
01425     width+=5;
01426     }
01427   width=real_width=width+4;
01428   height=5;
01429   if (!GoodSpot(InImage,ROWS,COLS,i,CenterR[i],CenterC[i],height,width))
01430     {
01431     height*=3;
01432     width*=3;
01433     while (height > 1  &&  width > 3)
01434       {
01435       for (r=0; r<ROWS; r+=height/2)
01436         {
01437         for (c=0; c<COLS; c+=width/4)
01438           if (GoodSpot(InImage,ROWS,COLS,i,r,c,height,width))
01439             break;
01440         if (c < COLS)
01441           break;
01442         }
01443       if (r < ROWS)
01444         break;
01445       height/=2;
01446       width/=4;
01447       }
01448     if (height <= 1  ||  width <= 3)
01449       {
01450       r=CenterR[i];
01451       c=CenterC[i];
01452       }
01453     }
01454   else
01455     {
01456     r=CenterR[i];
01457     c=CenterC[i];
01458     }
01459   r-=2;
01460   c-=real_width/2;
01461   for (y=0; y<5; y++)
01462     for (x=0; x<real_width; x++)
01463       if (Label[y][x] == 1)
01464         OutImage[(r+y)*COLS+c+x]=mappit[7]-1;
01465   }
01466 
01467 if ((fpt=fopen(Filename,"w")) == NULL)
01468   {
01469   printf("Unable to open %s for writing.\n",Filename);
01470   exit(1);
01471   }
01472 i=0;
01473 for (c=0; c<8; c++)
01474   if (mappit[c])
01475     i++;
01476 j=0;
01477 for (c=0; c<8; c++)
01478   if (mappit[c])
01479     {
01480     cmap[j]=color_numbers[c*3];
01481     cmap[i+j]=color_numbers[c*3+1];
01482     cmap[i*2+j]=color_numbers[c*3+2];
01483     j++;
01484     }
01485 raster[0]=RAS_MAGIC;
01486 raster[1]=COLS;
01487 raster[2]=ROWS;
01488 raster[3]=8;
01489 raster[4]=ROWS*COLS;
01490 raster[5]=1;
01491 raster[6]=1;
01492 raster[7]=j*3;
01493 fwrite(raster,sizeof(int),8,fpt);
01494 fwrite(cmap,sizeof(unsigned char),j*3,fpt);
01495 fwrite(OutImage,sizeof(unsigned char),ROWS*COLS,fpt);
01496 fclose(fpt);
01497 }
01498 
01499 
01500 
01501 int GoodSpot(InImage,ROWS,COLS,Label,r,c,height,width)
01502 
01503 unsigned char   *InImage;
01504 int             ROWS,COLS,Label,r,c,width,height;
01505 
01506 {
01507 int     x,y;
01508 
01509 for (y=r-height/2; y<=r+height/2; y++)
01510   for (x=c-width/2; x<=c+width/2; x++)
01511     if (y < 0  ||  y >= ROWS  ||  x < 0  ||  x >= COLS  ||
01512         InImage[y*COLS+x] != Label)
01513       return(0);
01514 return(1);
01515 }
01516 
01517 
01518 
01519 


structure_coloring_fkie
Author(s): Bastian Gaspers
autogenerated on Sun Jan 5 2014 11:38:09