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 COVAR (
00014 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 ) { register short i, j, k, l;
00020     char   msg[SLEN];
00021     double *sum=0, *sumsq=0, count, pinc, psum;
00022 
00023     if (TIMES) TIMING(T_COVAR);
00024     if (NAMES) {
00025         MESSAGE('I',"");
00026         MESSAGE('I',"COVAR");
00027         MESSAGE('I',"");
00028         sprintf(msg," Input image:                          %s",in->text);
00029         MESSAGE('I',msg);
00030         sprintf(msg," Line and pixel subsample increment:   %d",inc);
00031         MESSAGE('I',msg);
00032         MESSAGE('I'," ...............");
00033     }
00034 
00035     /* check input */
00036     if (!CHECKIMG(in)) {
00037         MESSAGE('E'," Can't identify image.");
00038         goto the_end;
00039     } else if (inc <= 0) {
00040         MESSAGE('E'," Subsample increment must be greater than zero.");
00041         goto the_end;
00042     }
00043 
00044     /* allocate space for summation arrays */
00045     if (!(sum=(double *)malloc(in->nbnd*sizeof(double)))) {
00046         MESSAGE('E'," Memory request failed.");
00047         goto the_end;
00048     }
00049     if (!(sumsq=(double *)malloc(in->nbnd*in->nbnd*sizeof(double)))) {
00050         MESSAGE('E'," Memory request failed.");
00051         goto the_end;
00052     }
00053 
00054     /* initialize progress indicator */
00055     if (LINES  &&  !PROGRESS(psum=0.0)) goto the_end;
00056     pinc = 2.0/(double)(in->nbnd+1)/(double)in->nbnd/(double)(in->nlin/inc + (in->nlin%inc?1:0));
00057 
00058     /* initialize */
00059     for (i=0; i<in->nbnd; i++) {
00060         sum[i] = 0.0;
00061         for (j=0; j<in->nbnd; j++) {
00062             sumsq[i*in->nbnd+j] = 0.0;
00063         }
00064     }
00065 
00066     /* compute the sums */
00067     for (i=0; i<in->nbnd; i++) {
00068         for (j=0; j<in->nlin; j+=inc) {
00069             for (k=0; k<in->npix; k+=inc) {
00070                 sum[i]              += (double)in->data[i][j][k];
00071                 sumsq[i*in->nbnd+i] += (double)in->data[i][j][k] * in->data[i][j][k];
00072             }
00073             if (LINES  &&  !PROGRESS(psum+=pinc)) goto the_end;
00074         }
00075     }
00076     for (i=0; i<in->nbnd-1; i++) {
00077         for (l=i+1; l<in->nbnd; l++) {
00078             for (j=0; j<in->nlin; j+=inc) {
00079                 for (k=0; k<in->npix; k+=inc) {
00080                     sumsq[i*in->nbnd+l] = sumsq[l*in->nbnd+i] += (double)(in->data[i][j][k] * in->data[l][j][k]);
00081                 }
00082                 if (LINES  &&  !PROGRESS(psum+=pinc)) goto the_end;
00083             }
00084         }
00085     }
00086 
00087     /* calculate the covariance matrix */
00088     count = (double)(in->nlin/inc+(in->nlin%inc ? 1:0)) * (double)(in->npix/inc+(in->npix%inc ? 1:0));
00089     for (i=0; i<in->nbnd; i++) {
00090         for (j=0; j<in->nbnd; j++) {
00091             cov[i*in->nbnd+j] = (sumsq[i*in->nbnd+j]-sum[i]*sum[j]/count)/(count-1.0);
00092         }
00093     }
00094 
00095     /* calculate the correlation matrix */
00096     for (i=0; i<in->nbnd; i++) {
00097         for (j=0; j<in->nbnd; j++) {
00098             cor[i*in->nbnd+j] = cov[i*in->nbnd+j]/sqrt(cov[i*in->nbnd+i]*cov[j*in->nbnd+j]);
00099         }
00100     }
00101 
00102     /* output both matrices */
00103     MESSAGE('I',"");
00104     MESSAGE('I'," Covariance Matrix");
00105     for (i=0; i<in->nbnd; i++) {
00106         for (j=k=0,msg[k++]=' '; j<in->nbnd; j+=1,k=strlen(msg)) {
00107             sprintf(msg+k,"%12.4e",cov[i*in->nbnd+j]);
00108         }
00109         MESSAGE('I',msg);
00110     }
00111     MESSAGE('I',"");
00112     MESSAGE('I'," Correlation Matrix");
00113     for (i=0; i<in->nbnd; i++) {
00114         for (j=k=0,msg[k++]=' '; j<in->nbnd; j+=1,k=strlen(msg)) {
00115             sprintf(msg+k,"%12.4e",cor[i*in->nbnd+j]);
00116         }
00117         MESSAGE('I',msg);
00118     }
00119 
00120     the_end:
00121     if (sum)   free(sum);
00122     if (sumsq) free(sumsq);
00123     if (TIMES) TIMING(T_EXIT);
00124 }

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