Main Page   Data Structures   File List   Data Fields   Globals  

median.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 median filtering of an image.                     */
00009 /*                                                                            */
00010 /*----------------------------------------------------------------------------*/
00011 /*-Interface Information------------------------------------------------------*/
00012 void MEDIAN (
00013 IMAGE *in,      /*  I   Pointer to the input image.                           */
00014 short jsize,    /*  I   Number of lines in the median window.                 */
00015 short ksize,    /*  I   Number of pixels/line in the median window.           */
00016 IMAGE **out     /*  O   Address of a pointer to the output image.             */
00017 /*----------------------------------------------------------------------------*/
00018 ) { register short i, j, k, l, m, count, index, jstart, jend, kstart, kend, jover=(jsize-1)/2, kover=(ksize-1)/2;
00019     char   msg[SLEN];
00020     double pinc, psum;
00021     PIXEL  med, *list=0;
00022 
00023     if (TIMES) TIMING(T_MEDIAN);
00024     if (NAMES) {
00025         MESSAGE('I',"");
00026         MESSAGE('I',"MEDIAN");
00027         MESSAGE('I',"");
00028         sprintf(msg," Input image:           %s",in->text);
00029         MESSAGE('I',msg);
00030         sprintf(msg," Window size: lines:    %d",jsize);
00031         MESSAGE('I',msg);
00032         sprintf(msg,"              pixels:   %d",ksize);
00033         MESSAGE('I',msg);
00034         MESSAGE('I'," ...............");
00035     }
00036 
00037     /* check input */
00038     if (!CHECKIMG(in)) {
00039         MESSAGE('E'," Can't identify image.");
00040         goto the_end;
00041     } else if (jsize <= 0  ||  jsize%2 == 0) {
00042         MESSAGE('E'," Number of lines in the median window must be an odd number greater than zero.");
00043         goto the_end;
00044     } else if (jsize > in->nlin) {
00045         MESSAGE('E'," Image line size must be equal to or greater than window size.");
00046         goto the_end;
00047     } else if (ksize <= 0  ||  ksize%2 == 0) {
00048         MESSAGE('E'," Number of pixels/line in the median window must be an odd number greater than zero.");
00049         goto the_end;
00050     } else if (ksize > in->npix) {
00051         MESSAGE('E'," Image pixel size must be equal to or greater than window size.");
00052         goto the_end;
00053     }
00054 
00055     /* create image of appropriate size */
00056     if (!CHECKIMG(*out)) GETMEM(in->nbnd,in->nlin,in->npix,out);
00057     if (!*out) goto the_end;
00058 
00059     /* allocate space for 1-D array */
00060     if (!(list=(PIXEL *)malloc(jsize*ksize*sizeof(PIXEL)))) {
00061         MESSAGE('E'," Memory request failed.");
00062         goto the_end;
00063     }
00064 
00065     /* initialize progress indicator */
00066     if (LINES  &&  !PROGRESS(psum=0.0)) goto the_end;
00067     pinc = 1.0/(double)in->nbnd/(double)in->nlin;
00068 
00069     /* put pixels inside window into array */
00070     for (i=0; i<in->nbnd; i++) {
00071         for (j=0; j<in->nlin; j++) {
00072             jstart = max(0,j-jover);
00073             jend   = min(j+jover,in->nlin-1);
00074             for (k=0; k<in->npix; k++) {
00075                 kstart = max(0,k-kover);
00076                 kend   = min(k+kover,in->npix-1);
00077                 for (count=0,l=jstart; l<=jend; l++) {
00078                     for (m=kstart; m<=kend; m++) {
00079                         list[count++] = in->data[i][l][m];
00080                     }
00081                 }
00082 
00083                 /* compute median */
00084                 for (l=0; l<(count+1)/2; l++) {
00085                     med = list[index=l];
00086                     for (m=l+1; m<count; m++) {
00087                         if (med > list[m]) med = list[index=m];
00088                     }
00089                     list[index] = list[l];
00090                 }
00091                 (*out)->data[i][j][k] = med;
00092             }
00093             if (LINES  &&  !PROGRESS(psum+=pinc)) goto the_end;
00094         }
00095     }
00096 
00097     the_end:
00098     if (list)  free(list);
00099     if (TIMES) TIMING(T_EXIT);
00100 }

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