Main Page   Data Structures   File List   Data Fields   Globals  

covar.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 computes the band-to-band covariance and correlation       */
00009 /*   matrices of an image.                                                    */
00010 /*                                                                            */
00011 /*----------------------------------------------------------------------------*/
00012 /*-Interface Information------------------------------------------------------*/
00013 void
00014 COVAR (IMAGE * in,              /*  I   Pointer to the image.                                 */
00015        short inc,               /*  I   Pixels/line and line increment.                       */
00016        double *cov,             /*  O   Pointer to the covariance matrix.                     */
00017        double *cor              /*  O   Pointer to the correlation matrix.                    */
00018 /*----------------------------------------------------------------------------*/
00019   )
00020 {
00021   register short i, j, k, l;
00022   char msg[SLEN];
00023   double *sum = 0, *sumsq = 0, count, pinc, psum;
00024 
00025   if (TIMES)
00026     TIMING (T_COVAR);
00027   if (NAMES)
00028     {
00029       MESSAGE ('I', "");
00030       MESSAGE ('I', "COVAR");
00031       MESSAGE ('I', "");
00032       sprintf (msg, " Input image:                          %s", in->text);
00033       MESSAGE ('I', msg);
00034       sprintf (msg, " Line and pixel subsample increment:   %d", inc);
00035       MESSAGE ('I', msg);
00036       MESSAGE ('I', " ...............");
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 (inc <= 0)
00046     {
00047       MESSAGE ('E', " Subsample increment must be greater than zero.");
00048       goto the_end;
00049     }
00050 
00051   /* allocate space for summation arrays */
00052   if (!(sum = (double *) malloc (in->nbnd * sizeof (double))))
00053     {
00054       MESSAGE ('E', " Memory request failed.");
00055       goto the_end;
00056     }
00057   if (!(sumsq = (double *) malloc (in->nbnd * in->nbnd * sizeof (double))))
00058     {
00059       MESSAGE ('E', " Memory request failed.");
00060       goto the_end;
00061     }
00062 
00063   /* initialize progress indicator */
00064   if (LINES && !PROGRESS (psum = 0.0))
00065     goto the_end;
00066   pinc =
00067     2.0 / (double) (in->nbnd +
00068                     1) / (double) in->nbnd / (double) (in->nlin / inc +
00069                                                        (in->nlin %
00070                                                         inc ? 1 : 0));
00071 
00072   /* initialize */
00073   for (i = 0; i < in->nbnd; i++)
00074     {
00075       sum[i] = 0.0;
00076       for (j = 0; j < in->nbnd; j++)
00077         {
00078           sumsq[i * in->nbnd + j] = 0.0;
00079         }
00080     }
00081 
00082   /* compute the sums */
00083   for (i = 0; i < in->nbnd; i++)
00084     {
00085       for (j = 0; j < in->nlin; j += inc)
00086         {
00087           for (k = 0; k < in->npix; k += inc)
00088             {
00089               sum[i] += (double) in->data[i][j][k];
00090               sumsq[i * in->nbnd + i] +=
00091                 (double) in->data[i][j][k] * in->data[i][j][k];
00092             }
00093           if (LINES && !PROGRESS (psum += pinc))
00094             goto the_end;
00095         }
00096     }
00097   for (i = 0; i < in->nbnd - 1; i++)
00098     {
00099       for (l = i + 1; l < in->nbnd; l++)
00100         {
00101           for (j = 0; j < in->nlin; j += inc)
00102             {
00103               for (k = 0; k < in->npix; k += inc)
00104                 {
00105                   sumsq[i * in->nbnd + l] = sumsq[l * in->nbnd + i] +=
00106                     (double) (in->data[i][j][k] * in->data[l][j][k]);
00107                 }
00108               if (LINES && !PROGRESS (psum += pinc))
00109                 goto the_end;
00110             }
00111         }
00112     }
00113 
00114   /* calculate the covariance matrix */
00115   count =
00116     (double) (in->nlin / inc +
00117               (in->nlin % inc ? 1 : 0)) * (double) (in->npix / inc +
00118                                                     (in->npix % inc ? 1 : 0));
00119   for (i = 0; i < in->nbnd; i++)
00120     {
00121       for (j = 0; j < in->nbnd; j++)
00122         {
00123           cov[i * in->nbnd + j] =
00124             (sumsq[i * in->nbnd + j] - sum[i] * sum[j] / count) / (count -
00125                                                                    1.0);
00126         }
00127     }
00128 
00129   /* calculate the correlation matrix */
00130   for (i = 0; i < in->nbnd; i++)
00131     {
00132       for (j = 0; j < in->nbnd; j++)
00133         {
00134           cor[i * in->nbnd + j] =
00135             cov[i * in->nbnd +
00136                 j] / sqrt (cov[i * in->nbnd + i] * cov[j * in->nbnd + j]);
00137         }
00138     }
00139 
00140   /* output both matrices */
00141   MESSAGE ('I', "");
00142   MESSAGE ('I', " Covariance Matrix");
00143   for (i = 0; i < in->nbnd; i++)
00144     {
00145       for (j = k = 0, msg[k++] = ' '; j < in->nbnd; j += 1, k = strlen (msg))
00146         {
00147           sprintf (msg + k, "%12.4e", cov[i * in->nbnd + j]);
00148         }
00149       MESSAGE ('I', msg);
00150     }
00151   MESSAGE ('I', "");
00152   MESSAGE ('I', " Correlation Matrix");
00153   for (i = 0; i < in->nbnd; i++)
00154     {
00155       for (j = k = 0, msg[k++] = ' '; j < in->nbnd; j += 1, k = strlen (msg))
00156         {
00157           sprintf (msg + k, "%12.4e", cor[i * in->nbnd + j]);
00158         }
00159       MESSAGE ('I', msg);
00160     }
00161 
00162 the_end:
00163   if (sum)
00164     free (sum);
00165   if (sumsq)
00166     free (sumsq);
00167   if (TIMES)
00168     TIMING (T_EXIT);
00169 }

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