Main Page   Data Structures   File List   Data Fields   Globals  

findbestfocus.c

Go to the documentation of this file.
00001 #include        "sadie.h"
00002 #include        "proto.h"
00003 
00004 
00005 /*-General Information--------------------------------------------------------*/
00006 /*                                                                            */
00007 /*   This function takes an array of input images and determines which one    */
00008 /*   has the best focus.  See W. S. Conner's thesis for algorithm             */
00009 /*   description.                                                             */
00010 /*                                                                            */
00011 /*----------------------------------------------------------------------------*/
00012 /*-Interface Information------------------------------------------------------*/
00013 void FINDBESTFOCUS (
00014 IMAGE **in,     /*  I   Pointer to an array[nimg] containing pointers         */
00015                 /*      to the input images.                                  */
00016 int nimg,       /*  I   Number of input images.                               */
00017 int* bestfocus  /*      Pointer to the best focused image's number            */
00018 /*----------------------------------------------------------------------------*/
00019 ) { register int i, j, k;
00020     char   msg[SLEN];
00021     int nlin, npix;
00022     IMAGE *periodogram=NULL;
00023     PIXEL **radialavg=NULL;
00024     PIXEL dcvalue;
00025     PIXEL avgvalue;
00026     PIXEL maxvalue;
00027     PIXEL value;
00028     int count_array[11];
00029     int delta_x, delta_y, radius;
00030 
00031     int progmeter;
00032     double progress;
00033     double localpercent;
00034     double localprogress;
00035     int count, total, increment;
00036     int cancel=0;
00037 
00038     progmeter = ProgMeter_Create("Find Best Focus");
00039     progress = 0.0;
00040 
00041 
00042     /* check input */
00043     for (i=0; i<nimg; i++) {
00044         if (!CHECKIMG(in[i])) {
00045             MESSAGE('E'," Can't identify input image(s).");
00046             goto the_end;
00047         }
00048     }
00049     /* for (nlin=in[0]->nlin,i=1; i<nimg; i++) { */
00050     for (nlin=256,i=0; i<nimg; i++) {
00051         if (nlin != in[i]->nlin) {
00052             //printf("%d: %d vs. %d\n",i,nlin,in[i]->nlin);
00053             MESSAGE('E'," Number of lines must be identical in all images.");
00054             goto the_end;
00055         }
00056     }
00057     /* for (npix=in[0]->npix,i=1; i<nimg; i++) { */
00058     for (npix=256,i=0; i<nimg; i++) {
00059         if (npix != in[i]->npix) {
00060             MESSAGE('E'," Number of pixels must be identical in all images.");
00061             goto the_end;
00062         }
00063     }
00064     for (i=0; i<nimg; i++) {
00065         if (in[i]->nbnd != 1) {
00066             MESSAGE('E'," All images must be single band.");
00067             goto the_end;
00068         }
00069     }
00070     
00071     
00072 
00073     radialavg = (PIXEL**) malloc(nimg*sizeof(PIXEL  * ));
00074     if (radialavg == (PIXEL **) NULL){
00075         
00076         printf("No memory for radialavg\n");
00077         goto the_end;
00078     
00079     } else {
00080 
00081         for  (i = 0; i < nimg; i++){
00082             radialavg[i] = (PIXEL  *) calloc(11, sizeof(PIXEL));
00083             if (radialavg[i] == (PIXEL *) NULL){
00084                 printf ("No memory for element %d\n",radialavg[i]);
00085                 goto the_end;
00086             }
00087         }
00088         
00089     }
00090     
00091     
00092     if (progmeter) {
00093         localpercent = 0.9;
00094         localprogress = 0.0;
00095         count = 0;
00096         total = nimg*(22+(49*49));
00097         increment = rint((double)total/25.0);
00098     }
00099     for (i=0; i<nimg; i++) {
00100     
00101         for (j=0; j<11; j++) {
00102             if (progmeter) {
00103                 count++;
00104                 if ((count%increment) == 0) {
00105                     localprogress = ((double)count/(double)total)*100.0;
00106                     cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00107                     if (cancel != 0) goto the_end;
00108                 }
00109             }
00110 
00111             count_array[j] = 0;
00112         }
00113     
00114         PERIODOGRAM(in[i],&periodogram);
00115         
00116         
00117         
00118         /* Only look at the 14/256 to 24/256 cycles/pixel frequency range */
00119         for (j=40; j<89; j++) {
00120             delta_y = (double)(j-64);
00121             for (k=40; k<89; k++) {
00122 
00123                 if (progmeter) {
00124                     count++;
00125                     if ((count%increment) == 0) {
00126                         localprogress = ((double)count/(double)total)*100.0;
00127                         cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00128                         if (cancel != 0) goto the_end;
00129                     }
00130                 }
00131 
00132                 delta_x = (double)(k-64);
00133 
00134                 radius = (int) rnd(sqrt((delta_x)*(delta_x) + (delta_y)*(delta_y)));
00135 
00136                 if ((radius <= 24) &&(radius >= 14)) {
00137                     //printf("%d: %d -- %f -- %f -- (%d,%d)\n",i,radius-14,radialavg[i][radius - 14],periodogram->data[0][j][k],j,k);
00138                     radialavg[i][radius - 14] += periodogram->data[0][j][k];
00139                     count_array[radius - 14] += 1;
00140                 }
00141             }
00142         }
00143         
00144         dcvalue = periodogram->data[0][64][64];
00145 
00146         for (j=0; j<11; j++) {
00147             if (progmeter) {
00148                 count++;
00149                 if ((count%increment) == 0) {
00150                     localprogress = ((double)count/(double)total)*100.0;
00151                     cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00152                     if (cancel != 0) goto the_end;
00153                 }
00154             }
00155 
00156             radialavg[i][j] /= (PIXEL) count_array[j];
00157             radialavg[i][j] /= dcvalue;
00158         }
00159         
00160     }
00161     if (progmeter) {
00162         progress += localpercent * 100.0;
00163     }
00164     
00165     printf("Done creating periodograms...\n");
00166     
00167     /* Normalize the frequencies to one another */
00168     for (j=0; j<11; j++) {
00169         avgvalue = (PIXEL)0.0;
00170         
00171         for (i=0; i<nimg; i++) {
00172             avgvalue += radialavg[i][j];
00173         }
00174 
00175         avgvalue /= (PIXEL)nimg;
00176 
00177         for (i=0; i<nimg; i++) {
00178             radialavg[i][j] /= avgvalue;
00179         }
00180     }
00181     
00182     printf("Done normalizing frequencies...\n");
00183 
00184     /* Find the best focused image */
00185     *bestfocus = 0;
00186     maxvalue = (PIXEL) -99999.0;
00187     for (i=0; i<nimg; i++) {
00188         value = (PIXEL)0.0;
00189     
00190         for (j=0; j<11; j++) {
00191             value += radialavg[i][j];
00192         }
00193         
00194         if (value > maxvalue) {
00195             *bestfocus = i;
00196             maxvalue = value;
00197         }
00198         
00199         printf("image %d: %f\n",i,value);
00200     }
00201 
00202     if (progmeter) {
00203         progress = 100.0;
00204         cancel = ProgMeter_Update(progmeter,progress);
00205         if (cancel != 0) goto the_end;
00206     }
00207     printf("Image %d has the best focus.\n",*bestfocus);
00208 
00209     the_end:
00210     if(radialavg) {
00211         /* Free Bresh Array */
00212         for (i=0; i < nimg; i++){
00213             if(radialavg[i]) {
00214                 free(radialavg[i]);
00215                 radialavg[i] = NULL;
00216             }
00217         }
00218 
00219         free(radialavg);
00220         radialavg = NULL;
00221     }
00222     
00223     if (periodogram) {
00224         RELMEM(periodogram);
00225     }
00226 
00227     if (progmeter) {
00228         ProgMeter_Destroy(progmeter);
00229     }
00230 
00231 
00232 }
00233 
00234 
00235 

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