/***************************************************************************** - - - POLONY SEQUENCER ACQUISITION SUITE - - Church Lab - - Harvard Medical School - - - - Free software for running a Polony Sequencing automated microscope - - - - ========================================================================= - - - - - - autoexpose.cpp - - - - Program to perform automated exposure and sensitivity setting. Adjusts - - exposure and sensitivity to bring the background value for each channel - - independently to a pre-determined value. These pre-determined values - - have been manually selected to minimize interference between channels. - - In computing the intensity distribution for each channel, only pixels - - corresponding to beads are used. - - - - Written by Greg Porreca, 02-15-2006 - - - - Revised - - - - - - This software may be used, modified, and distributed freely, but this - - header may not be modified and must appear at the top of this file. - - - - - *****************************************************************************/ #include "Autoexpose.h" #include Autoexpose::Autoexpose(Reporter* rep, CHamCam* cHamCam, CFilter* cFilter, CScope* cScope, CPifoc* cPifoc, Acquisition* acquis, short unsigned int *bf_img){ r = rep; this_camera = cHamCam; this_filter = cFilter; this_te2000 = cScope; this_pifoc = cPifoc; acq = acquis; bf_thresh = 7500; ae_sens = 95; min_exp = 30; num_exp = 9; step_exp = 20; } Autoexpose::~Autoexpose(){;} void Autoexpose::autoexpose(double *exposure, double *sensitivity, short unsigned int *bf_img, int fluorophores, char *cyclename){ char debugimgfilename[500]; int num_colors = 4; // Deal with image memory short unsigned int **image_ptr_stack; if(image_ptr_stack = (short unsigned int**)malloc(num_exp*4)==NULL){ r->error("allocate memory for autoexpose pointer stack failed", 1); } brightfield_img = (short unsigned int*)malloc(1000000*sizeof(short unsigned int)); for(int i=0; i<100000; i++){ *(brightfield_img+i) = *(bf_img + i); } //UNCOMMENT THE NEXT LINE IF YOU WANT TO SAVE DEBUGGING IMAGES strcpy(debugimgfilename, ""); sprintf(debugimgfilename, "%d_000000", 0); save_image(brightfield_img, debugimgfilename, cyclename); // //SET UP CAMERA FOR THE CORRECT NUMBER OF EXPOSURES if(!this_camera->SetupMultiple(num_exp*num_colors)){ r->error("autoexpose: could not setup camera for acquisition", 1); } if(!this_camera->QueueCapture()){ r->error("autoexpose: queue capture of multiple frames failed", 0); } //Iterate over all exposure/fluorophore combinations for(j=0; jacquire_expose_shutter(min_exp + (i*step_exp), ae_sens)){ sprintf(log_string, "Error acquiring image %d, fluorophore %d in fluorescence autofocus stack", i, j); r->error(log_string, 0); } else{ image_ptr_stack[(j*num_exp) + i] = acq->acquire_getdata((j*num_exp) + i); } } } strcpy(debugimgfilename, ""); sprintf(debugimgfilename, "%d_%03d%03d", fluorophore, (int)expose_num, (int)sens_num); save_image(image_ptr_stack[(i*num_sens)+j], debugimgfilename, cyclename); if(!this_camera->EndMultiple()){ r->error("autoexpose: could not end multiple acquisition", 1); } r->log("autoexposure: assigning exposure variable in autoexpose",5); *exposure = max_exp; r->log("autoexposure: assigning sensitivity variable in autoexpose",5); *sensitivity = max_sens; } int Autoexpose::getPeak(int *hist, int num_bins){ int curr_binval = 0; int curr_bin = 0; for(int i=0; i curr_binval) && (i < 16000)){ curr_bin = i; curr_binval = *(hist + i); } } if(curr_bin == 0){ r->error("ERROR: getPeak in autoexpose is finding peak at pixel intensity 0",0); } return curr_bin; } void Autoexpose::calchist(short unsigned int *image, int *hist){ for(int i=0; i<16384; i++){ *(hist + i) = 0; } num_nonzero_bf_pixels = 0; for(int j=0; j<1000000; j++){ if(*(brightfield_img+j) > bf_thresh){ (*(hist + (*(image + j))))++; num_nonzero_bf_pixels++; } } sprintf(log_string, "Current brightfield image has %d above thresh pixels.", num_nonzero_bf_pixels); r->log(log_string, 4); } void Autoexpose::dumptofile(int *histogram, double curr_exp, double curr_sens, char *cyclename){ FILE *debugfile; int i; char debugfilename[500]; //OPEN TEMP DEBUGFILE strcpy(debugfilename, "autoe_"); strcat(debugfilename, cyclename); strcat(debugfilename, ".dat"); if((debugfile = fopen(debugfilename, "a")) == NULL){ fprintf(stderr, "Problem opening exposure info file %s. \n", debugfilename); } fprintf(debugfile, "%d\t%d", (int)(curr_exp * 1000), (int)(curr_sens)); for(i=0; i<16384; i++){ fprintf(debugfile, "\t%d", *(histogram + i)); } fprintf(debugfile, "\n"); fclose(debugfile); } bool Autoexpose::save_image(short unsigned int *curr_img, char *filename, char *cyclename){ char img_output_fullname[500]; FILE *img_outfile; strcpy(img_output_fullname, ""); strcat(img_output_fullname, cyclename); strcat(img_output_fullname, "_"); strcat(img_output_fullname, filename); strcat(img_output_fullname, ".raw"); if((img_outfile = fopen(img_output_fullname, "w+b"))==NULL){ sprintf(log_string, "ERROR opening raw output file %s", img_output_fullname); r->error(log_string, 0); return false; } fwrite(curr_img, 2, 1000000, img_outfile); fclose(img_outfile); return true; }