def snapUVPNG(exposure, gain,filename): """ snaps an image and saves the 14bit image as an 8 bit greyscale PNG by shifting out 6 bits """ global img_array global img_array_float global frames global beadpos_xcol global beadpos_yrow global num_beads for i in range(frames): PC.py_snapPtr(img_array, exposure,gain,'spare') img_array_float += img_array.astype(np.float) # sum accumulator # end for img_array_out = (img_array_float/frames).astype(np.uint16) img_array_float *= 0 # reset accumulator img_array_out.tofile(filename+'.raw') image_8bit = (img_array_out >> 6).astype(np.uint8) im = Image.fromarray(image_8bit.reshape((1000,1000)),'L') PC.cameraClose() # also frees up image buffer memory im.save(filename + ".png", "png")
def snapPicPNG(exposure, gain, color, filename): """ snaps an image and saves the 14bit image as an 8 bit greyscale PNG by shifting out 6 bits """ global img_array global img_array_float global frames global beadpos_xcol global beadpos_yrow global num_beads global my_color for i in range(frames): PC.py_snapPtr(img_array, exposure,gain,color) img_array_float += img_array.astype(np.float) # sum accumulator # end for img_array_out = (img_array_float/frames).astype(np.uint16) img_array_float *= 0 # reset accumulator img_array_out.tofile(filename+'.raw') image_8bit = (img_array_out >> 6).astype(np.uint8) im = Image.fromarray(image_8bit.reshape((1000,1000)),'L') PC.cameraClose() # also frees up image buffer memory im.save(filename + ".png", "png") if num_beads != 0 and my_color == color: print("value at 1st point: %d" % \ img_array_out[beadpos_xcol[0]+1000*beadpos_yrow[0]]) print("value at 2nd point: %d" % \ img_array_out[beadpos_xcol[1]+1000*beadpos_yrow[1]]) print("value at 3rd point: %d" % \ img_array_out[beadpos_xcol[2]+1000*beadpos_yrow[2]]) # end if del img_array_out del image_8bit del im
img_array = np.empty(data_size, dtype=np.uint16) img_array2 = np.empty(data_size, dtype=np.uint16) beadpos_xcol = np.empty(MAX_BEADS_PERFRAME, dtype=np.uint16) beadpos_yrow = np.empty(MAX_BEADS_PERFRAME, dtype=np.uint16) segmask_image = np.empty(data_size, dtype=np.uint16) MaestroF.darkfield_off() MaestroF.filter_home() MaestroF.filter_goto(my_color) time.sleep(1) MapFunc.init_illum() MapFunc.light_all() PC.py_snapPtr(img_array, .35,80,my_color) #invert for images where beads are lighter than background img_array = 16383 - img_array # invert the 14 bit image num_beads = FO.find_objects(1000, 1000, \ img_array, \ beadpos_xcol, beadpos_yrow, \ segmask_image) print("The number of beads found: %d" % num_beads) MapFunc.convertPicPNG2(16383-img_array,0,1) MapFunc.convertPicPNG2(segmask_image*65535, 0,2) #MapFunc.convertPicPNG(segmask_image*65535+img_array, 0,3) print("phase ONE complete!!!!!!!!!!!!!") PC.cameraClose() # also frees up image buffer memory
def find(): """ This function: 1) Takes all images, saves with labels "_before" for all 2) Then finds all the objects of one color "mycolor", a) takes the images averaging over "frames" images a) finds all objects b) prints out a bunch of bead position values found in the image 3) creates an overlay image putting the "mycolor" object centroids on top of the raw images of the composite of all colors and saves a png of 'colorsnap-'+'find_overlay_' + my_color +'_before' + ".png """ # declare all globals for function global img_array global img_array_float global img_array_flat global beadpos_xcol global beadpos_yrow global segmask_image global exposures global gains global colors global frames global num_beads global my_color global my_color_ind global MapFunc global MaestroF num_beads = 0 MaestroF.darkfield_off() """ DO NOT Home the filter wheel!!! In between sequencing and releasing This destroys the mapping, since the filter whell cannot reapeatedly home To the same postion """ #MaestroF.filter_home() snapAllPics(exposures, gains, "_before") MaestroF.filter_goto(my_color) time.sleep(1) for i in range(frames): PC.py_snapPtr(img_array, \ exposures[my_color_ind], \ gains[my_color_ind] , \ my_color) img_array_float += img_array.astype(np.float) # end for img_array_out = (img_array_float/frames).astype(np.uint16) img_array_float *= 0 # reset accumulator #invert for images where beads are lighter than background IT.flatten_image(1000, 1000, img_array_out, img_array_flat,1) img_array_flat = 16383 - img_array_flat # invert the 14 bit image num_beads = FO.find_objects(1000, 1000, \ img_array_flat, \ beadpos_xcol, beadpos_yrow, \ segmask_image) print("The number of beads found: %d" % num_beads) MapFunc.convertPicPNG2(16383-img_array_flat,0,1) MapFunc.convertPicPNG2(segmask_image*65535, 0,2) print("phase ONE complete!!!!!!!!!!!!!") print("value at 1st point: %d at point %d, %d" % \ (img_array_out[beadpos_xcol[0]+1000*beadpos_yrow[0]], \ beadpos_xcol[0], beadpos_yrow[0])) print("value at 2nd point: %d at point %d, %d" % \ (img_array_out[beadpos_xcol[1]+1000*beadpos_yrow[1]], \ beadpos_xcol[1], beadpos_yrow[1])) print("value at 3rd point: %d at point %d, %d" % \ (img_array_out[beadpos_xcol[2]+1000*beadpos_yrow[2]], \ beadpos_xcol[2], beadpos_yrow[2])) #PC.cameraClose() # also frees up image buffer memory stop_release() """ imG = Image.open("map_0_1" +".png") # just beads imB = Image.open("map_0_2" +".png") # the found beads im = Image.new('RGB', (1000,1000)) pix = im.load() pixG = imG.load() pixB = imB.load() for i in range(1000): for j in range(1000): pix[i,j] = (0,pixG[i,j],pixB[i,j]) #px = -238501 # end for # end for im.save("color_overlay_orig" +".png", "png") """ """ saves the pretaken raw 14bit image as an 8 bit greyscale PNG by shifting out 6 bits """ shape = (1000, 1000) img_1D_list = [] for c in colors: img_file = open('colorsnap-'+c+'_before' +'.raw', 'rb') # load a 1000000 length array img_array_1D = np.fromfile(file=img_file, dtype=np.uint16) img_1D_list.append(img_array_1D.reshape(shape)) img_file.close() del img_array_1D # end for # R = cy5, G = cy3, B = txRed # yellow = fam ==> RGB:255,255,0 img_total = (img_1D_list[1] + \ img_1D_list[2] + \ img_1D_list[3]).clip(min=None,max=16383).astype(np.uint16) img_3D = np.dstack([img_total, \ 16383*segmask_image.reshape(shape), \ img_1D_list[my_color_ind]]) img_8bit = (img_3D >> 6).astype(np.uint8) #im = Image.fromarray(img_8bit.reshape(-1,1000*3),'RGB') im = Image.fromarray(img_8bit,'RGB') im.save('colorsnap-'+'find_overlay_' + my_color +'_before' + ".png", "png") # clean house del img_array_out del img_total del img_3D del img_8bit del im del shape del img_1D_list
def mapGen(self, test_me = False): """ This function generates the mapping it: 1) loads the points to illuminate 2) generates a image for the DMD based on said coordinates 3) illuminates the image 4) takes a picture 5) copies the picture from the frame buffer 6) finds the illuminated points in the camera coordinates 7) generates a mapping to the DMD using a pseudoinverse 8) writes the mapping to class memory 9) writes the mapping to file Polonator_IlluminateFunctions is SWIGed as follows Illuminate and Hardware coordinates are mapped using the SWIG This allows you to access an array of Coordinates in C and python as follows once the IlluminateCoords_type struct is also interfaced in SWIG : new_coordArray(int size) -- creates an array of size delete_coordArray(IlluminateCoords_type * arry) -- deletes and array coordArray_get_x(IlluminateCoords_type *c_xy, int i) -- gets an value coordArray_set_x(IlluminateCoords_type *c_xy, int i, signed short int val) -- sets a value ptr_coordArray(IlluminateCoords_type *c_xy, int i) returns a pointer to an index """ """ get points for bitmap and load them SWIG style mapping_basis is just a list of points to to illuminate to generate a good and sufficient mapping basis coordinates are given as a range from -1 to 1 with 0,0 being the image center """ MF = self.MF print("STATUS:\tMappingFunctions: opening frame buffer\n") mapping_basis = open(MF.config_dir +'/mapping_basis.coordinates') print( "STATUS:\tMappingFunctions: getting list of points \ to illuminate from file\n") idx = -1 num_points = 0 for line in mapping_basis: if line[0] != '#': # '#' is reserved for comments on a separate line basis_coordinate = line.split() # tab delimited fields print(basis_coordinate) if basis_coordinate[0] == 'number': # this is the number of basis points we need to illuminate num_points = int(basis_coordinate[1]) idx = num_points # this creates the array of coordinates to illuminate points_to_illum_x = numpy.empty(idx, dtype=numpy.int32) # this is the array containing the found points on the CCD points_found_x = numpy.empty(idx, dtype=numpy.int32) points_to_illum_y = numpy.empty(idx, dtype=numpy.int32) points_found_y = numpy.empty(idx, dtype=numpy.int32) # write coordinates in reverse idx = idx - 1 print("number of Points to Illuminate: " + str(num_points)) elif (basis_coordinate[0] == 'XY') and (idx > -1): # make sure its tagged as a coordinate and the index is positive # scale to dimensions of the DMD bc_x = int( float(MF.IlluminateWidth)/2 \ *float(basis_coordinate[1]) ) \ + (float(MF.IlluminateWidth)-1)/2 bc_y = int( float(MF.IlluminateHeight)/2 \ *float(basis_coordinate[2]) ) \ + (float(MF.IlluminateHeight)-1)/2 print("read(%d,%d)\n" % (bc_x, bc_y)) points_to_illum_x[idx] = bc_x points_to_illum_y[idx] = bc_y idx = idx - 1 #end if #end if #end for """ Set up imaging """ MaestroF = MF.MaestroF MaestroF.darkfield_off() MaestroF.filter_home() MaestroF.filter_goto("spare") time.sleep(1) #MaestroF.filter_unlock() """ set up release hardware """ #self.illumInit() num_sub = 0 # keeps track of not found points data_size = 1000000 img_array = numpy.empty(data_size, dtype=numpy.uint16) #img_array_out = numpy.empty(data_size, dtype=numpy.uint16) img_array_float = numpy.zeros(data_size, dtype=numpy.float) for idx in range(num_points): # generate bitmap print("STATUS:\tMappingFunctions: clearing frame buffer\n") PI.py_clear_framebuffer() print("STATUS:\tMappingFunctions: creating mask\n") print("STATUS:\tMappingFunctions: generating one image of with ' + \ 'one pixel\n") PI.py_illuminate_point( int(points_to_illum_x[idx]),\ int(points_to_illum_y[idx]),\ MF.mask_number0) # illuminate just ONE spot print("STATUS:\tMappingFunctions: illuminating one point\n") print("illuminating (%d,%d)\n" % \ (int(points_to_illum_x[idx]), int(points_to_illum_y[idx]))) PI.py_illuminate_expose() # turn on the frame buffer #time.sleep(1) # analyze image for centroid print("STATUS:\tMappingFunctions: taking picture \ of one illuminated point\n") # take a picture and get a pointer to the picture frames = 5 for i in range(frames): PC.py_snapPtr(img_array, MF.expose,MF.gain,"spare") # sum accumulator img_array_float += img_array.astype(numpy.float) # end for # average over frames img_array_out = (img_array_float/frames).astype(numpy.uint16) img_array_float *= 0 # reset accumulator self.convertPicPNG(img_array_out, \ points_to_illum_x[idx], points_to_illum_y[idx]) # read in the config file. must be formated correctly print("STATUS:\tMappingFunctions: find illuminated point\n") # SWIGged function for finding the centroid # doing array[x:x+1] returns a pointer to an index x in an array # essentially in numpy a = points_found_x[idx:idx+1] b = points_found_y[idx:idx+1] max_val = PI.py_illuminate_spot_find(img_array_out,a,b) if max_val < 100: print("point not found") num_sub += 1 # end if else: print("found(%d,%d)" % \ (points_found_x[idx],points_found_y[idx]) ) # end else # free up memory #unload_camera_image(img_array) PI.py_clear_framebuffer() PI.py_illuminate_float() #PC.cameraClose() # also frees up image buffer memory #end for num_points -= num_sub # perform mapping operation print("STATUS:\tMappingFunctions: generating mapping\n") PC.cameraClose() # also frees up image buffer memory self.generateMapping(points_found_x, points_found_y, \ points_to_illum_x, points_to_illum_y, num_points) self.writeMappingFile() for i in range(num_points): print("illuminated(%d,%d)" % \ (points_to_illum_x[i],points_to_illum_y[i]) ) print("found(%d,%d)" % (points_found_x[i],points_found_y[i]) ) print(" ") # end for print("Finished map generation\n")