Main Page   Data Structures   File List   Data Fields   Globals  

lvlslice.c

Go to the documentation of this file.
00001 #include        "sadie.h"
00002 
00003 /*-Copyright Information------------------------------------------------------*/
00004 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00005 /*----------------------------------------------------------------------------*/
00006 /*-General Information--------------------------------------------------------*/
00007 /*                                                                            */
00008 /*   This function performs a level-slice classification on an image.         */
00009 /*   Pixels lying within two or more boxes in feature space are assigned      */
00010 /*   by the nearest-mean algorithm.                                           */
00011 /*                                                                            */
00012 /*----------------------------------------------------------------------------*/
00013 /*-Interface Information------------------------------------------------------*/
00014 void
00015 LVLSLICE (IMAGE * in,           /*  I   Pointer to the input image.                           */
00016           PIXEL * grng,         /*  I   Matrix[nlev][nbnd][2], containing class boundaries.   */
00017           short nlev,           /*  I   Number of classes                                     */
00018           /*      (and number of graylevels in the output image).       */
00019           IMAGE ** out          /*  O   Address of a pointer to the output image.             */
00020           /*      The input image may be overwritten (out = &in).       */
00021 /*----------------------------------------------------------------------------*/
00022   )
00023 {
00024   register short i, j, k, l, index, count, assign, class;
00025   char msg[SLEN];
00026   PIXEL *center = 0, dnew, dold;
00027 
00028   if (TIMES)
00029     TIMING (T_LVLSLICE);
00030   if (NAMES)
00031     {
00032       MESSAGE ('I', "");
00033       MESSAGE ('I', "LVLSLICE");
00034       MESSAGE ('I', "");
00035       sprintf (msg, " Input image:         %s", in->text);
00036       MESSAGE ('I', msg);
00037       sprintf (msg, " Number of classes:   %d", nlev);
00038       MESSAGE ('I', msg);
00039       sprintf (msg, "       ");
00040       for (i = 0; i < in->nbnd; i++)
00041         {
00042           sprintf (msg + strlen (msg), "          Band %-11d", i + 1);
00043         }
00044       MESSAGE ('I', msg);
00045       sprintf (msg, " Class");
00046       for (i = 0; i < in->nbnd; i++)
00047         {
00048           sprintf (msg + strlen (msg), "      Min         Max     ");
00049         }
00050       MESSAGE ('I', msg);
00051       for (i = 0; i < nlev; i++)
00052         {
00053           sprintf (msg, " %3d ", i + 1);
00054           for (j = 0; j < in->nbnd; j++)
00055             {
00056               sprintf (msg + strlen (msg), "%14.4e%12.4e",
00057                        grng[(i * in->nbnd + j) * 2 + 0],
00058                        grng[(i * in->nbnd + j) * 2 + 1]);
00059             }
00060           MESSAGE ('I', msg);
00061         }
00062       MESSAGE ('I', " ...............");
00063     }
00064 
00065   /* check input */
00066   if (!CHECKIMG (in))
00067     {
00068       MESSAGE ('E', " Can't identify image.");
00069       goto the_end;
00070     }
00071   else if (nlev <= 0)
00072     {
00073       MESSAGE ('E', " Number of classes must be greater than zero.");
00074       goto the_end;
00075     }
00076 
00077   /* create image of appropriate size */
00078   if (!CHECKIMG (*out))
00079     GETMEM (1, in->nlin, in->npix, out);
00080   if (!*out)
00081     goto the_end;
00082 
00083   /* allocate space for center array */
00084   if (!(center = (PIXEL *) malloc (nlev * in->nbnd * sizeof (PIXEL))))
00085     {
00086       MESSAGE ('E', " Memory request failed.");
00087       goto the_end;
00088     }
00089 
00090   /* compute center coordinates for each class  */
00091   for (j = i = 0; i < nlev * in->nbnd; i++)
00092     {
00093       center[i] = (grng[j++] + grng[j++]) / 2.0;
00094     }
00095 
00096   /* perform a level slice classification */
00097   for (j = 0; j < in->nlin; j++)
00098     {
00099       for (k = 0; k < in->npix; k++)
00100         {
00101           for (assign = index = class = l = 0; l < nlev; l++)
00102             {
00103               for (index = l * in->nbnd * 2, count = i = 0; i < in->nbnd; i++)
00104                 {
00105                   if (grng[index++] <= in->data[i][j][k]
00106                       && in->data[i][j][k] <= grng[index++])
00107                     {
00108                       count += 1;
00109                     }
00110                   else
00111                     {
00112                       break;
00113                     }
00114                 }
00115               if (count == in->nbnd)
00116                 {
00117                   if (assign == 0)
00118                     {
00119                       class = l + 1;
00120                       assign = 1;
00121                     }
00122                   else          /* calculate distance to competing classes */
00123                     {
00124                       for (dold = dnew = 0.0, i = 0; i < in->nbnd; i++)
00125                         {
00126                           dold +=
00127                             (in->data[i][j][k] -
00128                              center[(class - 1) * in->nbnd +
00129                                     i]) * (in->data[i][j][k] - center[(class -
00130                                                                        1) *
00131                                                                       in->
00132                                                                       nbnd +
00133                                                                       i]);
00134                           dnew +=
00135                             (in->data[i][j][k] -
00136                              center[l * in->nbnd + i]) * (in->data[i][j][k] -
00137                                                           center[l *
00138                                                                  in->nbnd +
00139                                                                  i]);
00140                         }
00141                       if (dnew < dold)
00142                         class = l + 1;
00143                     }
00144                 }
00145             }
00146           (*out)->data[0][j][k] = (PIXEL) class;
00147         }
00148     }
00149 
00150 the_end:
00151   if (center)
00152     free (center);
00153   if (TIMES)
00154     TIMING (T_EXIT);
00155 }

Generated on Sun May 18 15:36:11 2003 for tclSadie by doxygen1.3