Main Page   Data Structures   File List   Data Fields   Globals  

decorstr.c

Go to the documentation of this file.
00001 #include        "sadie.h"
00002 
00003 /*-Copyright Information------------------------------------------------------*/
00004 /* Copyright (c) 1992 by the University of Arizona Digital Image Analysis Lab */
00005 /*----------------------------------------------------------------------------*/
00006 /*-General Information--------------------------------------------------------*/
00007 /*                                                                            */
00008 /*   This function performs a decorrelation stretch on a multi-band           */
00009 /*   image. The transformed image is uncorrelated, and each band has a        */
00010 /*   variance of the average of the original band variances. The mean         */
00011 /*   of each band remains unchanged.                                          */
00012 /*                                                                            */
00013 /*----------------------------------------------------------------------------*/
00014 /*-Interface Information------------------------------------------------------*/
00015 void
00016 DECORSTR (IMAGE * in,           /*  I   Pointer to the input image                            */
00017           short inc,            /*  I   Pixels/line and line increment to be used             */
00018           /*      for calculating the covariance matrix.                */
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;
00025   double *mean = 0, *dev = 0, *cov = 0, *cor = 0, *eig = 0, *pct =
00026     0, avgdev, sum, pinc, psum;
00027   PIXEL *gmin = 0, *gmax = 0, *grng = 0;
00028   long *zero = 0;
00029 
00030   if (TIMES)
00031     TIMING (T_DECORSTR);
00032 
00033   if (NAMES)
00034     {
00035       MESSAGE ('I', "");
00036       MESSAGE ('I', "DECORSTR");
00037     }
00038 
00039   /* check input */
00040   if (!CHECKIMG (in))
00041     {
00042       MESSAGE ('E', " Can't identify image.");
00043       goto the_end;
00044     }
00045   else if (in->nbnd == 1)
00046     {
00047       MESSAGE ('E', " Image must be multi-band.");
00048       goto the_end;
00049     }
00050   else if (inc <= 0)
00051     {
00052       MESSAGE ('E', " Subsample increment must be greater than zero.");
00053       goto the_end;
00054     }
00055 
00056   /* create image of appropriate size */
00057   if (!CHECKIMG (*out))
00058     GETMEM (in->nbnd, in->nlin, in->npix, out);
00059   if (!*out)
00060     goto the_end;
00061 
00062   /* allocate space */
00063   if (!(mean = (double *) malloc (in->nbnd * sizeof (double))))
00064     {
00065       MESSAGE ('E', " Memory request failed.");
00066       goto the_end;
00067     }
00068   if (!(dev = (double *) malloc (in->nbnd * sizeof (double))))
00069     {
00070       MESSAGE ('E', " Memory request failed.");
00071       goto the_end;
00072     }
00073   if (!(gmin = (PIXEL *) malloc (in->nbnd * sizeof (PIXEL))))
00074     {
00075       MESSAGE ('E', " Memory request failed.");
00076       goto the_end;
00077     }
00078   if (!(gmax = (PIXEL *) malloc (in->nbnd * sizeof (PIXEL))))
00079     {
00080       MESSAGE ('E', " Memory request failed.");
00081       goto the_end;
00082     }
00083   if (!(grng = (PIXEL *) malloc (in->nbnd * sizeof (PIXEL))))
00084     {
00085       MESSAGE ('E', " Memory request failed.");
00086       goto the_end;
00087     }
00088   if (!(zero = (long *) malloc (in->nbnd * sizeof (long))))
00089     {
00090       MESSAGE ('E', " Memory request failed.");
00091       goto the_end;
00092     }
00093   if (!(cov = (double *) malloc (in->nbnd * in->nbnd * sizeof (double))))
00094     {
00095       MESSAGE ('E', " Memory request failed.");
00096       goto the_end;
00097     }
00098   if (!(cor = (double *) malloc (in->nbnd * in->nbnd * sizeof (double))))
00099     {
00100       MESSAGE ('E', " Memory request failed.");
00101       goto the_end;
00102     }
00103   if (!(eig = (double *) malloc (in->nbnd * in->nbnd * sizeof (double))))
00104     {
00105       MESSAGE ('E', " Memory request failed.");
00106       goto the_end;
00107     }
00108   if (!(pct = (double *) malloc (in->nbnd * sizeof (double))))
00109     {
00110       MESSAGE ('E', " Memory request failed.");
00111       goto the_end;
00112     }
00113 
00114   /* initialize progress indicator */
00115   if (LINES && !PROGRESS (psum = 0.0))
00116     goto the_end;
00117   pinc = 1.0 / (double) in->nlin;
00118 
00119   /* calculate statistics, covariance matrix and eigenvectors of the input image */
00120   STATS (in, inc, (short) 0, IGNORE, 0.0, 0.0, mean, dev, gmin, gmax, grng,
00121          zero);
00122   if (ABORT)
00123     goto the_end;
00124   avgdev = 0.0;
00125   for (i = 0; i < in->nbnd; i++)
00126     {
00127       avgdev += dev[i];
00128     }
00129   avgdev /= in->nbnd;
00130   COVAR (in, inc, cov, cor);
00131   if (ABORT)
00132     goto the_end;
00133   EIGEN (cov, in->nbnd, eig);
00134   if (ABORT)
00135     goto the_end;
00136 
00137   for (j = 0; j < in->nlin; j++)
00138     {
00139       for (k = 0; k < in->npix; k++)
00140         {
00141 
00142           /* perform principal component transform on zero mean image */
00143           for (i = 0; i < in->nbnd; i++)
00144             {
00145               for (pct[i] = 0.0, l = 0; l < in->nbnd; l++)
00146                 {
00147                   pct[i] +=
00148                     (double) ((in->data[l][j][k] -
00149                                mean[l]) * eig[l * in->nbnd + i]);
00150                 }
00151             }
00152 
00153           /* equalize variance of separate principal component scores and do inverse PCT.  Add mean back in */
00154           for (i = 0; i < in->nbnd; i++)
00155             {
00156               for (sum = 0.0, l = 0; l < in->nbnd; l++)
00157                 {
00158                   sum +=
00159                     eig[i * in->nbnd +
00160                         l] * ((pct[l] * avgdev) /
00161                               sqrt (cov[l * in->nbnd + l]));
00162                 }
00163               (*out)->data[i][j][k] = (PIXEL) (sum + mean[i]);
00164             }
00165 
00166         }
00167       if (LINES && !PROGRESS (psum += pinc))
00168         goto the_end;
00169     }
00170 
00171 the_end:
00172   if (cov)
00173     free (cov);
00174   if (cor)
00175     free (cor);
00176   if (eig)
00177     free (eig);
00178   if (pct)
00179     free (pct);
00180   if (mean)
00181     free (mean);
00182   if (dev)
00183     free (dev);
00184   if (gmin)
00185     free (gmin);
00186   if (gmax)
00187     free (gmax);
00188   if (grng)
00189     free (grng);
00190   if (zero)
00191     free (zero);
00192   if (TIMES)
00193     TIMING (T_EXIT);
00194 }

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