def archive_human_masks(human_directory, new_directory, work_directory): ''' For a directory of hand-drawn masks, mask out everything in the accompanying bright-field file except for the worm itself and a 100-pixel surrounding area to save disk space. Also, re-compress all images to maximize compression and space efficiency. ''' for a_subdir in os.listdir(human_directory): if os.path.isdir(human_directory + os.path.sep + a_subdir): folderStuff.ensure_folder(new_directory + os.path.sep + a_subdir) for a_file in os.listdir(human_directory + os.path.sep + a_subdir): if a_file.split(' ')[-1] == 'hmask.png': if not os.path.isfile(new_directory + os.path.sep + a_subdir + os.path.sep + a_file): print('Up to ' + a_subdir + ' ' + a_file + '.') my_stem = a_file.split(' ')[0] my_mask = freeimage.read(human_directory + os.path.sep + a_subdir + os.path.sep + my_stem + ' ' + 'hmask.png') bf_path = human_directory + os.path.sep + a_subdir + os.path.sep + my_stem + ' ' + 'bf.png' if os.path.isfile(bf_path): my_image = freeimage.read(bf_path) else: my_image = freeimage.read(bf_path.replace(human_directory, work_directory)) area_mask = my_mask.copy().astype('bool') distance_from_mask = scipy.ndimage.morphology.distance_transform_edt(np.invert(area_mask)).astype('uint16') area_mask[distance_from_mask > 0] = True area_mask[distance_from_mask > 100] = False my_image[np.invert(area_mask)] = False freeimage.write(my_image, new_directory + os.path.sep + a_subdir + os.path.sep + my_stem + ' ' + 'bf.png', flags = freeimage.IO_FLAGS.PNG_Z_BEST_COMPRESSION) freeimage.write(my_mask, new_directory + os.path.sep + a_subdir + os.path.sep + my_stem + ' ' + 'hmask.png', flags = freeimage.IO_FLAGS.PNG_Z_BEST_COMPRESSION) elif a_file.split('.')[-1] == 'json': shutil.copyfile(human_directory + os.path.sep + a_subdir + os.path.sep + a_file, new_directory + os.path.sep + a_subdir + os.path.sep + a_file) return
def pick_full_masks(working_dir, human_dir, temporary_dir): ''' Copy the bright-field images to trace (giving up on automated wormFinding) from working_dir to temporary_dir. ''' my_masks = select_daily(working_dir) my_masks = remove_already_have(my_masks, human_dir) for a_mask in my_masks: base_folder = temporary_dir subdirectory_folder = os.path.sep.join(a_mask.split(os.path.sep)[:-1]).replace(working_dir, temporary_dir) folderStuff.ensure_folder(base_folder) folderStuff.ensure_folder(subdirectory_folder) shutil.copyfile(a_mask, a_mask.replace(working_dir, temporary_dir)) return
def pick_masks_to_trace(working_dir, human_dir, temporary_dir): ''' Copy the bright-field images to trace from working_dir to temporary_dir. ''' my_masks = random_select_egg_ages(working_dir) my_masks = remove_already_have(my_masks, human_dir) for a_mask in my_masks: base_folder = temporary_dir subdirectory_folder = os.path.sep.join(a_mask.split(os.path.sep)[:-1]).replace(working_dir, temporary_dir) folderStuff.ensure_folder(base_folder) folderStuff.ensure_folder(subdirectory_folder) if os.path.isfile(a_mask): shutil.copy(a_mask, a_mask.replace(working_dir, temporary_dir)) return
def totally_random(working_dir, human_dir, temporary_dir, image_number = 200): ''' Select image_number masks from working_dir without regard for age bins. ''' my_masks = random_select(working_dir, image_number) my_masks = remove_already_have(my_masks, human_dir) for a_mask in my_masks: base_folder = temporary_dir subdirectory_folder = os.path.sep.join(a_mask.split(os.path.sep)[:-1]).replace(working_dir, temporary_dir) folderStuff.ensure_folder(base_folder) folderStuff.ensure_folder(subdirectory_folder) if os.path.isfile(a_mask): shutil.copy(a_mask, a_mask.replace(working_dir, temporary_dir)) return
def select_final_outlines(color_directory, final_directory, working_directory, egg_mode = False): ''' Select the properly filled outlines from color_directory and move them into final_directory along with the appropriate metadata and bright field files from working_directory. ''' mask_ending = 'hmask' if egg_mode: mask_ending = 'emask' for a_subdir in os.listdir(color_directory): folderStuff.ensure_folder(final_directory + os.path.sep + a_subdir) for an_image in os.listdir(color_directory + os.path.sep + a_subdir): if an_image.split(' ')[-1] == 'outline.png': destination_mask = final_directory + os.path.sep + a_subdir + os.path.sep + an_image.replace('outline', mask_ending) outline_image = freeimage.read(color_directory + os.path.sep + a_subdir + os.path.sep + an_image) my_dimensions = len(outline_image.shape) if my_dimensions == 2: shutil.copyfile(color_directory + os.path.sep + a_subdir + os.path.sep + an_image.replace('outline', mask_ending), destination_mask) elif my_dimensions == 3: fixed_outline = outline_image[:, :, 0].astype('uint8') fixed_outline[fixed_outline > 0] = -1 freeimage.write(fixed_outline, destination_mask) shutil.copyfile(working_directory + os.path.sep + a_subdir + os.path.sep + an_image.replace('outline', 'bf'), destination_mask.replace(mask_ending, 'bf')) shutil.copyfile(working_directory + os.path.sep + a_subdir + os.path.sep + 'position_metadata_extended.json', final_directory + os.path.sep + a_subdir + os.path.sep + 'position_metadata_extended.json') return