Main Page   Data Structures   File List   Data Fields   Globals  

chaincode.c

Go to the documentation of this file.
00001 #include "sadie.h"
00002 #include "proto.h"
00003 
00004 #define UP 1
00005 #define DOWN 2
00006 #define LEFT 3
00007 #define RIGHT 4
00008 
00009 
00010 /*----------------------------------------------------------------------------*/
00011 /*-General Information--------------------------------------------------------*/
00012 /*                                                                            */
00013 /* This function computes an 8-connected chain code for an input labeled image*/
00014 /*  image containing a edge maps.                                       */
00015 /*                                                                            */
00016 /*----------------------------------------------------------------------------*/
00017 /*-Interface Information------------------------------------------------------*/
00018 void CHAINCODE (
00019 IMAGE  *in,      /*  I   Pointer to the input image.                          */
00020 IMAGE  **out     /*  O   Address of a pointer to the output image             */
00021 /*----------------------------------------------------------------------------*/
00022 ) { register short i, j, k, n;
00023     char msg[SLEN];
00024     char ccode[SLEN];
00025     int ccodepos = 0;
00026     int regions = 0;
00027     int tempval;
00028     int curlbl;
00029     int foundpix, startx, starty, done, dir;
00030 
00031     /* if (TIMES) TIMING(T_CHAINCODE); */
00032     if (NAMES) {
00033         MESSAGE('I',"");
00034         MESSAGE('I',"CHAINCODE");
00035         MESSAGE('I',"");
00036         sprintf(msg," Input image:                       %s",in->text);
00037         MESSAGE('I',msg);
00038         MESSAGE('I'," ...............");
00039     }
00040 
00041     /* check input */
00042     if (!CHECKIMG(in)) {
00043         MESSAGE('E'," Can't identify image.");
00044         goto the_end;
00045     } else if (in->nbnd > 1) {
00046         MESSAGE('E'," Image must be single-band.");
00047         goto the_end;
00048     }
00049 
00050 
00051 
00052     
00053     
00054     /* Find out how many objects are in the input labeled image */
00055     /* This assumes that labels are assigned consecutively when 
00056         scanning LTR-TTB across the image.                      */
00057     RANGE(in);
00058     tempval = (int) rnd(in->gmin);
00059     for (j=0; j<in->nlin; j++) {
00060         for (k=0; k<in->npix; k++) {
00061             if ((int) rnd(in->data[0][j][k]) > tempval) {
00062                 tempval = (int) rnd(in->data[0][j][k]);
00063                 regions++;
00064             }
00065         }    
00066     }
00067     
00068     if (regions == 0) {
00069         MESSAGE('E'," Number of regions in image must be greater than zero.");
00070         goto the_end;
00071     }
00072 
00073 
00074     /* create output image of appropriate size */
00075     if (!CHECKIMG(*out)) GETMEM(in->nbnd,in->nlin,in->npix,out);
00076     if (!*out) goto the_end;
00077 
00078 
00079     /* Initialize the boundary image to zeros */
00080     for (j=0; j<in->nlin; j++) {
00081         for (k=0; k<in->npix; k++) {
00082             (*out)->data[0][j][k] = (PIXEL)0.0;
00083         }    
00084     }
00085 
00086     for (curlbl = (int)rnd(in->gmin) + 1; curlbl <= (int)rnd(in->gmax); curlbl++) {
00087         /* Find the first pixel in the region */
00088         foundpix = 0;
00089 
00090         for (j=0; j<in->nlin;j++) {
00091             for (k=0; k<in->npix; k++) {
00092                 tempval = (int) rnd(in->data[0][j][k]);
00093                 if (curlbl == tempval) {
00094                     foundpix = 1;
00095                     break;
00096                 }
00097             }
00098             
00099             if (foundpix == 1) {
00100                 break;
00101             }
00102         }
00103         
00104         /* Record the starting location */
00105         startx = k;
00106         starty = j;
00107         
00108         if (foundpix == 1) {
00109         
00110             done = 0;
00111             
00112             strcpy(ccode, "     ");
00113             ccodepos = strlen(ccode);
00114 
00115             /* Find the first neighbor included in the region */
00116             if ((k>0) && ((int) rnd(in->data[0][j][k-1]) == curlbl)) { /*Check n1*/
00117                 dir = UP;
00118                 k = k - 1;
00119                 (*out)->data[0][j][k] = (PIXEL)1.0;
00120                 sprintf(ccode+ccodepos,"%d",1);
00121             } else if ((j>0) && (k>0) && ((int)rnd(in->data[0][j-1][k-1]) ==curlbl)) { /*Check n2*/
00122                 dir = UP;
00123                 k = k - 1;
00124                 j = j - 1;
00125                 (*out)->data[0][j][k] = (PIXEL)2.0;
00126                 sprintf(ccode+ccodepos,"%d",2);
00127             } else if ((j>0) && ((int)rnd(in->data[0][j-1][k]) ==curlbl)) { /*Check n3*/
00128                 dir = RIGHT;
00129                 j = j - 1;
00130                 (*out)->data[0][j][k] = (PIXEL)3.0;
00131                 sprintf(ccode+ccodepos,"%d",3);
00132             } else if ((j>0) && (k<in->npix-1) && ((int)rnd(in->data[0][j-1][k+1]) ==curlbl)) { /*Check n4*/
00133                 dir = RIGHT;
00134                 k = k + 1;
00135                 j = j - 1;
00136                 (*out)->data[0][j][k] = (PIXEL)4.0;
00137                 sprintf(ccode+ccodepos,"%d",4);
00138             } else if ((k<in->npix-1) && ((int)rnd(in->data[0][j][k+1]) ==curlbl)) { /*Check n5*/
00139                 dir = DOWN;
00140                 k = k + 1;
00141                 (*out)->data[0][j][k] = (PIXEL)5.0;
00142                 sprintf(ccode+ccodepos,"%d",5);
00143             } else if ((j<in->nlin-1) && (k<in->npix-1) && ((int)rnd(in->data[0][j+1][k+1]) ==curlbl)) { /*Check n6*/
00144                 dir = DOWN;
00145                 k = k + 1;
00146                 j = j + 1;
00147                 (*out)->data[0][j][k] = (PIXEL)6.0;
00148                 sprintf(ccode+ccodepos,"%d",6);
00149             } else if ((j<in->nlin-1) && ((int)rnd(in->data[0][j+1][k]) ==curlbl)) { /*Check n7*/
00150                 dir = LEFT;
00151                 j = j + 1;
00152                 (*out)->data[0][j][k] = (PIXEL)7.0;
00153                 sprintf(ccode+ccodepos,"%d",7);
00154             } else if ((j<in->nlin-1) && (k>0) && ((int)rnd(in->data[0][j+1][k-1]) ==curlbl)) { /*Check n8*/
00155                 dir = LEFT;
00156                 k = k - 1;
00157                 j = j + 1;
00158                 (*out)->data[0][j][k] = (PIXEL)8.0;
00159                 sprintf(ccode+ccodepos,"%d",8);
00160             } else {
00161                 /* This is a one pixel region */
00162                 done = 1;
00163                 sprintf(msg,"Region %d is a one pixel region", curlbl);
00164                 MESSAGE('I',msg);  
00165             }
00166 
00167         } else {
00168             /* No foreground objects were found */
00169             done = 1;
00170             sprintf(msg,"No foreground objects were found!");
00171             MESSAGE('I',msg);  
00172         }
00173 
00174         while (done != 1) {
00175             ccodepos = strlen(ccode);
00176 
00177             if (dir == RIGHT) {
00178                 if ((k>0) && ((int) rnd(in->data[0][j][k-1]) == curlbl)) { /*Check n1*/
00179                     dir = UP;
00180                     k = k - 1;
00181                     (*out)->data[0][j][k] = (PIXEL)1.0;
00182                     sprintf(ccode+ccodepos," - %d",1);
00183                 } else if ((j>0) && (k>0) && ((int)rnd(in->data[0][j-1][k-1]) ==curlbl)) { /*Check n2*/
00184                     dir = UP;
00185                     k = k - 1;
00186                     j = j - 1;
00187                     (*out)->data[0][j][k] = (PIXEL)2.0;
00188                     sprintf(ccode+ccodepos," - %d",2);
00189                 } else if ((j>0) && ((int)rnd(in->data[0][j-1][k]) ==curlbl)) { /*Check n3*/
00190                     dir = RIGHT;
00191                     j = j - 1;
00192                     (*out)->data[0][j][k] = (PIXEL)3.0;
00193                     sprintf(ccode+ccodepos," - %d",3);
00194                 } else if ((j>0) && (k<in->npix-1) && ((int)rnd(in->data[0][j-1][k+1]) ==curlbl)) { /*Check n4*/
00195                     dir = RIGHT;
00196                     k = k + 1;
00197                     j = j - 1;
00198                     (*out)->data[0][j][k] = (PIXEL)4.0;
00199                     sprintf(ccode+ccodepos," - %d",4);
00200                 } else if ((k<in->npix-1) && ((int)rnd(in->data[0][j][k+1]) ==curlbl)) { /*Check n5*/
00201                     dir = DOWN;
00202                     k = k + 1;
00203                     (*out)->data[0][j][k] = (PIXEL)5.0;
00204                     sprintf(ccode+ccodepos," - %d",5);
00205                 } else if ((j<in->nlin-1) && (k<in->npix-1) && ((int)rnd(in->data[0][j+1][k+1]) ==curlbl)) { /*Check n6*/
00206                     dir = DOWN;
00207                     k = k + 1;
00208                     j = j + 1;
00209                     (*out)->data[0][j][k] = (PIXEL)6.0;
00210                     sprintf(ccode+ccodepos," - %d",6);
00211                 } else if ((j<in->nlin-1) && ((int)rnd(in->data[0][j+1][k]) ==curlbl)) { /*Check n7*/
00212                     dir = LEFT;
00213                     j = j + 1;
00214                     (*out)->data[0][j][k] = (PIXEL)7.0;
00215                     sprintf(ccode+ccodepos," - %d",7);
00216                 } else if ((j<in->nlin-1) && (k>0) && ((int)rnd(in->data[0][j+1][k-1]) ==curlbl)) { /*Check n8*/
00217                     dir = LEFT;
00218                     k = k - 1;
00219                     j = j + 1;
00220                     (*out)->data[0][j][k] = (PIXEL)8.0;
00221                     sprintf(ccode+ccodepos," - %d",8);
00222                 } else {
00223                     MESSAGE('E'," Could not find neighbor in region.");
00224                     sprintf(msg,"   curlbl = %d, k = %d, j = %d, dir = %d", curlbl,k,j,dir);
00225                     MESSAGE('E',msg);  
00226                     goto the_end;
00227                 }
00228             } else if (dir == LEFT) {
00229                 if ((k<in->npix-1) && ((int)rnd(in->data[0][j][k+1]) ==curlbl)) { /*Check n1*/
00230                     dir = DOWN;
00231                     k = k + 1;
00232                     (*out)->data[0][j][k] = (PIXEL)5.0;
00233                     sprintf(ccode+ccodepos," - %d",5);
00234                 } else if ((j<in->nlin-1) && (k<in->npix-1) && ((int)rnd(in->data[0][j+1][k+1]) ==curlbl)) { /*Check n2*/
00235                     dir = DOWN;
00236                     k = k + 1;
00237                     j = j + 1;
00238                     (*out)->data[0][j][k] = (PIXEL)6.0;
00239                     sprintf(ccode+ccodepos," - %d",6);
00240                 } else if ((j<in->nlin-1) && ((int)rnd(in->data[0][j+1][k]) ==curlbl)) { /*Check n3*/
00241                     dir = LEFT;
00242                     j = j + 1;
00243                     (*out)->data[0][j][k] = (PIXEL)7.0;
00244                     sprintf(ccode+ccodepos," - %d",7);
00245                 } else if ((j<in->nlin-1) && (k>0) && ((int)rnd(in->data[0][j+1][k-1]) ==curlbl)) { /*Check n4*/
00246                     dir = LEFT;
00247                     k = k - 1;
00248                     j = j + 1;
00249                     (*out)->data[0][j][k] = (PIXEL)8.0;
00250                     sprintf(ccode+ccodepos," - %d",8);
00251                 } else if ((k>0) && ((int) rnd(in->data[0][j][k-1]) == curlbl)) { /*Check n5*/
00252                     dir = UP;
00253                     k = k - 1;
00254                     (*out)->data[0][j][k] = (PIXEL)1.0;
00255                     sprintf(ccode+ccodepos," - %d",1);
00256                 } else if ((j>0) && (k>0) && ((int)rnd(in->data[0][j-1][k-1]) ==curlbl)) { /*Check n6*/
00257                     dir = UP;
00258                     k = k - 1;
00259                     j = j - 1;
00260                     (*out)->data[0][j][k] = (PIXEL)2.0;
00261                     sprintf(ccode+ccodepos," - %d",2);
00262                 } else if ((j>0) && ((int)rnd(in->data[0][j-1][k]) ==curlbl)) { /*Check n7*/
00263                     dir = RIGHT;
00264                     j = j - 1;
00265                     (*out)->data[0][j][k] = (PIXEL)3.0;
00266                     sprintf(ccode+ccodepos," - %d",3);
00267                 } else if ((j>0) && (k<in->npix-1) && ((int)rnd(in->data[0][j-1][k+1]) ==curlbl)) { /*Check n8*/
00268                     dir = RIGHT;
00269                     k = k + 1;
00270                     j = j - 1;
00271                     (*out)->data[0][j][k] = (PIXEL)4.0;
00272                     sprintf(ccode+ccodepos," - %d",4);
00273                 } else {
00274                     MESSAGE('E'," Could not find neighbor in region.");
00275                     sprintf(msg,"   curlbl = %d, k = %d, j = %d, dir = %d", curlbl,k,j,dir);
00276                     MESSAGE('E',msg);  
00277                     goto the_end;
00278                 }
00279             } else if (dir == UP) {
00280                 if ((j<in->nlin-1) && ((int)rnd(in->data[0][j+1][k]) ==curlbl)) { /*Check n1*/
00281                     dir = LEFT;
00282                     j = j + 1;
00283                     (*out)->data[0][j][k] = (PIXEL)7.0;
00284                     sprintf(ccode+ccodepos," - %d",7);
00285                 } else if ((j<in->nlin-1) && (k>0) && ((int)rnd(in->data[0][j+1][k-1]) ==curlbl)) { /*Check n2*/
00286                     dir = LEFT;
00287                     k = k - 1;
00288                     j = j + 1;
00289                     (*out)->data[0][j][k] = (PIXEL)8.0;
00290                     sprintf(ccode+ccodepos," - %d",8);
00291                 } else if ((k>0) && ((int) rnd(in->data[0][j][k-1]) == curlbl)) { /*Check n3*/
00292                     dir = UP;
00293                     k = k - 1;
00294                     (*out)->data[0][j][k] = (PIXEL)1.0;
00295                     sprintf(ccode+ccodepos," - %d",1);
00296                 } else if ((j>0) && (k>0) && ((int)rnd(in->data[0][j-1][k-1]) ==curlbl)) { /*Check n4*/
00297                     dir = UP;
00298                     k = k - 1;
00299                     j = j - 1;
00300                     (*out)->data[0][j][k] = (PIXEL)2.0;
00301                     sprintf(ccode+ccodepos," - %d",2);
00302                 } else if ((j>0) && ((int)rnd(in->data[0][j-1][k]) ==curlbl)) { /*Check n5*/
00303                     dir = RIGHT;
00304                     j = j - 1;
00305                     (*out)->data[0][j][k] = (PIXEL)3.0;
00306                     sprintf(ccode+ccodepos," - %d",3);
00307                 } else if ((j>0) && (k<in->npix-1) && ((int)rnd(in->data[0][j-1][k+1]) ==curlbl)) { /*Check n6*/
00308                     dir = RIGHT;
00309                     k = k + 1;
00310                     j = j - 1;
00311                     (*out)->data[0][j][k] = (PIXEL)4.0;
00312                     sprintf(ccode+ccodepos," - %d",4);
00313                 } else if ((k<in->npix-1) && ((int)rnd(in->data[0][j][k+1]) ==curlbl)) { /*Check n7*/
00314                     dir = DOWN;
00315                     k = k + 1;
00316                     (*out)->data[0][j][k] = (PIXEL)5.0;
00317                     sprintf(ccode+ccodepos," - %d",5);
00318                 } else if ((j<in->nlin-1) && (k<in->npix-1) && ((int)rnd(in->data[0][j+1][k+1]) ==curlbl)) { /*Check n8*/
00319                     dir = DOWN;
00320                     k = k + 1;
00321                     j = j + 1;
00322                     (*out)->data[0][j][k] = (PIXEL)6.0;
00323                     sprintf(ccode+ccodepos," - %d",6);
00324                 } else {
00325                     MESSAGE('E'," Could not find neighbor in region.");
00326                     sprintf(msg,"   curlbl = %d, k = %d, j = %d, dir = %d", curlbl,k,j,dir);
00327                     MESSAGE('E',msg);  
00328                     goto the_end;
00329                 }
00330             } else {    /* dir == DOWN */
00331                 if ((j>0) && ((int)rnd(in->data[0][j-1][k]) ==curlbl)) { /*Check n1*/
00332                     dir = RIGHT;
00333                     j = j - 1;
00334                     (*out)->data[0][j][k] = (PIXEL)3.0;
00335                     sprintf(ccode+ccodepos," - %d",3);
00336                 } else if ((j>0) && (k<in->npix-1) && ((int)rnd(in->data[0][j-1][k+1]) ==curlbl)) { /*Check n2*/
00337                     dir = RIGHT;
00338                     k = k + 1;
00339                     j = j - 1;
00340                     (*out)->data[0][j][k] = (PIXEL)4.0;
00341                     sprintf(ccode+ccodepos," - %d",4);
00342                 } else if ((k<in->npix-1) && ((int)rnd(in->data[0][j][k+1]) ==curlbl)) { /*Check n3*/
00343                     dir = DOWN;
00344                     k = k + 1;
00345                     (*out)->data[0][j][k] = (PIXEL)5.0;
00346                     sprintf(ccode+ccodepos," - %d",5);
00347                 } else if ((j<in->nlin-1) && (k<in->npix-1) && ((int)rnd(in->data[0][j+1][k+1]) ==curlbl)) { /*Check n4*/
00348                     dir = DOWN;
00349                     k = k + 1;
00350                     j = j + 1;
00351                     (*out)->data[0][j][k] = (PIXEL)6.0;
00352                     sprintf(ccode+ccodepos," - %d",6);
00353                 } else if ((j<in->nlin-1) && ((int)rnd(in->data[0][j+1][k]) ==curlbl)) { /*Check n5*/
00354                     dir = LEFT;
00355                     j = j + 1;
00356                     (*out)->data[0][j][k] = (PIXEL)7.0;
00357                     sprintf(ccode+ccodepos," - %d",7);
00358                 } else if ((j<in->nlin-1) && (k>0) && ((int)rnd(in->data[0][j+1][k-1]) ==curlbl)) { /*Check n6*/
00359                     dir = LEFT;
00360                     k = k - 1;
00361                     j = j + 1;
00362                     (*out)->data[0][j][k] = (PIXEL)8.0;
00363                     sprintf(ccode+ccodepos," - %d",8);
00364                 } else if ((k>0) && ((int) rnd(in->data[0][j][k-1]) == curlbl)) { /*Check n7*/
00365                     dir = UP;
00366                     k = k - 1;
00367                     (*out)->data[0][j][k] = (PIXEL)1.0;
00368                     sprintf(ccode+ccodepos," - %d",1);
00369                 } else if ((j>0) && (k>0) && ((int)rnd(in->data[0][j-1][k-1]) ==curlbl)) { /*Check n8*/
00370                     dir = UP;
00371                     k = k - 1;
00372                     j = j - 1;
00373                     (*out)->data[0][j][k] = (PIXEL)2.0;
00374                     sprintf(ccode+ccodepos," - %d",2);
00375                 } else {
00376                     MESSAGE('E'," Could not find neighbor in region.");
00377                     sprintf(msg,"   curlbl = %d, k = %d, j = %d, dir = %d", curlbl,k,j,dir);
00378                     MESSAGE('E',msg);  
00379                     goto the_end;
00380                 }
00381             }
00382 
00383             if ((k==startx) && (j==starty)) {
00384                 done = 1;
00385                 sprintf(msg,"Chain Code for Region %d:", curlbl);
00386                 MESSAGE('I',msg);
00387                 MESSAGE('I',ccode);
00388             }
00389             
00390         }
00391     }
00392     
00393         
00394     the_end:
00395     /* if (TIMES) TIMING(T_EXIT); */
00396 
00397 }
00398 
00399 
00400 
00401 
00402 
00403 

Generated on Wed Apr 9 08:56:04 2003 for TREES by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002