示例#1
0
int main(void)
{
    byte* buffer;
    byte* pivots;
    size_t i;

    comb_init();  
    buffer = malloc(NUM_COMBS_ITER * COMB_SIZE);
    pivots = malloc(NUM_FILES * COMB_SIZE);

    for (i = 0; i < NUM_ITERS; i++)
    {
        printf("\nIteration %d of %d.\n\n", i + 1, NUM_ITERS);

        printf("Filling combinations buffer...");
        timer_reset();
        fill_comb_buffer(buffer);
        printf(" DONE in %fs.\n", timer_elapsed());

        printf("Sorting combinations buffer...");
        timer_reset();
        qsort(buffer, NUM_COMBS_ITER, COMB_SIZE, comb_sum_cmp);
        printf(" DONE in %fs.\n", timer_elapsed());

        if (i == 0)
        {
            printf("Creating output files...");
            create_output_files();
            printf(" DONE.\n");

            printf("Getting and saving pivots...");
            get_pivots(pivots, buffer);
            save_pivots(pivots);
            printf(" DONE.\n");
        }

        printf("Saving sorted combinations buffer...");
        timer_reset();
        save_comb_buffer(buffer, pivots);
        printf(" DONE in %fs.\n", timer_elapsed());
    }

    free(buffer);
    free(pivots);
    comb_free();

    return 0;
}
示例#2
0
文件: baseline.c 项目: timburrow/ovj3
int mathfunc() {
  float Baseline, X; 
  int pixel, im;
  char msg[1024];

  if (nbr_infiles<1 || input_sizes_differ || !want_output(0)){
    return FALSE;
  }

  if (nbr_outfiles != in_vec_len[0]) {
    sprintf(msg,"%s: The number of output images (%d) must match number of input images (%d)\n",
      PGM,nbr_outfiles,in_vec_len[0]);
    ib_errmsg(msg);
    return FALSE;
  }
  
  create_output_files(nbr_infiles, in_object[0]);

  /* Loop through every pixel */
  for (pixel = 0; pixel < img_size; pixel++){

    if (nbr_image_vecs < 2)
      Baseline = in_data[0][pixel];
    else {
      Baseline = 0;
      for (im = vecindx[1]; im < vecindx[1]+in_vec_len[1]; im++)
        Baseline += in_data[im][pixel];
      Baseline /= in_vec_len[1];
    }

    /* Loop through all images in input vector */
    for (im = 0; im < nbr_infiles; im++){
      X = in_data[im][pixel];
    
      if (want_output(im)){
        out_data[im][pixel] = X - Baseline;
      }
    
    }  /* end of image loop */
  }  /* end pixel loop */
  return TRUE;
}
示例#3
0
文件: fit.c 项目: timburrow/ovj3
int
mathfunc()
{
    int i;
    double val;
    double *xvars;		/* Values of the independent variable(s) */
    int nvars;			/* Nbr of independent variables */
    int np = 0;			/* Number of numerical parameters found */
    double threshold = 0;	/* Ignore pixels below this intensity */
    double sigLev = 1; // Significance level to set pixel's fit value (1=no significance)
    double chisq = 0; // Chi-square -- alternative to sigLev, if set
    double snThresh = 0; // min S/N to set value of parameter pixel
    int nparams = 0;		/* Nbr of parameters in fit */
    char *xname = "ti";
    char msg[256];
    char *str;

    int quick = FALSE;
    int noderiv = FALSE;
    int gotfun = FALSE;
    int fit_type = NONLINEAR;
    int use_prev_params = FALSE;
    int prev = FALSE;
    int noprev = FALSE;
    int pdone;

    void (*function)() = NULL;
    void (*jacobian)() = NULL;
    int (*guess)() = NULL;
    int (*parfix)() = NULL;
    int (*method)() = NULL;
    double *(*xvarfunc)() = set_xvars;

    int arg = 2;
    extern double d1mach_();

    if (in_vec_len[0]<1){
	ib_errmsg("MATH: fit: No input images");
	return FALSE;
    }
    if (input_sizes_differ){
	ib_errmsg("MATH: fit: Input image sizes differ");
	return FALSE;
    }
    if (!want_output(0)){
	ib_errmsg("MATH: fit: No frame for first output image");
	return FALSE;
    }

    /* Read numerical parameters (nothing to do with params of the fit!) */
    pdone = FALSE;
    for (i=0; i<nbr_params && !pdone; i++){
	val = in_params[i];
	switch (i){
	  case 0:
	    threshold = val;
	    pdone = TRUE;	/* Last parameter to read */
	    break;
	}
    }
    nbr_params -= i;
    in_params += i;

    /* Read string parameters */
    gotfun = FALSE;
    for (i=0; i<nbr_strings; i++){
	str = in_strings[i];
	if (!gotfun && getfunction(str, &nparams, &use_prev_params, &fit_type,
				   &function, &jacobian, &guess, &parfix))
	{
	    /* Got a functional form */
	    gotfun = TRUE;
	}else if (!quick && strcasecmp(str,"quick") == 0){
	    /* Use "quick" mode */
	    quick = TRUE;
	}else if (!noderiv && strcasecmp(str,"noderiv") == 0){
	    /* Do not use derivative, even if available */
	    noderiv = TRUE;
	}else if (!prev && strcasecmp(str,"prev") == 0){
	    /* Use previous parameter values for estimates */
	    prev = TRUE;
	}else if (!noprev && strcasecmp(str,"noprev") == 0){
	    /* Do not use previous parameter values for estimates */
	    noprev = TRUE;
	} else if (strncasecmp(str, "p=", 2) == 0) {
	    val = atof(str+2);
	    if (val != 0) {
	        val = val < 1e-20 ? 1e-20 : (val > 1 ? 1 : val);
	        sigLev = val;
	    }
        } else if (strncasecmp(str, "chisq=", 6) == 0) {
            val = atof(str+6);
            if (val != 0) {
                chisq = val;
            }
        } else if (strncasecmp(str, "snThresh=", 9) == 0) {
            val = atof(str+9);
            if (val != 0) {
                snThresh = val;
            }
	}else{
	    /* None of the above--assume independent variable name */
	    xname = str;
	}
    }

    /* Do not write to more output files than we can usefully use */
    if (nparams){
	int maxout;
	maxout = 2 * nparams + 1;
	if (maxout<nbr_outfiles) nbr_outfiles = maxout; /* Change global var */
    }
    create_output_files(nbr_outfiles, in_object[0]);

    /* Check the setup */
    if (!gotfun){
	ib_errmsg("MATH: fit: No known fit type specified");
	return FALSE;
    }

    if (noderiv){
	jacobian = NULL;
    }

    if (prev){
	use_prev_params = TRUE;
    }else if (noprev){
	use_prev_params = FALSE;
    }

    if (quick || !function){
	method = NULL;
    }else{
	method = marquardt;
    }

    /* Set the independent variable */
    xvars = (*xvarfunc)(in_object, in_vec_len[0], xname, &nvars);
    if (!xvars){
	sprintf(msg,"MATH: No values for independent variable \"%.200s\"",
                xname);
	ib_errmsg(msg);
	return FALSE;
    }

    if (chisq == 0) {
        chisq = chisqCompInv(sigLev, in_vec_len[0] - nparams + 1);
    }

    fit_images(in_object, in_vec_len[0], xvars, nvars,
	       threshold, chisq, snThresh, img_width, img_height, img_depth,
	       out_object, nbr_outfiles, want_output, fit_type,
	       nparams, use_prev_params,
	       function, jacobian, method, guess, parfix);

    write_output_files();

    return TRUE;
}
示例#4
0
文件: stats.c 项目: timburrow/ovj3
int mathfunc() {
  double sum, sum_x2, mean, std, min, max, X, thr; 
  double av_mean,av_std, av_pp, max_std, min_std, max_pp, min_pp;
  float  fract;
  float  **tmp_data;
  int    pixel, im, N, Nfilter;
  int    r,c, r0, c0, r1, r2, c1, c2;
  int    rad, d, do_filter;
  int    n1, n2, n3, n4;
  char   msg[1024],pgm[1024];
  enum  {nstdp, npp, nmean, nstd};  /* output images 0,1,2,3 */
  
  FILE *fp;
    
  if (nbr_infiles<1 || input_sizes_differ || !want_output(0))
    return FALSE;

  create_output_files(4, in_object[0]);

  /*************************************/
  /* Get input parameters              */
  /*************************************/
  /* Comment */
  strcpy(pgm,"stats");
  if (nbr_strings > 0) {
    strcpy(pgm,in_strings[0]);
  }

  /* Filter size */
  if ((img_width < 128) || (img_height < 128))
    Nfilter = 5; /* for small matrices, default to a 5x5 filter */
  else
    Nfilter = 11;  /* else default to a 11x11 filter */
  do_filter = 1;
  if (nbr_params > 0)
    Nfilter = in_params[0];  /* filter kernel size */

  /* How much of the image should we use? */
  if (nbr_params > 1)
    fract = in_params[1]/100;
  else
    fract = 0.80; /* default to 80% */


  /* Zero center pixels in output images? */
  if (nbr_strings > 1) {
    if (!strcmp("nodc",in_strings[1])) {
      r1 = r2 = c1 = c2 = -1;
      n1 = n2 = n3 = n4 = -1; /* default to zero'ing center pixel */
    }
  }
  else {
    /* Four pixels in center of image - avoid DC artifact */
    r1 = img_height/2-1;
    r2 = r1 + 1;
    c1 = img_width/2-1;
    c2 = c1 + 1;

    n1 = (r1-1)*img_width + c1;
    n2 = (r1-1)*img_width + c2;
    n3 = (r2-1)*img_width + c1;
    n4 = (r2-1)*img_width + c2;

  }

  /* Get threshold for segmenting image */
  thr = threshold(in_data[0],img_height,img_width);
  /* Find object based on first image */
  find_object(in_data[0],thr,img_height,img_width,
      &r0,&c0,&r1,&c1,&r2,&c2,&rad);

  /* Optional filtering of input images */
  if (Nfilter > 0) { /* Filter all images */
    do_filter = 1;      
    if ((tmp_data = (float **) malloc(sizeof(float *)*nbr_infiles)) == NULL)
      ib_errmsg("MALLOC ERROR 1");      
    for (im = 0; im < nbr_infiles; im++)
      if ((tmp_data[im] = (float *) malloc(sizeof(float)*img_size)) == NULL)
        ib_errmsg("MALLOC ERROR 2");      

    for (im = 0; im < nbr_infiles; im++)
      filter(in_data[im],tmp_data[im],Nfilter,img_height,img_width,thr);
  }

  /* Initialize output image */
  for (r = 0; r < img_height; r++) {
    for (c = 0; c < img_width; c++) {
      pixel = r*img_width + c;

      if (want_output(nmean)) out_data[nmean][pixel] = 0;
      if (want_output(nstd))  out_data[nstd][pixel]  = 0;
      if (want_output(nstdp)) out_data[nstdp][pixel] = 0;
      if (want_output(npp))   out_data[npp][pixel]   = 0;
    }
  }


  /* Loop through every pixel */
  av_std = av_pp = 0;
  
  for (r = r1; r < r2; r++){
    for (c = c1; c < c2; c++){
      pixel = r*img_width + c;
      d = (int) sqrt((double) ((r-r0)*(r-r0) + (c-c0)*(c-c0)));  /* distance from center */

      sum = sum_x2  = 0.0;
      min = 1e6; 
      max = 0.0;
      mean = std = 0;
      N = 0;
      
      if ((pixel != n1) && (pixel != n2) && (pixel != n3) && (pixel != n4)) {  /* skip center pixels */
        if (in_data[0][pixel] > thr) {
          /* Loop through all images in input vector */
          for (im = 0; im < nbr_infiles; im++) {
            if (do_filter)
              X = tmp_data[im][pixel];
            else
              X = in_data[im][pixel];

            sum    += X;
            sum_x2 += (X*X);
      
            if (X < min) min = X;
            if (X > max) max = X;
            
            N++;
          }  /* end of image loop */
          mean = sum/N;
          std  = (double) sqrt((double)((sum_x2/N) - (mean*mean)));

        }  /* End check threshold */
      } /* End check center pixel */

/* DEBUG
if (((r == 37) && (c == 28)) || ((r == 32) && (c == 25))){
  printf("At (%d,%d), max, min, mean, pp, std is %f, %f, %f, %f, %f\n",
    c,r,max, min, mean, (max - min)/mean*100.0,std);
  printf("sum, sumsq, sumsq/N, mean^2, sqr = %f, %f, %f, %f, %f, N = %d\n",
    sum,sum_x2*1000,sum_x2/N*1000*1000, mean*mean*1000*1000,(sum_x2/N - mean*mean)*1000*1000,N);
}
/**/
      if (want_output(nmean)) out_data[nmean][pixel] = mean;
      if (want_output(nstd))  out_data[nstd][pixel]  = std;
      if (want_output(nstdp)) {
        if (mean != 0)
          out_data[nstdp][pixel] = std/mean*100;
        else
          out_data[nstdp][pixel] = 0;
      }
      if (want_output(npp)) {
        if (mean != 0)
          out_data[npp][pixel] = (max - min)/mean*100.0;     
        else
          out_data[npp][pixel] = 0;
      }
    } /* end columns loop */  
  }  /* end rows loop */


  /* Get average standard deviation and PP within image */
  av_std = av_pp = 0; 
  max_std = max_pp = 0;
  min_std = min_pp = 1e6;
  N = 0;
  for (r = 0; r < img_height; r++){
    for (c = 0; c < img_width; c++){
      pixel = r*img_width + c;
      d = (int) sqrt((double) ((r-r0)*(r-r0) + (c-c0)*(c-c0)));  /* distance from center */
      if ((pixel != n1) && (pixel != n2) && (pixel != n3) && (pixel != n4)) {  /* skip center pixels */
        if ((d < rad) && (in_data[0][pixel] > thr)) {
	  N++;
          X = out_data[nstdp][pixel];
          av_std += X;
          if (max_std < X) max_std = X;
          if (min_std > X) min_std = X;

          if (want_output(npp)) {
            X = out_data[npp][pixel];
            av_pp += X;
            if (max_pp < X) max_pp = X;
            if (min_pp > X) min_pp = X;
          }
        }
      }
    }
  }
	

  if (N > 0) {
    printf("=========== STATS: %s ===================\n",pgm);
    printf("Average standard deviation in %d pixels is %.2f%%\n",N,av_std/N);
    printf("Standard deviations range from %.2f%% to %.2f%%\n",min_std,max_std);
    if (want_output(npp)) {
      printf("Average peak-to-peak is %.2f%%\n",av_pp/N);
      printf("Peak-to-peak range from %.2f%% to %.2f%%\n",min_pp,max_pp);
    }


    if ((fp = fopen("STAB_measurements.txt","a")) == NULL) {
      sprintf(msg,"Can't open STAB_measurements.txt for printing results");
      ib_errmsg(msg);
      return FALSE;
    }
    fprintf(fp,"=========== STATS: %s ===================\n",pgm);
    fprintf(fp,"Average standard deviation in %d pixels is %.2f%%\n",N,av_std/N);
    fprintf(fp,"Standard deviations range from %.2f%% to %.2f%%\n",min_std,max_std);
    if (want_output(npp)) {
      fprintf(fp,"Average peak-to-peak is %.2f%%\n",av_pp/N);
      fprintf(fp,"Peak-to-peak range from %.2f%% to %.2f%%\n",min_pp,max_pp);
    }
    fclose(fp);

  }

  return TRUE;
}
示例#5
0
int mathfunc() {
  double meanS, noise, meanG, maxS, minS, sum2, stdS, thr, X, minmax;
  int   pixel, pixel2, im, n1, n2, r, c, r1, c1, rG, cG, rS1, rS2, cS1, cS2;
  int   im_r1, im_r2, im_c1, im_c2, r0, c0, rad, d,
        Nfilter, NF2, N, skip_pix, pixelG;
  double fract;
  char msg[1024],pgm[1024];
  FILE  *fp;

  if (nbr_infiles<1 || input_sizes_differ){
    return FALSE;
  }


  if (nbr_outfiles != in_vec_len[0]) {
    sprintf(msg,"Math: You must supply as many output images as input images\n");
    ib_errmsg(msg);
    return FALSE;
  }


  /**************************/
  /* Create output images ***/
  /**************************/
  if (want_output(1))
    create_output_files(nbr_infiles*2, in_object[0]);
  else
    create_output_files(nbr_infiles, in_object[0]);

  /* How large a (mean) filter do we apply to find the ghosting level */
  if ((img_width < 128) || (img_height < 128))
    Nfilter = 5; /* for small matrices, default to a 5x5 filter */
  else
    Nfilter = 11;  /* else default to a 11x11 filter */
  if (nbr_params > 0) Nfilter = (int) in_params[0];
  NF2 = (int) (Nfilter/2);

  fract = 0.8;
  if (nbr_params > 1)
    fract = in_params[1]/100;

  strcpy(pgm,"SNR");
  if (nbr_strings > 0)
    strcpy(pgm,in_strings[0]);

  /**************************/
  /* Calculations ***********/
  /**************************/
  /* Get threshold for segmenting image */
  thr = threshold(in_data[0],img_height,img_width);

  /* Find image boundaries (radius) *********/
  find_object(in_data[0],thr,img_height,img_width,
    &r0,&c0,&im_r1,&im_c1,&im_r2,&im_c2,&rad);
  /* rad is the smallest radius; im_r1/c1/r2/c2 gives maximum extent of object */

  /* Find noise standard deviation **************/
  noise = find_noise(in_data[0],im_r1-3, im_c1-3, im_r2+3, im_c2+3);


  for (im = 0; im < nbr_infiles; im++){

    /* Apply a NxN mean filter to the image */
    filter(in_data[im],out_data[im],Nfilter,img_height,img_width,thr);

    /***********************************************/
    /* Calculate signal intensity and uniformity ***/
    /***********************************************/
    meanS = sum2 = n2 = 0;
    maxS = 0; minS = 1e6;
    for (r = 0; r < img_height; r++) {
      for (c = 0; c < img_width; c++) {
        pixel = r*img_width + c;
        X = in_data[im][pixel];
        d = (int) sqrt((double) ((r-r0)*(r-r0) + (c-c0)*(c-c0)));  /* distance from center */
        if (d <= rad*fract) {
            meanS += X;
            sum2  += (X*X);
            n2++;

            minmax = out_data[im][pixel];
            /* Find max/min filtered signal intensity */
            if (maxS < minmax) {maxS = minmax; rS1=r; cS1 = c;}
            if (minS > minmax) {minS = minmax; rS2=r; cS2 = c;}

        }
      }
    }
    meanS /= n2;
    stdS  = (float) sqrt((double)((sum2/n2) - (meanS*meanS)));


    /******************************************/
    /* Calculate ghost intensity **************/
    /******************************************/
    /* Assume ghosting is in horizontal direction   */
    /* Search +/- Nfilter columns beyond maximum extent of object */
    meanG = 0;
    cG = rG = pixelG = 0;
    skip_pix = (Nfilter > 3 ? Nfilter : 3);
    for (r = im_r1-1; r <= im_r2; r++) {
      /* Check to the left of the image */
      for (c = skip_pix; c < im_c1-skip_pix; c++) {
        pixel = r*img_width + c;
        X = out_data[im][pixel];
        if (X > meanG) {
          meanG = X;
          rG = r; cG = c; pixelG = pixel;
        }
      }
      /* Check to the right of the image */
      for (c = im_c2+skip_pix; c < img_width-skip_pix; c++) {
        pixel = r*img_width + c;
        X = out_data[im][pixel];
        if (X > meanG) {
          meanG = X;
          rG = r; cG = c; pixelG = pixel;
        }
      }
    }

    for (r = 0; r < img_height; r++) {
      for (c = 0; c < img_width; c++) {
        pixel = r*img_width + c;
        X = in_data[im][pixel];
        d = (int) sqrt((double) ((r-r0)*(r-r0) + (c-c0)*(c-c0)));  /* distance from center */
        if (!(d <= rad*fract)) {
          out_data[im][pixel] = 0;
        }
      }
    }
    out_data[im][pixelG] = meanG;




    printf("=========== %s: image %d ===================\n",pgm,im+1);
    printf("Signal, Noise, Ghosting (x100): %.6f, %.6f, %.6f\n",maxS*100,noise*100, meanG*100);
    printf("SNR: %.f (NEMA standard: %.f)\n",maxS/noise, maxS/noise*1.253);
    printf("Ghosting: %.2f%% of max signal (in %dx%dROI)\n",(meanG-noise)/maxS*100,Nfilter,Nfilter);
    printf("Maximum ghosting is in pixel %d, %d\n",cG,rG);
    printf("Percent Image Uniformity is%.2f%%\n",(1-(maxS-minS)/(maxS+minS))*100);
    printf("minS, maxS = %f and %f at (%d,%d),(%d,%d)\n",minS*100,maxS*100,cS2,rS2,cS1,rS1);
/*    printf("Image variation is %.f%%\n",stdS/meanS*100); */

    if ((fp = fopen("SNR_measurements.txt","a")) == NULL) {
      sprintf(msg,"Can't open file SNR_measurements.txt for printing results");
      ib_errmsg(msg);
      return FALSE;
    }
    fprintf(fp,"=========== %s: image %d ===================\n",pgm,im+1);
    fprintf(fp,"Signal, Noise, Ghosting (x100): %.6f, %.6f, %.6f\n",maxS*100,noise*100, meanG*100);
    fprintf(fp,"SNR: %.f (NEMA standard: %.f)\n",maxS/noise, maxS/noise*1.253);
    fprintf(fp,"Ghosting: %.2f%% of max signal (in %dx%dROI)\n",(meanG-noise)/maxS*100,Nfilter,Nfilter);
    fprintf(fp,"Maximum ghosting is in pixel %d, %d\n",cG,rG);
    fprintf(fp,"Percent Image Uniformity is%.2f%%\n",(1-(maxS-minS)/(maxS+minS))*100);
    fprintf(fp,"minS, maxS = %f and %f at (%d,%d),(%d,%d)\n",minS*100,maxS*100,cS2,rS2,cS1,rS1);
    fclose(fp);

  }  /* end image loop */

  return TRUE;
}