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
00015 CONTNORM (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   )
00023 {
00024   register short i, j, k;
00025   char msg[SLEN];
00026   double oldsigma, gain, bias;
00027   PIXEL sum, sumsq, oldmean;
00028 
00029   if (TIMES)
00030     TIMING (T_CONTNORM);
00031   if (NAMES)
00032     {
00033       MESSAGE ('I', "");
00034       MESSAGE ('I', "CONTNORM");
00035       MESSAGE ('I', "");
00036       sprintf (msg, " Input image:                          %s", in->text);
00037       MESSAGE ('I', msg);
00038       sprintf (msg, " Graylevel mean:                     %12.4e", mean);
00039       MESSAGE ('I', msg);
00040       sprintf (msg, " Graylevel standard deviation:       %12.4e", sigma);
00041       MESSAGE ('I', msg);
00042       sprintf (msg, " Line and pixel subsample increment:   %d", inc);
00043       MESSAGE ('I', msg);
00044       MESSAGE ('I', " ...............");
00045     }
00046 
00047   /* check input */
00048   if (!CHECKIMG (in))
00049     {
00050       MESSAGE ('E', " Can't identify image.");
00051       goto the_end;
00052     }
00053   else if (inc <= 0)
00054     {
00055       MESSAGE ('E', " Subsample increment must be greater than zero.");
00056       goto the_end;
00057     }
00058 
00059   /* create image of appropriate size */
00060   if (!CHECKIMG (*out))
00061     GETMEM (in->nbnd, in->nlin, in->npix, out);
00062   if (!*out)
00063     goto the_end;
00064 
00065   /* compute statistics */
00066   for (sum = sumsq = 0.0, i = 0; i < in->nbnd; i++)
00067     {
00068       for (j = 0; j < in->nlin; j += inc)
00069         {
00070           for (k = 0; k < in->npix; k += inc)
00071             {
00072               sum += in->data[i][j][k];
00073               sumsq += in->data[i][j][k] * in->data[i][j][k];
00074             }
00075         }
00076     }
00077   oldmean =
00078     (PIXEL) ((double) sum /
00079              ((double) in->nbnd *
00080               (double) (in->nlin / inc +
00081                         (in->nlin % inc ? 1 : 0)) * (double) (in->npix / inc +
00082                                                               (in->npix %
00083                                                                inc ? 1 :
00084                                                                0))));
00085   oldsigma =
00086     sqrt ((double) sumsq /
00087           ((double) in->nbnd *
00088            (double) (in->nlin / inc +
00089                      (in->nlin % inc ? 1 : 0)) * (double) (in->npix / inc +
00090                                                            (in->npix %
00091                                                             inc ? 1 : 0))) -
00092           (double) oldmean * (double) oldmean);
00093 
00094   /* calculate parameters for stretch */
00095   gain = sigma / oldsigma;
00096   bias = (double) mean - gain * (double) oldmean;
00097 
00098   /* perform stretch */
00099   for (i = 0; i < in->nbnd; i++)
00100     {
00101       for (j = 0; j < in->nlin; j++)
00102         {
00103           for (k = 0; k < in->npix; k++)
00104             {
00105               (*out)->data[i][j][k] =
00106                 (PIXEL) (gain * (double) in->data[i][j][k] + bias);
00107             }
00108         }
00109     }
00110 
00111 the_end:
00112   if (TIMES)
00113     TIMING (T_EXIT);
00114 }

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