Main Page   Data Structures   File List   Data Fields   Globals  

contnorm.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 contrast normalization on an image.  The mean   */
00009 /*   graylevel is shifted, and the graylevels are stretched to result in      */
00010 /*   the specified standard deviation from the mean.                          */
00011 /*                                                                            */
00012 /*----------------------------------------------------------------------------*/
00013 /*-Interface Information------------------------------------------------------*/
00014 void CONTNORM (
00015 IMAGE  *in,     /*  I   Pointer to the input image.                           */
00016 PIXEL  mean,    /*  I   Mean graylevel in the output image.                   */
00017 double sigma,   /*  I   Standard deviation in the output image.               */
00018 short  inc,     /*  I   Line and pixel subsample increment.                   */
00019 IMAGE  **out    /*  O   Address of a pointer to the output image.             */
00020                 /*      The input image may be overwritten (out = &in).       */
00021 /*----------------------------------------------------------------------------*/
00022 ) { register short i, j, k;
00023     char   msg[SLEN];
00024     double oldsigma, gain, bias;
00025     PIXEL  sum, sumsq, oldmean;
00026 
00027     if (TIMES) TIMING(T_CONTNORM);
00028     if (NAMES) {
00029         MESSAGE('I',"");
00030         MESSAGE('I',"CONTNORM");
00031         MESSAGE('I',"");  
00032         sprintf(msg," Input image:                          %s",in->text);
00033         MESSAGE('I',msg);
00034         sprintf(msg," Graylevel mean:                     %12.4e",mean);
00035         MESSAGE('I',msg);
00036         sprintf(msg," Graylevel standard deviation:       %12.4e",sigma);
00037         MESSAGE('I',msg);
00038         sprintf(msg," Line and pixel subsample increment:   %d",inc);
00039         MESSAGE('I',msg);
00040         MESSAGE('I'," ...............");
00041     }
00042 
00043     /* check input */
00044     if (!CHECKIMG(in)) {
00045         MESSAGE('E'," Can't identify image.");
00046         goto the_end;
00047     } else if (inc <= 0) {
00048         MESSAGE('E'," Subsample increment must be greater than zero.");
00049         goto the_end;
00050     }
00051 
00052     /* create image of appropriate size */
00053     if (!CHECKIMG(*out)) GETMEM(in->nbnd,in->nlin,in->npix,out);
00054     if (!*out) goto the_end;
00055 
00056     /* compute statistics */
00057     for (sum=sumsq=0.0,i=0; i<in->nbnd; i++) {
00058         for (j=0; j<in->nlin; j+=inc) {
00059             for (k=0; k<in->npix; k+=inc) {
00060                 sum   += in->data[i][j][k];
00061                 sumsq += in->data[i][j][k]*in->data[i][j][k];
00062             }
00063         }
00064     }    
00065     oldmean  = (PIXEL)((double)sum/((double)in->nbnd*(double)(in->nlin/inc+(in->nlin%inc ? 1:0))*(double)(in->npix/inc+(in->npix%inc ? 1:0))));
00066     oldsigma = sqrt((double)sumsq/((double)in->nbnd*(double)(in->nlin/inc+(in->nlin%inc ? 1:0))*(double)(in->npix/inc+(in->npix%inc ? 1:0)))-(double)oldmean*(double)oldmean);
00067 
00068     /* calculate parameters for stretch */
00069     gain    = sigma / oldsigma;
00070     bias    = (double)mean - gain*(double)oldmean;
00071     
00072     /* perform stretch */
00073     for (i=0; i<in->nbnd; i++) {
00074         for (j=0; j<in->nlin; j++) {
00075             for (k=0; k<in->npix; k++) {
00076                 (*out)->data[i][j][k] = (PIXEL)(gain*(double)in->data[i][j][k] + bias);
00077             }
00078         }
00079     }
00080 
00081     the_end:
00082     if (TIMES) TIMING(T_EXIT);
00083 }

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