def get_enhance_bounds(chf_fpaths, low_theshold, high_threshold): # initialize the pixels array pix = [] # open 100 images max (time consuming) if len(chf_fpaths) > 100: chf_fpaths = random.sample(chf_fpaths, 100) # create a for loop here counter = 1 for image_path in chf_fpaths: # open the image print "Getting pixels in Image " + image_path print str(counter) + " / " + str(len(chf_fpaths)) counter += 1 imp_orig = Opener().openImage(image_path) # get the pixel values image_pix = list(imp_orig.getProcessor().getPixels()) imp_orig.close() imp_orig.flush() # select randomly 10% of the pixels (maybe memory issues) image_pix_sel = random.sample(image_pix, int(len(image_pix) * 0.1)) pix = pix + image_pix_sel # get the percentile values to threshold IJ.log('Quantifying pixel values for contrast enhancement...') low_pix = percentile(pix, low_theshold) high_pix = percentile(pix, high_threshold) return low_pix, high_pix
for channel in range(1, (number_of_channels_in_mouse + 1)): channel_files = getChannelFiles(MouseIDFiles, channel) # get the full path chf_fpaths = [path.join(In_dir, x) for x in channel_files] # get the minimum and maximum pixel value min_pixval, max_pixval = get_enhance_bounds( chf_fpaths, low_theshold, high_threshold) IJ.log("Found pixel bounds " + str(min_pixval) + " and " + str(max_pixval) + " for channel " + str(channel)) counter = 1 for chfile in chf_fpaths: # open file ch_img = Opener().openImage(chfile) ch_tit = ch_img.getTitle() # adjust contrast ch_img.getProcessor().setMinAndMax(min_pixval, max_pixval) # convert to 8-bit (which also applies the contrast) ImageConverter(ch_img).convertToGray8() # save IJ.saveAsTiff(ch_img, path.join(Out_dir, ch_tit)) # close and flush ch_img.close() ch_img.flush() print("Image " + str(counter) + " of " + str(len(chf_fpaths)) + " processed") counter += 1 IJ.log('Mouse ' + MouseID + ' processed') print("DONE, find your results in " + Out_dir)